多个数据源配置

时间:2013-07-16 12:44:52

标签: java spring hibernate datasource

我在一个包含两个dataSources的项目上工作。 如果我想注入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>

setter:

@Required
@Resource
public void setSecurityDataSource(DataSource securityDataSource) {
    jdbcTemplate = new JdbcTemplate(securityDataSource);
}

服务器为安全性和默认数据源记录此信息(显然不会阻止任何内容):

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)

代码中的任何地方都没有显示堆栈跟踪。

当我尝试:

private SecurityUserDTO retrieve(String userLogin) {
    return jdbcTemplate.query("SELECT * FROM users WHERE username = ?", with(userLogin), new UserExtractor());
}

Hibernate不会发现表用户存储在数据库安全性中,因为它在默认数据库中找不到它(没有这样的表)

HTTP Status 401 - PreparedStatementCallback; bad SQL grammar [SELECT * FROM users WHERE username = ?]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'condor.users' doesn't exist

如果我尝试注入并提供名称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实例化。

请帮助我,谢谢!

1 个答案:

答案 0 :(得分:0)

实际上我的defaultDataSource bean是在applicationContext.xml中声明的,而我的securityDataSource bean是在applicationContext-security.xml中,虽然没有声明在context.xml中由contextConfigLocation浏览。 以下是更改的示例代码:

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

它有效! 我见过的securityDataSource bean属于另一个上下文...