views:

107

answers:

2

Hi

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?

Thanks Er

A: 

Could you define a bean id and use the @Qualifier annotation to differentiate the two, one in your test class and one in the actual code?

Murali VP
+5  A: 

Why do you need to include the JdbcUserDetailsManager in your web/servlet application context? The WebApplicationContext gets the main ApplicationContext as a parent "automatically" (if you configure it correctly.) See this IBM article for an example of setting contextConfigLocation so the web app knows where to find the main ApplicationContext.

Or this example: 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>
Jeanne Boyarsky
Thanks for the answer. My problem is not the webapp, but the unit tests which have nothing to do with the webapp. The webapp runs fine withouth the bean defintion, but I cannot run the unit tests without the bean definition mentioned.
Right. But you can pass as many xml files as you want in the @ContextConfiguration(locations={myStringArrayOfXmlFiles})In other words, the test can take care of loading both XML files so you don't need to do an include yourself.
Jeanne Boyarsky
Ah thanks ... didn't know that ... +1