Spring @Bean autowire() - 第二次(?)时间失败

时间:2015-06-17 15:53:45

标签: java spring dependency-injection autowired

好的,所以,我有一个创建两个bean的配置类。它们定义如下:

public class FileUploadConfiguration {

    public static final String UPLOAD_PREFIX = "file.upload";

    @Bean(name = "fileService", autowire = Autowire.BY_TYPE)
    @DependsOn("fileUploadPaths")
    @Autowired
    public CRUDFileService fileService(@Qualifier("fileUploadPaths") PrefixedPropertyFactoryBean fileUploadPaths) throws Exception {
        Map<String, String> paths = Maps.fromProperties(fileUploadPaths.getObject());
        return new CRUDFileService(File.class.getName(), paths);
    }

    @Bean(name = "fileUploadPaths")
    public PrefixedPropertyFactoryBean fileUploadPaths(Environment environment) {
        PrefixedPropertyFactoryBean fileUploadPaths = new PrefixedPropertyFactoryBean();
        fileUploadPaths.setPrefix(UPLOAD_PREFIX);
        fileUploadPaths.setLocations(ResourceUtils.getActiveResources(environment));
        return fileUploadPaths;
    }
}

如果我在两个实例中都放置断点,则流程如下:

1) fileUploadPaths 被实例化,看起来对我来说是正确的。

2) fileService 获得实例化,但通过 FileUploadConfiguration bean创建的 postProcessPropertyValues ...这对我来说似乎很奇怪。 enter image description here

3)之后,我得到一个例外,因为看起来Spring似乎再次尝试创建 fileService ,但现在通过 createBean ,并且由于某种原因,@ Qualifier关于参数定义似乎被忽略了。

  

线程中的异常&#34; main&#34;   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为&#39; fileService&#39;的bean时出错在课堂上定义   x.y.z.FileUploadConfiguration:表达不满意的依赖关系   通过索引为0的构造函数参数   [a.b.c.PrefixedPropertyFactoryBean]:不明确的工厂方法参数   types - 您是否将正确的bean引用指定为工厂方法   参数呢?在   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:735)     在   org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)     在   org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)

注意:如果我将 fileService 定义更改为以下内容,则会修复此问题:

@Bean(name = "fileService")
@DependsOn("fileUploadPaths")
@Autowired
public CRUDFileService fileService(@Qualifier("fileUploadPaths") PrefixedPropertyFactoryBean fileUploadPaths, AutowireCapableBeanFactory factory) throws Exception {
    Map<String, String> paths = Maps.fromProperties(fileUploadPaths.getObject());
    CRUDFileService crudFileService = new CRUDFileService(File.class.getName(), paths);
    factory.autowireBean(crudFileService);
    return crudFileService;
}

因此,我不是让Spring通过 @Bean(autowire = Autowire.BY_TYPE)自动装配我的bean,而只是自动装配 AutowireCapableBeanFactory 并自动装配 CRUDFileService < / strong> bean我自己。

我的问题是:为什么会发生这种行为?我正确使用 @Bean(autowire = Autowire.BY_TYPE)吗?我之前使用它并且它像魅力一样工作,除了我没有像我用这个那样自动调整bean方法参数。

1 个答案:

答案 0 :(得分:0)

我看到的问题是你试图将Spring FactoryBean作为依赖注入另一个bean。 FactoryBean是特殊的,它是一个bean的工厂,结果bean是你应该注入其他对象的。

因此,假设您在CRUDFileService中的依赖项是Properties类而不是FactoryBean,如果您要尝试这样做,它应该干净利落:

public class FileUploadConfiguration {

    public static final String UPLOAD_PREFIX = "file.upload";

    @Bean(name = "fileService", autowire = Autowire.BY_TYPE)
    @Autowired
    public CRUDFileService fileService(@Qualifier("fileUploadPaths") Properties fileUploadPaths) throws Exception {
        Map<String, String> paths = Maps.fromProperties(fileUploadPaths);
        return new CRUDFileService(File.class.getName(), paths);
    }

    @Bean(name = "fileUploadPaths")
    public PrefixedPropertyFactoryBean fileUploadPaths(Environment environment) {
        PrefixedPropertyFactoryBean fileUploadPaths = new PrefixedPropertyFactoryBean();
        fileUploadPaths.setPrefix(UPLOAD_PREFIX);
        fileUploadPaths.setLocations(ResourceUtils.getActiveResources(environment));
        return fileUploadPaths;
    }
}