gpt4 book ai didi

java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'userController' : Injection of autowired dependencies failed

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:49 27 4
gpt4 key购买 nike

我浏览了很多相关主题并尝试了不同的方法,但错误仍然存​​在。环境:Spring data jpa、Hibernate、maven、tomcat7

错误堆栈:

org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zzz.service.UserService com.zzz.controller.UserController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.zzz.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at ...

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" ... ... >
<context:component-scan base-package="com.zzz">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/zzz" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.zzz.entity" />
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<jpa:repositories base-package="com.zzz.repository" />

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" ... ... >
<context:component-scan base-package="com.zzz.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="static/" />

<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/templates.xml</value>
</list>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
</beans>

UserController.java

@Controller
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("/users")
public String users(Model model) {
model.addAttribute("users", userService.findAll());
return "users";
}
}

UserService.java

@Service
public class UserService {

@Autowired
private UserRepository userRepository;

public List<User> findAll() {
return userRepository.findAll();
}
}

UserRepository.java

public interface UserRepository extends JpaRepository<User, Integer> {

}

UDPATE 1

有 2 个 Autowiring 错误:

无法 Autowiring 字段:私有(private) com.zzz.repository.UserRepository com.zzz.service.UserService.userRepository

无法 Autowiring 字段:私有(private) com.zzz.service.UserService com.zzz.controller.UserController.userService

更新2

我尝试使用servlet-dispatcher.xml作为唯一的配置文件(将applicationContext.xml的内容放入其中),它工作得很好。所以这是这2个xml文件之间的问题,但我无法告诉它。

更新3

我将applicationContext.xml重命名为root-context.xml,并在web.xml中将contextConfigLocation设置为它,错误消失了!

似乎是有关 applicationContext.xml 的一些配置错误,有人可以帮忙吗?

最佳答案

另一个猜测:在调度程序 xml 中,您只扫描 Controller 包...因此,如果未加载 application.xml,它不会扫描服务包,因此也不会加载用户服务...如果您在调度程序.xml 中扫描整个 com.zzz 包,会发生什么,将会很有趣

关于java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'userController' : Injection of autowired dependencies failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242950/

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