HelloWorld HelloWorld - 2 months ago 30
Java Question

Error while creating a bean : unsatisfied dependency expressed

@Controller

@Controller
public class ControllerClass {
@Autowired
ServiceClass serviceClass;

@RequestMapping("/")
public String getAll(Model model) {
System.out.println(serviceClass.getAll());
model.addAttribute("contents", serviceClass.getAll());
return "NewFile";
}
}


Repository

@Repository
public interface RepositiryClass {

List<Domain> get();
}


RepoImpl

public class RepoImpl implements RepositiryClass {

@Override
public List<Domain> get() {
List<Domain> dList = new ArrayList<>();
Domain one = new Domain("testOne", "testTwo");
Domain two = new Domain("testTwo", "TestOne");
Domain three = new Domain("three", "three");

dList.add(one);
dList.add(two);
dList.add(three);

return dList;
}


@Service

@Service
public interface ServiceClass {

List<Domain> getAll();
}


ServiceClassImpl

public class ServiceClassImpl implements ServiceClass {

@Autowired
RepositiryClass repositriyClass;

@Override
public List<Domain> getAll() {

return repositriyClass.get();
}

}


Exception

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controllerClass': Unsatisfied dependency expressed through field 'serviceClass': No qualifying bean of type [com.controller.domain.service.ServiceClass] found for dependency [com.controller.domain.service.ServiceClass]: 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.controller.domain.service.ServiceClass] found for dependency [com.controller.domain.service.ServiceClass]: 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)


root cause

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'serviceClassImpl': Unsatisfied dependency expressed through field 'repositriyClass': No qualifying bean of type [com.controller.domain.repo.RepositiryClass] found for dependency [com.controller.domain.repo.RepositiryClass]: 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.controller.domain.repo.RepositiryClass] found for dependency [com.controller.domain.repo.RepositiryClass]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)


web.xml

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


dispatcherServlet-servlet.xml

<mvc:annotation-driven />
<context:component-scan base-package="com.*" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>


package declaration

enter image description here

After Searching various forms, I am still not able to solve that
Why I am getting this error as I have everyting implemented and still not sure why I'm getting this error and how to solve that.

Thanks

Answer

Add the @Service Annotation to ServiceClassImpl. Not to the Interface.

@Service
public class ServiceClassImpl implements ServiceClass {

    @Autowired
    RepositiryClass repositriyClass;

    @Override
    public List<Domain> getAll() {

        return repositriyClass.get();
    }

}

Interface:

public interface ServiceClass {

    List<Domain> getAll();
}

also add the @Repository Annotation to the Repository implementation:

@Repository
public class RepoImpl implements RepositiryClass {

    @Override
    public List<Domain> get() {
        List<Domain> dList = new ArrayList<>();
        Domain one = new Domain("testOne", "testTwo");
        Domain two = new Domain("testTwo", "TestOne");
        Domain three = new Domain("three", "three");

        dList.add(one);
        dList.add(two);
        dList.add(three);

        return dList;
    }
}

Not to the interface:

public interface RepositiryClass {

    List<Domain> get();
}

Remember: The Annotation always belongs to the implementations of the Interfaces. Not to the Interface itselfs