當我嘗試使用@Autowired這讓我異常@Autowired領域引起org.springframework.beans.factory.BeanCreationException
事我已經加入到使用@Autowired註解
背景:註釋-config/
豆ID = 「GoAnalyserService」 類= 「com.dataanalyser.serviceimpl.GoAnalyserServiceImpl」/
上下文:組分掃描基包= 「com.dataanalyser.service」/
彈簧調度員的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文件....?
有消息稱註解的:找不到類[com.dataanalyser.serviceimpl。 GoAnalyserService。我猜這個班不在那個包裏。 –
如果您使用註釋,爲什麼不使用'@ Service'作爲您的服務類? – GriffeyDog