无法在UserDetailsS​​ervice中自动装配DAO类

时间:2015-11-02 13:01:55

标签: java spring security autowired

标题说明了一切。

这是我的app-security-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>

编辑:

好的我解决了它我必须在security.xml中添加sollowing行

 <!-- 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>

我真的很想知道为什么,这些信息已经包含在servlet-context.xml中了 你能帮我理解发生了什么吗?

1 个答案:

答案 0 :(得分:0)

您可以检查servlet上下文文件中是否存在组件扫描标记,如下所示

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