gpt4 book ai didi

java - @Autowired 字段导致 org.springframework.beans.factory.BeanCreationException

转载 作者:行者123 更新时间:2023-12-01 11:41:09 24 4
gpt4 key购买 nike

当我尝试使用 @Autowired 时,它给了我异常(exception)

我添加了使用 @Autowired 注释的内容

  • 上下文:注释配置/

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

  • 上下文:component-scan base-package="com.dataanalysisr.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 类

@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 文件需要添加到项目中....??

最佳答案

Put @Repo on GoAnalyserServiceImpl class

可能会解决您的错误,因为当您使用@Autowired时,您不必担心创建对象。并且使用@Autowired必须位于类的顶部@Controller或@Repo或@Service/仅@Component。 注解用于在 Spring 3 中将类标记为 Controller 。

关于java - @Autowired 字段导致 org.springframework.beans.factory.BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29515338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com