Java 如何在启动时自动连接配置文件配置类?

Java 如何在启动时自动连接配置文件配置类?,java,spring,datasource,autowired,Java,Spring,Datasource,Autowired,我想创建不同的数据库配置文件类,每个类都用于开发、生产和测试 我在的帮助下尝试了以下操作,但无法正确连接。为什么? interface DataConfig { DataSource dataSource(); } @Configuration @Profile("dev") public class StandaloneDataConfig implements DataConfig { @Bean @Over

我想创建不同的数据库配置文件类,每个类都用于开发、生产和测试

我在的帮助下尝试了以下操作,但无法正确连接。为什么?

 interface DataConfig {
        DataSource dataSource();
    }

    @Configuration
    @Profile("dev")
    public class StandaloneDataConfig implements DataConfig {
        @Bean
        @Override
        public dataSource dataSource() {
            //return the ds
        }
    }

    @Configuration
    @Profile("prod")
    public class JndiDataConfig implements DataConfig { ... }

    @Configuration
    @PropertySource({"classpath:config.properties"})
    class AppConfig {
        @Autowired
        private DataConfig cfg;

    }

@Configuration
@ComponentScan
@Import(AppConfig.class)
@EnableTransactionManagement
public class SpringBootConfig extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}
config.properties:

spring.profiles.active=dev
结果:启动时出现异常

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DataConfig dataConfig; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DataConfig] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 34 common frames omitted
弹簧4.0.3.1释放


不过,我的设置似乎在一般情况下都能正常工作:如果我删除其中一个数据库上的@Profile注释,一切都会正常连接。

我还没有测试您的配置,但我的最佳猜测是(在我做了一些调查之后)自动连线发生时,
@PropertySources
不可用于带注释的
@Conditional
配置类

根据@Profile是一种带有实现的
@Conditional
风格的注释。
为了在需要时提供
@PropertySource
,我认为您需要自己的自定义
@Conditional
实现,就像我在上面提到的SO帖子中一样,如果您定义的不是
条件,而是
配置条件
,请确保在
配置阶段使用。注册BEAN
阶段。

您需要将
配置.properties
文件重命名为
应用程序.properties
,以便Spring Boot自动启动。

我没有任何BEAN.xml由于只使用基于注释的配置。您使用的是Spring Boot吗?如果是,请显示主配置类(我猜是AppConfig)?是的,我使用的是Spring Boot,上面已更新。一般来说,我的设置是正常的,因为如果我删除其中一个数据库上的
@Profile
注释,所有内容都会正确连接。如果将属性文件重命名为
应用程序。属性
,会发生什么情况?哦,太好了!!我创建了一个
application.properties
文件,并将
spring.profiles.active=dev
放在那里,而不是放在我的自定义配置文件中。作品(你可以考虑把这个作为答案,这样我就可以接受它)现在,最后一个问题很有趣:我如何告诉Spring寻找类路径之外的应用程序?因此,在生产过程中,我可以在某个地方创建一个本地
应用程序.properties
文件,该文件总是使用
spring.profiles.active=prod
?这是一个很好的问题。有关详细信息,请参阅第21节。SpringBoot提供了许多默认设置供您选择