为什么 Spring 的 @Configurable 有时有效,有时无效? [英] Why does Spring's @Configurable sometimes work and sometimes not?

查看:36
本文介绍了为什么 Spring 的 @Configurable 有时有效,有时无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Spring 的 @Configurable 注释和 @Resource 在需要注入的字段上使用自动依赖注入.这涉及一些设置,比如将 spring-agent.jar 传递给我的 JVM.有关详细信息请参阅此处.

I'm trying to use automatic dependency injection via Spring's @Configurable annotation w/ @Resource on the fields needing injection. This involved some setup, like passing spring-agent.jar to my JVM. For the full details see here.

它有效......主要是.当我的 Tomcat 启动时,我看到 AspectJ 初始化消息,我的用户对象自动获取 FileService 引用等.

It works... mostly. When my Tomcat is booting up, I see the AspectJ init messages, my User objects automatically get FileService references, etc.

问题是有时它不会发生.它似乎是完全随机的;有时我启动时没有注入依赖项,有时它们是.我以前在我的用户上使用 @Transactional 时遇到了麻烦,因为它造成了冲突,我相信是代理.我正在使用 JPA,所以我的用户被标记为 @Entity,所以我现在最好的猜测是这会造成冲突.我读过你不能自动代理代理.为了抵消冲突,我遵循了一些我在网上找到的关于排除 Hibernate(我的 JPA impl)使用的 CGLIBjavassist 的注释.

The problem is that sometimes it just doesn't happen. It appears to be completely random; sometimes I boot up and the dependencies are not injected, sometimes they are. I previously had trouble with @Transactional being on my User because it created a conflict, I believe with proxies. I am using JPA, so my User is marked with @Entity, so my best guess right now is that this is creating a conflict. I've read you can't auto proxy a proxy. To offset the conflict, I followed some notes I found online about excluding CGLIB and javassist which Hibernate (my JPA impl) uses.

线索:

  • 要么全有,要么全无.我的所有 @Configurable 实例都已注入或没有注入.
  • 从数据库重新加载(重新实例化)实体似乎没有帮助;它要么有效,要么无效.
  • 多次重启 Tomcat 也无法修复它.似乎再次掷骰子的唯一事情是重新部署.换句话说,如果我重新部署它可能会起作用.

我怎样才能弄清楚出了什么问题?有人在 JPA 中使用 @Configurable 吗?为什么我的 dependencyCheck = true 在没有实际注入依赖项时不抛出错误?

How can I figure out what is going wrong? Is anyone using @Configurable with JPA? Why isn't my dependencyCheck = true throwing an error when dependencies are not actually injected?

实体

@Entity
@Configurable(dependencyCheck = true)
@NamedQueries( { @NamedQuery(name = "User.findAll", query = "SELECT user FROM User user"),
    @NamedQuery(name = "User.findByEmail", query = "SELECT user FROM User user WHERE user.email = :email") })
public abstract class User extends BaseModel {

private static final long serialVersionUID = 7881431079061750040L;

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;

@Column(unique = true, nullable = false)
private String email;

@Basic(optional = false)
private String password;

@Resource
private transient UserEmailer userEmailer;

@Resource
private transient FileService fileService;

...

aop.xml

<!DOCTYPE aspectj PUBLIC
    "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver options="-verbose">
        <include within="com.myapp.domain..*" />
        <exclude within="*..*CGLIB*" />
        <exclude within="*..*javassist*" />
    </weaver>
    <aspects>
        <aspect name="org.springframework.beans.factory.aspectj.AbstractInterfaceDrivenDependencyInjectionAspect" />
    </aspects>
</aspectj>

applicationContext.xml

...

<context:spring-configured />

<context:load-time-weaver />

<context:component-scan base-package="com.myapp" />

...

推荐答案

对我来说,这听起来像是 Spring 中一个众所周知的错误的发生:http://jira.springframework.org/browse/SPR-5401.

To me it sounds like an occurence of a well known bug in Spring: http://jira.springframework.org/browse/SPR-5401.

您是否正在尝试在多个应用程序上下文中使用 Configurable?在这种情况下,只有其中一个会受到依赖注入.哪一个获胜取决于哪个应用程序上下文是最后加载的.

Can it be that you are trying to use Configurable in several application contexts? In this case only one of them will be subject to dependency injection. Which one wins depends on which application context is the last one to be loaded.

解决方案?无 :-( 目前没有解决这个问题的计划.至少 SpringSource 的人在 4 月份德国 JAX 会议上是这么说的.

Solution? None :-( There are no plans to fix this issue. That is at least what the SpringSource guy told at the JAX conference in Germany in April.

这篇关于为什么 Spring 的 @Configurable 有时有效,有时无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆