2013-07-16 137 views
2

我在一個項目上工作,女巫包含兩個數據源。 如果我想注入securityDataSource,彈出注入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> 

二傳手:

@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()); 
} 

休眠不會發現存儲在數據庫安全表的用戶,因爲它看起來它在默認數據庫(它不具有這樣的表)

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實例化。

請幫助我,謝謝!

+0

請確保在運行時只有一個spring上下文(並且所有的bean都去了它)。問題可能是你有多個上下文。你的數據源被聲明爲一個並注入另一個。當你有兩個上下文的情況下的例子:如果你使用Spring MVC,那麼你通常有一個用於應用程序bean(服務,DAO等)的上下文,另一個用於Spring MVC控制器。 –

+0

任何想法爲什麼你看到兩個條目都帶有「BeanPostProcessors」警告?我希望只能看到JndiObjectFactoryBean行而不是BasicDataSource行。此外,您是否可以讓應用程序只使用其中一項,但不能同時使用這兩項? – enl8enmentnow

回答

0

實際上,我的defaultDataSource bean是在applicationContext.xml中聲明的,而我的securityDataSource bean是在applicationContext-security.xml中,儘管沒有聲明在context.xml中通過contextConfigLocation瀏覽web.xml。 這裏是已更改示例代碼:

<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屬於另一個上下文......