(First time posting a question on this forum, so please ignore any silly stuff) Working on fresh Spring Boot oriented project which has multiple configuration classes (Security, DataSourceConfig, DataConfig). The issue is DataConfig is using some bean defined in DataSourceConfig, but since the DataConfig is loading first, it is not getting that dependency.
@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");
}
}
So when I am starting the TOMCAT server (which is not the embedded one) in DEBUG mode having breakpoints on DataConfig.databaseConfiguration() and DataSourceConfig.xyzDataSource(), it always goes to former one instead of later, which is causing issues and giving "NullPointerException" for not getting datasource.
I don't know why '@Import' is not working for this. Any help would be really appriciated. Please let me know if you need any other info from my end.
Exception Trace:
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