当我尝试使用@Autowired时,它给了我例外
我使用@Autowired annotation
添加的内容context:annotation-config /
bean id =“GoAnalyserService”class =“com.dataanalyser.serviceimpl.GoAnalyserServiceImpl”/
context:component-scan base-package =“com.dataanalyser.service”/
我的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>
我的控制器类
@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();
}
}
我的服务类
public interface GoAnalyserService {
public Integer checkUserAuth(UserLogData userLogData);
}
我的服务实施类
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;
}
}
当我在没有@Autowired注释的情况下试用它时,这段代码很好用,但是当我添加@Autowired它给了我
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
还有更多这样的, 我认为spring-dispatcher-servlet.xml中出现了问题,我搜索了很多,仍然无法找到错误的东西。我有任何需要添加到项目中的jar文件.... ??
答案 0 :(得分:1)
将@Repo放在GoAnalyserServiceImpl类
上
当您使用@Autowired时,您可能无法解决创建对象的问题。并且使用@Autowired必须位于类@Controller or @Repo or @Service / only @Component.
之上注释用于将类标记为Spring 3中的控制器。
答案 1 :(得分:0)
尝试删除
<bean id = "goAnalyserService" class = "com.dataanalyser.serviceimpl.GoAnalyserServiceImpl"/>
来自xml配置。
并将GoAnalyserServiceImpl注释为@Component。
答案 2 :(得分:0)
首先,确保您在XML中声明的类存在于包中。然后,使用@Component注释注释您的类。 Spring使用这些注释来查找在使用@Autowired时应该实例化的类。
答案 3 :(得分:0)
您的组件扫描声明是:
<context:component-scan base-package="com.dataanalyser.service"/>
,而您的异常消息指出:
Cannot find class com.dataanalyser.serviceimpl.GoAnalyserService
因此,组件扫描中的包声明错误。成功:
<context:component-scan base-package="com.dataanalyser.serviceimpl"/>
或将课程的包更改为com.dataanalyser.service
当然假设您的服务类已正确注释