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,所以我现在最好的猜测是这会产生冲突.我读过你不能自动代理代理.为了抵消冲突,我在网上找到了一些关于排除CGLIB和javassist的注释,这些注释是Hibernate(我的JPA impl)使用的.
线索:
我怎么能弄清楚出了什么问题?是否有人使用@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)
| 归档时间: |
|
| 查看次数: |
11558 次 |
| 最近记录: |