Spring @Bean at method returning already created bean

人走茶凉 提交于 2019-12-25 18:32:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!