0
votes

When i try to use @Autowired it gives me exceptions

Things I have added to use @Autowired annotation

  • context:annotation-config /

  • bean id = "GoAnalyserService" class ="com.dataanalyser.serviceimpl.GoAnalyserServiceImpl"/

  • context:component-scan base-package="com.dataanalyser.service"/

My spring-dispatcher-servlet

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <context:component-scan base-package="com.dataanalyser.controller"/>
    <!-- <context:component-scan base-package="com.dataanalyser"/> -->
    <context:component-scan base-package="com.dataanalyser.dao"/>
    <context:component-scan base-package="com.dataanalyser.service"/> 
    <mvc:annotation-driven/>

    <mvc:default-servlet-handler/>
    <context:annotation-config />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value="/WEB-INF/" />            
        <property name = "suffix" value = ".html" />
    </bean>    

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/GoAnalyserDB" />
        <property name="username" value="postgres" />
        <property name="password" value="toor" />
    </bean>

    <bean id = "goAnalyserService" class = "com.dataanalyser.serviceimpl.GoAnalyserServiceImpl"/>
    <bean id = "userDao" class = "com.dataanalyser.daoimpl.UserDaoImpl"/>
</beans>

My controller class

@Controller
public class GoAnalyserControl {

    public GoAnalyserControl()
    {
        System.out.println("------- Inside the controller -------");
    }

    @Autowired
    GoAnalyserService goAnalyserService;

    @RequestMapping(value = "/logInChecker", method = RequestMethod.POST, consumes = {"application/json"})
    public @ResponseBody String logInCheckerFn(@RequestBody UserLogData userLogData){
        System.out.println("inside loginChecker()");
        //GoAnalyserService goAnalyserService = new GoAnalyserServiceImpl(); 
        Integer userAuthFlag = goAnalyserService.checkUserAuth(userLogData);
        System.out.println(userAuthFlag.toString());
        return userAuthFlag.toString();
    }
}

My Service class

public interface GoAnalyserService {

    public Integer checkUserAuth(UserLogData userLogData);

}

My Service implementation class

public class GoAnalyserServiceImpl implements GoAnalyserService {

    @Autowired
    UserDao userDao;

    @Override 
    public Integer checkUserAuth(UserLogData userLogData){
        //UserDao userDao = new UserDaoImpl(); 
        if((userDao.getUserCount(userLogData.getUserName())) == 1){
            String dbPass = userDao.getUserPass(userLogData.getUserName());
            if( dbPass.equals(userLogData.getPassword()))
                return 1;
            else
                return 2;
        }else
            return 0;
    }

}

When i try it out without @Autowired annotation, this code works out fine, but when i add @Autowired it gives me

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'goAnalyserControl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.dataanalyser.service.GoAnalyserService com.dataanalyser.controller.GoAnalyserControl.goAnalyserService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.dataanalyser.serviceimpl.GoAnalyserService] for bean with name 'GoAnalyserService' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.dataanalyser.serviceimpl.GoAnalyserService
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.dataanalyser.service.GoAnalyserService com.dataanalyser.controller.GoAnalyserControl.goAnalyserService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.dataanalyser.serviceimpl.GoAnalyserService] for bean with name 'GoAnalyserService' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.dataanalyser.serviceimpl.GoAnalyserService
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.dataanalyser.serviceimpl.GoAnalyserService] for bean with name 'GoAnalyserService' defined in ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.dataanalyser.serviceimpl.GoAnalyserService

and few more like this, I think something is wrong in spring-dispatcher-servlet.xml, I have searched a lot, still cant find the thing wrong. I there any jar file that need to be added to the project ....??

4
The message says: Cannot find class [com.dataanalyser.serviceimpl.GoAnalyserService]. I guess the class is not in that package. - JB Nizet
If you're using annotations, why aren't you using @Service for your service classes? - GriffeyDog

4 Answers

1
votes

Put @Repo on GoAnalyserServiceImpl class

May be resolve your error beacuse when you used @Autowired you dont wory about creating object.And use @Autowired must be at top of class @Controller or @Repo or @Service / only @Component. annotation is used to mark the class as the controller in Spring 3.

0
votes

Try to remove

    <bean id = "goAnalyserService" class = "com.dataanalyser.serviceimpl.GoAnalyserServiceImpl"/>

from xml configuration.

And annotate GoAnalyserServiceImpl as @Component.

0
votes

First of all, make sure that the class you are declaring in your XML exists inside the package. Then, annotate your class with @Component annotation. Spring uses these annotations to find the class it is supposed to instantiate when @Autowired is used.

0
votes

Your component-scan declaration is:

<context:component-scan base-package="com.dataanalyser.service"/>

while your exception message states that :

Cannot find class com.dataanalyser.serviceimpl.GoAnalyserService

So you have a wrong package declaration in your component scan. Make it :

<context:component-scan base-package="com.dataanalyser.serviceimpl"/>

or change the package of the class to com.dataanalyser.service

Assuming of course that your service class is annotated properly