我设置了以下Spring Java配置:
@Configuration
public class FooConfig {
@Autowired
private IReferenceDataDAO referenceDataDAO;
@Bean
public IReferenceDataService getReferenceDataService() {
return new ReferenceDataServiceImpl(referenceDataDAO);
}
}
我试图使用@Autowire属性在另一个配置类中引用 ReferenceDataService bean:
@Configuration
public class BarConfig {
@Autowired
private IRuleService ruleService;
@Autowired
private IReferenceDataService referenceDataService;
}
以下是定义IReferenceDataDAO的配置:
@Configuration
public class FooBarConfig {
@Bean
public IReferenceDataDAO getReferenceDataDAO() {
return new ReferenceDataDAOImpl(getStaticData(), getMapper());
}
}
这是ReferenceDataServiceImpl:
public class ReferenceDataServiceImpl implements IReferenceDataService {
private IReferenceDataDAO dao;
public ReferenceDataServiceImpl(IReferenceDataDAO dao) {
this.dao = dao;
}
上述配置是在主配置类中导入的:
@Configuration
@Import({
FoosAppConfig.class,
BarAppConfig.class,
FooBarAppConfig.class,
})
我注意到此配置导致 ReferenceDataServiceImpl 中的 referenceDAO 设置为null。我究竟做错了什么?不应该@Autowire注释确保我的bean在设置之前完全配置了吗?
答案 0 :(得分:0)
BarConfig需要导入FooConfig @Import({FooConfig.class})
答案 1 :(得分:0)
试试这个:
@Configuration
@Import({ FooConfig.class })
public class BarConfig {
@Autowired
private IRuleService ruleService;
@Autowired
private IReferenceDataService referenceDataService;
}
这允许BarConfig访问IReferenceDataService bean。