2015-11-02 89 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

要包含多个包,请尝试使用下面的代码行,而不是使用两个标签''。这应该有所帮助。 –

相关问题