Java 多数据源配置

Java 多数据源配置,java,spring,hibernate,datasource,Java,Spring,Hibernate,Datasource,我在一个包含两个数据源的项目上工作。 如果我想注入securityDataSource,spring将注入defaultDataSource applicationContext.xml: <bean id="defaultDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>${defa

我在一个包含两个数据源的项目上工作。 如果我想注入securityDataSource,spring将注入defaultDataSource

applicationContext.xml:

<bean id="defaultDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>${default.jndi.datasource.name}</value>
    </property>
</bean>

<bean id="securityDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>${security.jndi.datasource.name}</value>
    </property>
</bean>
服务器为安全性和默认数据源记录以下内容(显然没有阻止任何内容):

10:28:23.912 [INFO ] o.s.w.c.s.XmlWebApplicationContext - Bean 'defaultDataSource' of type [class org.springframework.jndi.JndiObjectFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
10:28:24.345 [INFO ] o.s.w.c.s.XmlWebApplicationContext - Bean 'defaultDataSource' of type [class org.apache.commons.dbcp.BasicDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
10:28:24.922 [INFO ] o.s.w.c.s.XmlWebApplicationContext - Bean 'securityDataSource' of type [class org.springframework.jndi.JndiObjectFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
10:28:25.444 [INFO ] o.s.w.c.s.XmlWebApplicationContext - Bean 'securityDataSource' of type [class org.apache.commons.dbcp.BasicDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
代码中的任何位置都不显示stacktrace

当我尝试时:

private SecurityUserDTO retrieve(String userLogin) {
    return jdbcTemplate.query("SELECT * FROM users WHERE username = ?", with(userLogin), new UserExtractor());
}
Hibernate找不到存储在数据库安全性中的表用户,因为它在默认数据库(没有这样的表)中查找它

如果我尝试注入并提供名称securityDataSource:

@Required
@Resource(name="securityDataSource")
public void setSecurityDataSource(DataSource securityDataSource) {
    jdbcTemplate = new JdbcTemplate(securityDataSource);
}
我将获得stackTrace:

12:17:48.557 [ERROR] o.s.w.c.ContextLoader - Context initialization failed
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'securityDataSource' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:159) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
Wrapped by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'securityDataSource' is defined
然而,bean securityDataSource确实存在,并且由spring在上下文中实例化


请帮帮我,谢谢

实际上,我的defaultDataSource bean是在applicationContext.xml中声明的,而我的securityDataSource bean是在applicationContext-security.xml中声明的,尽管contextConfigLocation没有声明要在web.xml中浏览。 以下是已更改的示例代码:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

上下文配置位置
/WEB-INF/applicationContext.xml
致:


上下文配置位置
/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-security.xml
而且它有效!
我看到的securityDataSource bean属于另一个上下文…

请确保在运行时只有一个spring上下文(这样所有bean都会进入它)。问题可能是您有多个上下文。您的数据源在一个中声明,在另一个中注入。有两个上下文的情况示例:如果您使用SpringMVC,那么您通常有一个用于应用程序bean(服务、DAO等)的上下文,另一个用于SpringMVC控制器。您知道为什么看到两个条目都带有“BeanPostProcessors”警告吗?我希望只看到JndiObjectFactoryBean行,而不看到BasicDataSource行。此外,您是否能够让应用程序只使用其中一个条目,而不是同时使用两个条目?
12:17:48.557 [ERROR] o.s.w.c.ContextLoader - Context initialization failed
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'securityDataSource' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:159) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303) ~[spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
Wrapped by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'securityDataSource' is defined
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml, /WEB-INF/applicationContext-security.xml</param-value>
</context-param>