Java Spring启动-多个配置类-导入不工作

Java Spring启动-多个配置类-导入不工作,java,spring-boot,Java,Spring Boot,(首次在此论坛上发布问题,请忽略任何愚蠢的东西) 正在处理面向fresh Spring引导的项目,该项目具有多个配置类(安全、DataSourceConfig、DataConfig)。问题是DataConfig正在使用DataSourceConfig中定义的某个bean,但是由于DataConfig是先加载的,所以它没有获得该依赖关系 @Configuration public class DataSourceConfig { @Bean(name="abcDataSource")

(首次在此论坛上发布问题,请忽略任何愚蠢的东西) 正在处理面向fresh Spring引导的项目,该项目具有多个配置类(安全、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");

    }
}
因此,当我在调试模式下启动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
原因:java.lang.NullPointerException
在org.apache.commons.configuration.DatabaseConfiguration.getConnection(DatabaseConfiguration.java:568)~[commons-configuration-1.6.jar:1.6]
在org.apache.commons.configuration.DatabaseConfiguration.getKeys(DatabaseConfiguration.java:515)~[commons-configuration-1.6.jar:1.6]
在org.apache.commons.configuration.ConfigurationConverter.getProperties(ConfigurationConverter.java:112)~[commons-configuration-1.6.jar:1.6]
在com.mcmcg.apollo.common.datasource.DataConfig.commonConfigurationFactoryBean(DataConfig.java:35)~[classes/:?]
在com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.CGLIB$commonConfigurationFactoryBean$1()~[classes/:?]
在com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776$$FastClassBySpringCGLIB$$40819bbc.invoke()~[classes/:?]
在org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.commonConfigurationFactoryBean()~[classes/:?]
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[?:1.8.0_73]
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)~[?:1.8.073]
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[?:1.8.073]
在java.lang.reflect.Method.invoke(Method.java:497)~[?:1.8.073]
在org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:162)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.ConstructorResolver.InstanceUsingFactoryMethod(ConstructorResolver.java:588)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1023)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:381)~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.commonConfigurationFactoryBean()~[classes/:?]
在com.mcmcg.apollo.common.datasource.DataConfig.propertyplaceholderconfig(DataConfig.java:29)~[classes/:?]
在com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.CGLIB$propertyPlaceholderConfigurer$0()~[classes/:?]
在com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776$$FastClassBySpringCGLIB$$40819bbc.invoke()~[classes/:?]
在org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
在com.mcmcg.apollo.common.datasource.DataConfig$$EnhancerBySpringCGLIB$$bb4c1776.propertyplaceholderconfigure()~[classes/:?]
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[?:1.8.0_73]
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)~[?:1.8.073]
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[?:1.8.073]
在java.lang.reflect.Method.invoke(Method.java:497)~[?:1.8.073]
位于org.springframework.beans.factory.sup