(第一次在这个论坛发帖提问,所以请忽略任何愚蠢的东西) 处理面向Spring Boot的新项目,该项目有多个配置类(Security,DataSourceConfig,DataConfig)。问题是DataConfig正在使用DataSourceConfig中定义的一些bean,但是由于DataConfig首先加载,所以它没有得到那种依赖。
@Configuration
public class DataSourceConfig {
@Bean(name="abcDataSource")
public DataSource abcDataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/ABC");
return dataSource;
}
@Bean(name="xyzDataSource")
@Lazy
public DataSource xyzDataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/XYZ");
return dataSource;
}
}
@Import({DataSourceConfig.class})
@Configuration
public class DataConfig {
@Autowired
DataSource xyzDataSource;
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws Exception {
PropertyPlaceholderConfigurer p = new PropertyPlaceholderConfigurer();
p.setProperties(commonsConfigurationFactoryBean());
return p;
}
@Bean
public Properties commonsConfigurationFactoryBean() {
return ConfigurationConverter.getProperties(databaseConfiguration());
}
@Bean
@Autowired
public DatabaseConfiguration databaseConfiguration() {
return new DatabaseConfiguration(xyzDataSource, "Table_Name", "Key_Column", "Value_Column");
}
}
因此,当我在DEBUG模式下启动TOMCAT服务器(不是嵌入式服务器)时,在DataConfig.databaseConfiguration()和DataSourceConfig.xyzDataSource()上有断点,它总是转到前一个而不是以后,这导致问题并给出“NullPointerException”以获取数据源。
我不知道为什么'@Import'不适用于此。任何帮助都会非常适合。如果您需要我的任何其他信息,请告诉我。
异常追踪:
Caused by: java.lang.NullPointerException
at org.apache.commons.configuration.DatabaseConfiguration.getConnection(DatabaseConfiguration.java:568) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.DatabaseConfiguration.getKeys(DatabaseConfiguration.java:515) ~[commons-configuration-1.6.jar:1.6]
at org.apache.commons.configuration.ConfigurationConverter.getProperties(ConfigurationConverter.java:112) ~[commons-configuration-1.6.jar:1.6]
at com.mcmcg.apollo.common.datasource.DataConfig.commonsConfigurationFactoryBean(DataConfig.java:35) ~[classes/:?]
at com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.CGLIB$commonsConfigurationFactoryBean$1(<generated>) ~[classes/:?]
at com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776$$FastClassBySpringCGLIB$$40819bbc.invoke(<generated>) ~[classes/:?]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.commonsConfigurationFactoryBean(<generated>) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_73]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_73]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_73]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1023) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:381) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.commonsConfigurationFactoryBean(<generated>) ~[classes/:?]
at com.mcmcg.apollo.common.datasource.DataConfig.propertyPlaceholderConfigurer(DataConfig.java:29) ~[classes/:?]
at com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.CGLIB$propertyPlaceholderConfigurer$0(<generated>) ~[classes/:?]
at com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776$$FastClassBySpringCGLIB$$40819bbc.invoke(<generated>) ~[classes/:?]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.propertyPlaceholderConfigurer(<generated>) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_73]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_73]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_73]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 27 more