为什么Spring的@Configurable有时可以工作,有时候不工作?

Rob*_*ell 10 java spring annotations dependency-injection jpa

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

它主要起作用.当我的Tomcat启动时,我看到AspectJ init消息,我的User对象自动获取FileService引用等.

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

线索:

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

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

实体

@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;

...
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

applicationContext.xml中

...

<context:spring-configured />

<context:load-time-weaver />

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

...
Run Code Online (Sandbox Code Playgroud)

Jör*_*rer 1

当注入不起作用时,无论出于何种原因,代码都无法进行任何依赖项检查,因此现在会抛出错误。

否则我在这里看不到任何表明随机故障的信息。你能提取一个简化的例子来检查吗?