2015-11-02 61 views
0

標題說明了一切。無法在UserDetailsS​​ervice中自動裝載DAO類

這裏是我的應用程序的安全性-context.xml的

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
         http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd 
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

    <global-method-security pre-post-annotations="enabled" /> 

    <http auto-config="true" use-expressions="true"> 

     <intercept-url pattern="/login" access="permitAll" /> 
     <intercept-url pattern="/show" access="hasRole('ROLE_USER')" /> 
     <intercept-url pattern="/action" access="hasRole('ROLE_USER')" /> 

     <form-login 
      login-page="/login" 
      default-target-url="/show" 
      always-use-default-target="true" 
      authentication-failure-url="/login?error" 
      username-parameter="username" 
      password-parameter="password" 
     /> 

     <logout logout-success-url="/login?logout" />  

    </http> 

    <!-- <authentication-manager> 
     <authentication-provider> 
      <user-service> 
       <user name="jimi" password="jimi" 
        authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> --> 

    <context:component-scan base-package="com.security" /> 
    <context:component-scan base-package="com.dao" /> 

    <authentication-manager> 
    <authentication-provider user-service-ref="user-details"/> 
    </authentication-manager> 

</beans:beans> 

我得到以下異常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined 

但實體管理器Bean與所有定義的servlet-context.xml的 沿有關DBMS連接的信息。

必須注意的是,例如,我可以在控制器中自動裝載DAO類。

的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:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:camel="http://camel.apache.org/schema/spring" xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" 
    > 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 
    <!-- 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/secure/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <aop:aspectj-autoproxy /> 
    <context:component-scan base-package="com.tagrate.controller" /> 
    <context:component-scan base-package="com.model" /> 
    <context:component-scan base-package="com.tagrate.dao" /> 
    <context:component-scan base-package="com.tagrate.service" /> 
    <context:component-scan base-package="com.security" /> 

    <beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <beans:property name="basename" value="messages"> 
     </beans:property> 
    </beans:bean> 

    <!-- MySQL Datasource with Commons DBCP connection pooling --> 
    <beans:bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"> 
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <beans:property name="url" value="jdbc:mysql://localhost:3306/hello"/> 
    <beans:property name="username" value="root"/> 
    <beans:property name="password" value="root"/> 
    </beans:bean> 

    <!-- EntityManagerFactory --> 
    <beans:bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> 

    <beans:property name="persistenceUnitName" value="myunit" /> 
    <beans:property name="dataSource" ref="dataSource" /> 

     </beans:bean> 

    <!-- Transaction Manager --> 
    <beans:bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> 
    <beans:property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </beans:bean> 

    <!-- Enable @Transactional annotation --> 
    <tx:annotation-driven/> 

</beans:beans> 

編輯:

OK我解決了這個問題,我不得不添加sollowing線security.xml文件

<!-- EntityManagerFactory --> 
    <beans:bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> 

    <beans:property name="persistenceUnitName" value="myunit" /> 
    <beans:property name="dataSource" ref="dataSource" /> 

     </beans:bean> 

    <!-- MySQL Datasource with Commons DBCP connection pooling --> 
    <beans:bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"> 
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <beans:property name="url" value="jdbc:mysql://localhost:3306/hello"/> 
    <beans:property name="username" value="root"/> 
    <beans:property name="password" value="root"/> 
    </beans:bean> 

我真的不知道爲什麼,這些info已經包含在servlet-context.xml中 你能幫我理解發生了什麼嗎?

+0

後'的UserDetailsS​​ervice class'源請 –

+0

什麼是你CustomUserServiceDetails的包? –

+0

'ContextLoaderListener'加載所謂的根上下文,即'DispatcherServlet'子進程。子上下文可以從父項查找bean,而不是從子項查找。你的dao是/孩子,因此'UserDetailsS​​ervice'找不到它,因爲它是在根上下文中定義的。 –

回答

0

你能檢查您是否已經出現在servlet上下文文件組件掃描標籤,如下

<context:component-scan base-package="your.base.package" /> 
+0

是的,我已經說過,我可以在控制器中自動安裝dao類,問題是當我嘗試在UserDetailsS​​ervice類上做同樣的事情時,它找不到與我在servlet-context上編寫的實體管理器相關的信息。 xml – GionJh

+0

要包含多個包,請嘗試使用下面的代碼行,而不是使用兩個標籤''。這應該有所幫助。 –

相關問題