【发布时间】:2018-11-11 23:13:32
【问题描述】:
当我尝试运行一个 Spring Boot 项目时,它告诉我它无法自动装配一些在配置类中实例化的 bean。 我认为spring不能按顺序加载那些配置类。
堆栈跟踪:no bean found the be autowired Ignite<Long,MyEntity> myEntityCache in MyDao
这里是来源:
主类
@SpringBootApplication
// The beans in the IgniteConfig have to be loaded before dao, service, and Controller
@ComponentScan(basePackageClasses={IgniteConfig.class,AppConfig.class})
public class DemoIgnite {
public static void main(String[] args) {
SpringApplication.run(DemoIgnite .class, args);
}
}
配置类 1
@Configuration
public class IgniteConfig {
@Bean
public SpringContext springContext() {
return new SpringContext();
}
@Bean
public Ignite igniteInstance(@Autowired SpringContext springContext) {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setIgniteInstanceName("instance");
List<CacheConfiguration> ccDas = new ArrayList<>();
CacheConfiguration cch = new CacheConfiguration<>("myEntitycache");
cch.setCacheMode(CacheMode.REPLICATED);
cch.setIndexedTypes(Long.class, myEntity.class);
ccDas.add(cch);
cfg.setCacheConfiguration( ccDas.toArray(new CacheConfiguration[0]));
SpringCacheManager springCacheManager = new SpringCacheManager();
springCacheManager.setConfiguration(cfg);
return Ignition.start(cfg);
}
@Bean
public IgniteCache<Long, MyEntity> myEntityCache(@Autowired Ignite igniteInstance) {
return igniteInstance.cache("myEntitycache");
}
配置类 2
@Configuration
@ComponentScan({
"com.demo.repository",
"com.demo.service",
"com.demo.controller"
})
public class AppConfig {
}
道类
@Repository
public class MyDao{
@Autowired
private Ignite<Long,MyEntity> myEntityCache;
...
服务类:
@Service
public class MyService{
@Autowird
private MyDao dao;
...
控制器类:
@RestController
@RequestMapping
public class MyController{
@Autowired
private MyService service;
....
【问题讨论】:
-
不要将它们作为参数,而是尝试在 myEntityCache() 中调用 igniteInstance(),然后从 igniteInstance() 中删除参数(因为它似乎不需要 SpringContext 来做任何事情.. Spring通常会先找到所有可能的 bean,然后再创建任何 - 所以声明的顺序应该无关紧要......(我认为,只要你没有循环,它就会根据需要对它们进行排序......)。
标签: spring spring-boot