FactoryBeans和Spring 3.0中基于注释的配置 [英] FactoryBeans and the annotation-based configuration in Spring 3.0
问题描述
Spring提供了 FactoryBean
接口,允许对bean进行非平凡的初始化。该框架提供了工厂bean的许多实现,并且 - 当使用Spring的XML配置时 - 工厂bean很容易使用。
Spring provides the FactoryBean
interface to allow non-trivial initialisation of beans. The framework provides many implementations of factory beans and -- when using Spring's XML config -- factory beans are easy to use.
然而,在Spring 3.0中,我不能找到一种令人满意的工厂bean使用基于注释的配置(néeJavaConfig)的方法。
However, in Spring 3.0, I can't find a satisfactory way of using factory beans with the annotation-based configuration (née JavaConfig).
显然,我可以手动实例化工厂bean并自己设置所有必需的属性,如下所示:
Obviously, I could manually instantiate the factory bean and set any required properties myself, like so:
@Configuration
public class AppConfig {
...
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource());
factory.setAnotherProperty(anotherProperty());
return factory.getObject();
}
但是,如果 FactoryBean
实现了任何特定于Spring的回调接口,例如 InitializingBean
, ApplicationContextAware
,例如,BeanClassLoaderAware
或 @PostConstruct
。我还需要检查FactoryBean,找出它实现的回调接口,然后通过调用 setApplicationContext
, afterPropertiesSet()$来实现此功能。 c $ c>等。
However, this would fail if the FactoryBean
implemented any Spring-specific callback interfaces, like InitializingBean
, ApplicationContextAware
, BeanClassLoaderAware
, or @PostConstruct
for example. I also need to inspect the FactoryBean, find out what callback interfaces it implements, then implement this functionality myself by calling setApplicationContext
, afterPropertiesSet()
etc.
这对我来说感觉很尴尬和反过来:应用程序开发人员不应该实现IOC容器的回调。
This feels awkward and back-to-front to me: application-developers should not have to implement the callbacks of the IOC container.
有没有人知道使用Spring Annotation配置的FactoryBeans更好的解决方案?
Does anyone know of a better solution to using FactoryBeans from Spring Annotation configs?
推荐答案
据我所知你的问题是你想要的结果 sqlSessionFactory()
是一个 SqlSessionFactory
(用于其他方法),但您必须从 @Bean
-annotated方法返回 SqlSessionFactoryBean
为了触发Spring回调。
As far as I understand your problem is what you want a result of sqlSessionFactory()
to be a SqlSessionFactory
(for use in other methods), but you have to return SqlSessionFactoryBean
from a @Bean
-annotated method in order to trigger Spring callbacks.
可以通过以下解决方法解决:
It can be solved with the following workaround:
@Configuration
public class AppConfig {
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactoryBean() { ... }
// FactoryBean is hidden behind this method
public SqlSessionFactory sqlSessionFactory() {
try {
return sqlSessionFactoryBean().getObject();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Bean
public AnotherBean anotherBean() {
return new AnotherBean(sqlSessionFactory());
}
}
重点是调用 @Bean
-annotated方法被一个方面拦截,该方面执行返回的bean的初始化(在你的情况下为 FactoryBean
),以便调用 sqlSessionFactoryBean()
in sqlSessionFactory()
返回完全初始化的 FactoryBean
。
The point is that calls to @Bean
-annotated methods are intercepted by an aspect which performs initialization of the beans being returned (FactoryBean
in your case), so that call to sqlSessionFactoryBean()
in sqlSessionFactory()
returns a fully initialized FactoryBean
.
这篇关于FactoryBeans和Spring 3.0中基于注释的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!