[英]In Spring javaconfig, how to initialize a @Bean which depends on a @Service
我已经将基于Spring 4.0的项目从xml转换为javaconfig。
在初始化时,我的一个bean需要通过Spring @Service( buildingService
)访问Hibernate以从DB获取一些配置数据。 bean初始化如下所示:
@Bean
@DependsOn({ "transactionManager", "webSocketHandler", "buildingService" })
Smarty smarty() {
Smarty bean = new Smarty();
bean.init(); // I also tried @Bean(initMethod = "init") with no difference
return bean;
}
问题是在bean.init()
,访问了服务,该服务因NullPointerException
而失败。
我将buildingService
添加到@DependsOn
但它没有帮助。
可能在@Bean
之后处理@Service
-annotated类!?
我可以自己初始化@Service
-annotated类吗?
编辑1
感谢所有反馈!
我需要添加一些细节:
buildingService不是@Bean
,它是@Service
,看起来像这样:
@Service("buildingService")
@Transactional
public class BuildingService {
...
public List<Building> getAll() {
final Session session = sessionFactory.getCurrentSession();
final Query query = session.createQuery("from Building order by name");
return query.list();
}
...
}
Smarty是一个Spring托管Bean,并在@Configuration
-annotated类中初始化,该类正在进行根上下文的初始化。
Smarty直接依赖于buildingService,如下所示:
@Resource(name = "buildingService")
private BuildingService buildingService;
我尝试使用@PostConstruct
注释Smarty.init()
,但这并没有改变任何东西。
请注意, Smarty.init()
所做的第一件事是调用buildingService.getAll();
你对bean的生命周期感到困惑。 Spring必须首先创建bean才能注入任何东西。 在你的@Bean
方法中,你已经创建了你的bean
Smarty bean = new Smarty();
然后立即调用其中一种方法
bean.init();
这似乎取决于注入的领域。
这两个电话之间没有任何内容。 你怎么期望Spring做任何事情?
相反,您可以使用@PostConstruct
注释您的init()
方法。 一旦Spring完成初始化你的bean,即。 当你的@Bean
方法返回并且Spring注入所有对象的注入目标时,它将自动调用该方法。
这里不需要@DependsOn
。
@Sevice
注释bean被自动发现并通过组件扫描进行初始化,以便在Spring Configuration上使用@ComponentScan
。
@ComponentScan
配置组件扫描指令以与
@Configuration
类一起使用。
@Bean
用于手动创建bean,而不使用@Service
或组件扫描等特殊注释。
@Bean
表示方法生成由Spring容器管理的bean。 (...)通常,@ Node方法在@Configuration类中声明。 在这种情况下,bean方法可以通过直接调用它们来引用同一个类中的其他@Bean方法。
上下文配置
@Autowired
EntityManager entityManager; //needs to access Hibernate
@Bean
Smarty smarty() {
return = new Smarty(entityManager);
}
还有你的Smarty
豆子
public Smarty {
final EntityManager entityManager;
public Smarty(EntityManager entityManager){
this.entityManager = entityManager;
}
}
您不需要@DependsOn
注释,因为Smarty bean已经(或应该)直接依赖于BuildingService。 有关何时使用它的更多信息,请参阅@DependsOn
javadoc。
以下示例演示了如何解决问题:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SmartyTest.TestConfig.class)
public class SmartyTest {
@Autowired
Smarty1 smarty1;
@Autowired
Smarty2 smarty2;
@Test
public void testSmarty() throws Exception {
}
@Configuration
static class TestConfig {
@Bean
public BuildingService buildingService() {
return new BuildingService();
}
@Bean
public Smarty1 smarty1(BuildingService buildingService) {
Smarty1 smarty = new Smarty1(buildingService);
smarty.init();
return smarty; // manually inject dependencies & handle initialisation
}
@Bean
public Smarty2 smarty2() {
// injecting the building service & initialising the component is handled by spring
// by using @Autowired & @PostConstruct(-> alternative to @Bean(initMethod="init"))
return new Smarty2();
}
}
static class BuildingService {
public void buildSomething() {
System.out.println("BuildingService: I am building something!");
}
}
static class Smarty1 {
BuildingService buildingService;
Smarty1(BuildingService buildingService) {
this.buildingService = buildingService;
}
public void init() {
System.out.println("Smarty 1: initialising ...");
buildingService.buildSomething();
}
}
static class Smarty2 {
@Autowired
BuildingService buildingService;
@PostConstruct
public void init() {
System.out.println("Smarty 2: initialising ...");
buildingService.buildSomething();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.