问题
I have below class StaticDataCachesServerBean which is autowired by it's interface i.e.
@Autowired
private StaticDataCaches staticDataCaches;
in some another class
I already created instance of sdcAutoAssignmentRuleCache
in init method of StaticDataCachesServerBean
class. As per my understanding if annotate sdcAutoAssignmentRuleCache
with @Bean
won't make any impact as instance is already created and same is returned when method is called.
Is my understanding correct?
What will happen if I don't annotate sdcAutoAssignmentRuleCache()
method with @Bean
and what will happen if I annotate with @Bean
?
@Configuration
public class StaticDataCachesServerBean implements StaticDataCaches {
@Autowired
private CerebrumApplication cerebrumApplication;
private SdcUserCache sdcUserCache;
private SdcAutoAssignmentRuleCache sdcAutoAssignmentRuleCache;
@Override
public void init() {
sdcUserCache = new SdcUserCacheImpl(
new CacheUserProviderImpl(cerebrumApplication.getCacheProvider()));
sdcAutoAssignmentRuleCache= new SdcAutoAssignmentRuleCacheImpl(
new CacheSdcAutoAssignmentRuleProviderImpl(cerebrumApplication.getCacheProvider()));
}
@Bean
@Override
public SdcAutoAssignmentRuleCache sdcAutoAssignmentRuleCache() {
return sdcAutoAssignmentRuleCache;
}
}
来源:https://stackoverflow.com/questions/45183164/spring-bean-at-method-returning-already-created-bean