2015-01-11 38 views
1

我無法解決注入Spring的bean問題。我用jar模塊創建了多模塊項目(有服務,dao,ropositories ......)和war模塊(有contollers和aplications配置)。當我開始我的Jboss應用程序,我得到這個異常:無法自動填充字段(Spring mvc - 註釋)

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; 
} 

我將是幫助,非常感謝。

+1

UserRepository的子類在哪裏?您無法注入接口,您必須在運行時將UserRepository的實現注入。查看錯誤/異常 –

+1

只是猜測你的'root-context.xml'沒有掃描存儲庫/服務,但只有'servlet-context.xml'。 –

回答

0

在您的異常仔細看會告訴你一切:

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。

+0

我認爲這是錯誤的答案。它應該工作,但他的配置是錯誤的。我使用Java配置文件,並且遇到同樣的問題,我修復了它,添加了@EnableJpaRepositories(basePackages = {「com.smartintranet.repository」})。我不確定xml配置,但他可能應該使用 Purzynski