0

I am getting the following error :

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

Source code :

FactoryBo.java

I added @Autowired on top od CleanupProvider class; I did it for corresponding interface as well; But it didn't work for me;

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

So, here I want to make the existing method createInstance as a factory method;

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>

Is there anything I missed?

Ram Kowsu
  • 711
  • 2
  • 10
  • 30

2 Answers2

0

You can add @Component to your CleanupProvider class:

@Component
public class CleanupProvider { ...}

Or define a method to create your been like so:

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

An explanation of which annotation to use is best described here: Understanding Spring @Autowired usage

karen
  • 893
  • 2
  • 13
  • 38
0

I believe you just missed providing your CleanupProvider class a @Bean annotation (like @Component, @Service, @Repository, @Controller) so that bean factory recognizes it and is able to Autowire, like this Spring documentation tells you to do so.

EDIT

Ok, so have seen your spring.xml, I assume the mistake was you were declaring the bean as ICleanupProvider class, which is the interface, whereas FactoryBO autowires the implementation (CleanupProvider).