无法自动装配字段(Spring mvc - 注释)
Could not autowire field (Spring mvc - annotations)
我无法通过注入 Spring 的 bean 解决我的问题。我用 jar 模块(有服务、dao、ropositories ...)和 war 模块(有控制器和应用程序配置)创建了多模块项目。当我在 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();
}
}
这是登录 class:
@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 上下文加载器找不到 UserRepository 的子 class。
就我而言,
我从外部 a.properties 获取属性,但忘记将属性添加到 a.properties 文件中。因为我们有一个包含类似文件 (a.properties) 的批处理应用程序,我正在该批处理的 属性 文件中添加值。
我无法通过注入 Spring 的 bean 解决我的问题。我用 jar 模块(有服务、dao、ropositories ...)和 war 模块(有控制器和应用程序配置)创建了多模块项目。当我在 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();
}
}
这是登录 class:
@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 上下文加载器找不到 UserRepository 的子 class。
就我而言,
我从外部 a.properties 获取属性,但忘记将属性添加到 a.properties 文件中。因为我们有一个包含类似文件 (a.properties) 的批处理应用程序,我正在该批处理的 属性 文件中添加值。