Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在UserDetailsService中自动连接DAO类_Java_Spring_Security_Autowired - Fatal编程技术网

Java 无法在UserDetailsService中自动连接DAO类

Java 无法在UserDetailsService中自动连接DAO类,java,spring,security,autowired,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/X

标题说明了一切

这是我的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>
<?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>
但是实体管理器bean是由servlet-context.xml定义的 以及有关DBMS连接的所有信息

必须注意的是,例如,我能够在控制器中自动连接DAO类

servlet-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>
<?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中了
您能帮我了解发生了什么吗?

您能检查一下servlet上下文文件中是否有如下组件扫描标记吗

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


是的,正如我说过的,我可以在控制器中自动连接dao类,问题是当我尝试在UserDetailsService类上执行相同操作时,它无法找到我在servlet-context.xml上编写的实体管理器的相关信息。要包含多个包,请尝试使用以下行,而不是使用两个标记
。这应该会有帮助。post
UserDetails服务类
source请您的CustomUserServiceDetails包是什么?
ContextLoaderListener
加载所谓的根上下文,即
DispatcherServlet
子上下文。子上下文可以从父级查找bean,而不是从子级查找根。您的dao在/在子级中,因此,
UserDetailsService
无法找到根上下文中定义的dao。是否有一种方法可以在不在security.xml中描述实体管理器的情况下组织工作?我是否应该使用root-context.xml?