【发布时间】:2016-04-01 23:22:11
【问题描述】:
我有一个用作 Rest API 的 Spring 3 项目,并且想将我拥有的 Spring bean 连接到一个非托管类中以用于日志记录。
在尝试了许多不同的事情之后,有效的方法是使用注释 @Configurable 标记我的非托管类。
喜欢:
@Configurable
public class ClassNotManagedBySpring {
@Autowired
@Qualifier("myBean")
private MyBean myBean;
}
@Service("myBean")
public class MyBean {
@Autowired
@Qualifier("someOtherBean")
private SomeOtherBean someOtherBean;
}
然后在我的 beans.xml 中:
<context:spring-configured/>
所以现在假设 ClassNotManagedBySpring.java 是 6 个类之一,它们都做类似的事情,除了其中 3 个由 spring 管理,因为它们具有 @Component 注释。
但是所有这 6 个类都需要 @Autowire MyBean.java 并且只有一些需要 @Configurable 注释。
请注意,我之前已经在此应用中使用 AspectJ 用于多种其他目的。
我想知道我的 spring 应用程序突然以这种方式将 spring 托管依赖项连接到非托管类中的风险是什么?
可能存在性能问题吗?运行时出错的风险?
如果这不是将 Spring 托管 bean 连接到非托管类的最佳方式,那是什么?
【问题讨论】:
标签: java spring dependency-injection inversion-of-control aspectj