返回已创建bean的方法的Spring @Bean

时间:2017-07-19 07:03:14

标签: spring javabeans autowired

我有以下类的StaticDataCachesServerBean,它由它的接口自动装配,即

@Autowired 
private StaticDataCaches staticDataCaches; 

在另一个班级

我已经在sdcAutoAssignmentRuleCache类的init方法中创建了StaticDataCachesServerBean的实例。根据我的理解,如果带有sdcAutoAssignmentRuleCache的注释@Bean不会产生任何影响,因为实例已经创建,并且在调用方法时返回相同的内容。 我的理解是否正确?

如果我不使用sdcAutoAssignmentRuleCache()注释@Bean方法,如果我使用@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;
    }

}

0 个答案:

没有答案