通过字段表示的对工厂bean的不满意依赖性

时间:2018-09-21 09:32:52

标签: spring

我遇到以下错误:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'factoryBO': Unsatisfied dependency expressed through field 'cleanupProvider': No qualifying bean of type [com.spring.factory.interfaces.impl.CleanupProvider] found for dependency [com.spring.factory.interfaces.impl.CleanupProvider]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.factory.interfaces.impl.CleanupProvider] found for dependency [com.spring.factory.interfaces.impl.CleanupProvider]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.spring.factory.runner.FactoryRunner.main(FactoryRunner.java:10)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spring.factory.interfaces.impl.CleanupProvider] found for dependency [com.spring.factory.interfaces.impl.CleanupProvider]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570)
    ... 15 more

源代码:

FactoryBo.java

我在CleanupProvider类的顶部添加了@Autowired;我也为相应的接口做了它;但这对我不起作用;

package com.spring.factory.bo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.spring.factory.interfaces.ICleanupProvider;
import com.spring.factory.interfaces.impl.CleanupProvider;

@Component
public class FactoryBO {

    @Autowired
    CleanupProvider cleanupProvider;


    public void getFactoryProviderLogic() {
        cleanupProvider.performCleanup("Algo");
    }
}

CleanupProvider.java

  package com.spring.factory.interfaces.impl;

    import com.spring.factory.interfaces.ICleanupProvider;
    import com.spring.factory.interfaces.ICleanupStrategy;

    public class CleanupProvider implements ICleanupProvider {

        @Override
        public String performCleanup(String strate) {
            System.out.println("Received Text:::"+ strate);
            return strate+":"+"Received";
        }

        @Override
        public void registerStrategy(ICleanupStrategy normalizeStrategy) {
            System.out.println("NormalizationProvider:::registerStrategy::");
        }

    }

CleanupProviderFactory .java

因此,在这里我想将现有方法createInstance设置为工厂方法;

package com.spring.factory.interfaces.impl;

import com.spring.factory.interfaces.ICleanupProvider;
import com.spring.factory.interfaces.ICleanupProviderFactory;

public class CleanupProviderFactory implements ICleanupProviderFactory {

    public ICleanupProvider createInstance() {
        ICleanupProvider normalizeProvider = new CleanupProvider();
        normalizeProvider.registerStrategy(new CleanupStrategy());
        return normalizeProvider;

    }

    public static ICleanupProviderFactory createFactoryInstance() {
        return new CleanupProviderFactory();
    }

}

ICleanupProvider.java

 package com.spring.factory.interfaces;

    public interface ICleanupProvider {

        public String performCleanup(String algo);

        public void registerStrategy(ICleanupStrategy strategy);

    }

ICleanupProviderFactory.java

    package com.spring.factory.interfaces;

    public interface ICleanupProviderFactory {

    }




   package com.spring.factory.runner;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import com.spring.factory.bo.FactoryBO;

    public class FactoryRunner {

        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            FactoryBO bo = context.getBean(FactoryBO.class);
            bo.getFactoryProviderLogic();
            context.close();
        }
    }

spring.xml

<context:annotation-config/>
    <context:component-scan base-package="com.spring.factory.bo"></context:component-scan>

    <bean id="cleanupProviderFactory"
        class="com.spring.factory.interfaces.impl.CleanupProviderFactory"
        factory-method="createFactoryInstance"></bean>

    <bean id="cleanupProvider"
        class="com.spring.factory.interfaces.ICleanupProvider"
        factory-bean="cleanupProviderFactory" lazy-init="default" factory-method="createInstance"></bean>

有什么我想念的吗?

2 个答案:

答案 0 :(得分:0)

您可以将@Component添加到CleanupProvider类中:

@Component
public class CleanupProvider { ...}

或定义一种创建您的方法的方法,

@Bean 
public CleanupProvider getCleanupProvider()  { 
    return new CleanupProvider();
}

此处最好说明使用哪种注释:Understanding Spring @Autowired usage

答案 1 :(得分:0)

我相信您只是错过了为CleanupProvider类提供@Bean批注(如@Component@Service@Repository@Controller),以便Bean工厂能够识别并能够自动连线,就像this Spring documentation告诉您这么做。

编辑

好吧,看过您的spring.xml了,我认为错误是因为您将bean声明为ICleanupProvider类,这是接口,而FactoryBO自动装配了实现(CleanupProvider)。