標題說明了一切。無法在UserDetailsService中自動裝載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中 你能幫我理解發生了什麼嗎?
後'的UserDetailsService class'源請 –
什麼是你CustomUserServiceDetails的包? –
'ContextLoaderListener'加載所謂的根上下文,即'DispatcherServlet'子進程。子上下文可以從父項查找bean,而不是從子項查找。你的dao是/孩子,因此'UserDetailsService'找不到它,因爲它是在根上下文中定義的。 –