为什么Spring的@Configurable有时可以工作,有时不工作? [英] Why does Spring's @Configurable sometimes work and sometimes not?

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

问题描述

我正在尝试通过Spring的@Configurable注释w @ @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的初始化消息,我的User对象自动获取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)使用的 CGLIB javassist 的笔记。

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任何时间也不会修复它。唯一再次出现骰子的事情是重新部署。换句话说,如果我重新部署它可能会工作。

我如何知道出了什么问题?有人使用@Configurable与JPA?为什么我的依赖关系没有被真正注入时引发错误?

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天全站免登陆