Reputation: 3678
When i try to run a spring boot project, it tolde me that it can not autowire some beans whitch are instanciated in a configuration classes. I think that spring can not load those configuration classes in order.
The stack trace : no bean found the be autowired Ignite<Long,MyEntity> myEntityCache in MyDao
Here is the source :
The main class
@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);
}
}
Config Class 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");
}
Config class 2
@Configuration
@ComponentScan({
"com.demo.repository",
"com.demo.service",
"com.demo.controller"
})
public class AppConfig {
}
Dao class
@Repository
public class MyDao{
@Autowired
private Ignite<Long,MyEntity> myEntityCache;
...
Service class:
@Service
public class MyService{
@Autowird
private MyDao dao;
...
Controller class:
@RestController
@RequestMapping
public class MyController{
@Autowired
private MyService service;
....
Upvotes: 3
Views: 3724
Reputation: 44932
This means that you don't have a bean of Ignite<Long,MyEntity>
type in your context. Moreover springContext
bean seems redundant, it's not used by igniteInstance
bean. As pointed out by moilejter it probably should be:
IgniteConfig
@Bean
public Ignite ignite() {
...
}
@Bean
public IgniteCache<Long, MyEntity> myEntityCache() {
return ignite().cache("myEntitycache");
}
MyDao
@Repository
public class MyDao {
@Autowired
private IgniteCache<Long, MyEntity> myEntityCache;
...
}
In principle Spring performs the bean setup in few phases as explained in chapter 1.3.2. Instantiating Beans docs:
Bean definition discovery - resources like @Configuration
classes or XML
files are scanned and bean signatures are collected.
Eager beans instantiation e.g. singletons - from the definitions collected in point 1 while resolving dependencies between definitions. That's why there is no explicit bean instantiation order as the process is driven from dependencies.
Lazy beans instantiation e.g. @Lazy
annotated - when the context is already up, this beans will be constructed only when accessed from code.
Upvotes: 1