2010-08-01 79 views
0

当试图从Spring Security的自动装配JdbcUserDetailsManager,我用下面的语句appcontext.xml(位于Web应用程序分开):春天的Autowire重复的问题

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

当运行单元测试,一切都很好。当开始我的web应用程序,它有它自己的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分别运行?

回答

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> 
+0

感谢您的回答。我的问题不是webapp,而是与webapp无关的单元测试。没有bean定义,webapp运行良好,但是我不能在没有提到bean定义的情况下运行单元测试。 – 2010-08-01 17:26:51

+0

没错。但是,您可以在@ContextConfiguration(locations = {myStringArrayOfXmlFiles})中传递尽可能多的xml文件。 换句话说,测试可以负责加载这两个XML文件,因此您不需要自己执行include操作。 – 2010-08-01 17:54:53

+0

啊谢谢......不知道那个...... +1 – 2010-08-01 17:58:01

1

您可以定义一个bean id并使用@Qualifier批注来区分两个,一个在您的测试类中,另一个在实际代码中?