我无法解决注入(inject) Spring bean 的问题。我使用 jar 模块(有服务、dao、ropositories ...)和 war 模块(有 Controller 和应用程序配置)创建了多模块项目。当我在 Jboss 上启动应用程序时,出现以下异常:
Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'login': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.engineering.pawel.service.UserService com.engineering.pawel.controller.Login.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.engineering.pawel.repository.UserRepository com.engineering.pawel.service.UserService.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.engineering.pawel.repository.UserRepository] 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)}
这是我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml,
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- ........................................................................... -->
<!-- Spring Security -->
<!-- ........................................................................... -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/my-emf</persistence-unit-ref-name>
<persistence-unit-name>my-jpa</persistence-unit-name>
</persistence-unit-ref>
</web-app>
下面是servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:annotation-config />
<!-- Handles HTTP GET requests for /resources by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<context:component-scan base-package="com.engineering.pawel" />
<!-- Database configuration -->
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/postgreSQL"
resource-ref="true" />
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/my-emf"
expected-type="javax.persistence.EntityManagerFactory" />
<beans:bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<beans:property name="transactionManagerName" value="java:/TransactionManager" />
</beans:bean>
<beans:bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
</beans:beans>
下面是UserRepository.java
package com.engineering.pawel.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.engineering.pawel.pojo.User;
@Repository
public interface UserRepository extends JpaRepository<User,Integer>{
}
下面是UserService.java
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
public void addUser(final String userNick, final String userPassword){
final User user = new User();
user.setNick(userNick);
user.setPassword(userPassword);
userRepository.saveAndFlush(user);
}
public List<User> getUsers(){
return this.userRepository.findAll();
}
}
这是登录类:
@Controller
public class Login {
@Autowired
private UserService userService;
}
我将非常感谢您的帮助。
最佳答案
仔细观察你的异常会告诉你一切:
NoSuchBeanDefinitionException: No qualifying bean of type
[com.engineering.pawel.repository.UserRepository] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
这仅仅意味着:
@Repository
public interface UserRepository extends JpaRepository<User,Integer>{
}
和
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository; // Here you need to have a Bean
// Implementing this UserRepository as an autowire candidate
...
}
Spring Context Loader 没有找到 UserRepository 的子类。
关于java - 无法 Autowiring 字段(Spring mvc - 注释),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27886864/