当尝试从Spring Security自动装配JdbcUserDetailsManager时,我在appcontext.xml中使用以下语句(与webapp分开):
<bean class="org.springframework.security.provisioning.JdbcUserDetailsManager">
<property name="dataSource" ref="dataSource"/>
</bean>
运行单元测试时一切都很好。当我启动我的网络应用程序时,它有自己的appcontext.xml,包括原始的appcontext.xml,我得到一个重复的错误:
No unique bean of type
[org.springframework.security.provisioning.JdbcUserDetailsManager] is defined:
expected single matching bean but found 2:
[org.springframework.security.provisioning.JdbcUserDetailsManager#0,
org.springframework.security.provisioning.JdbcUserDetailsManager#1]
如何优化我的两个appcontext.xml以分别运行服务层测试和webapp?
答案 0 :(得分:6)
为什么需要在Web / servlet应用程序上下文中包含JdbcUserDetailsManager? WebApplicationContext将主ApplicationContext作为父“自动”获取(如果您正确配置它。)有关设置contextConfigLocation的示例,请参阅this IBM article,以便Web应用程序知道在哪里可以找到主ApplicationContext。
或者这个例子: contextConfigLocation的 /WEB-INF/main-application-config.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mine</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-config.xml</param-value>
</init-param>
</servlet>
答案 1 :(得分:1)
你能定义一个bean id并使用@Qualifier注释来区分这两个,一个在测试类中,一个在实际代码中?