0

When trying to autowire JdbcUserDetailsManager from Spring Security, I use following statement in appcontext.xml (located separated from webapp):

 <bean class="org.springframework.security.provisioning.JdbcUserDetailsManager">
    <property name="dataSource" ref="dataSource"/>                                                                
</bean>

When running unit test all is fine. When starting my web app, which has it's own appcontext.xml including the original appcontext.xml, I get an duplicate error:

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]

How can I refine my two appcontext.xml in order to get both, the service layer tests and the webapp running respectively?

4

2 回答 2

6

为什么需要在 Web/servlet 应用程序上下文中包含 JdbcUserDetailsManager?WebApplicationContext “自动”获取主 ApplicationContext 作为父级(如果配置正确)。有关设置 contextConfigLocation 的示例,请参阅此 IBM 文章,以便 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>
于 2010-08-01T17:16:57.877 回答
1

您能否定义一个 bean id 并使用 @Qualifier 注释来区分两者,一个在您的测试类中,一个在实际代码中?

于 2010-08-01T16:50:50.107 回答