繁体   English   中英

如何将 Spring 配置文件设置为 package?

[英]How to set a Spring profile to a package?

我想将配置文件名称设置为整个 package,但我不知道如何设置。 如果哪里没有简单的方法,那么我必须用@Profile注释标记 package 和子包中的每个 class。

<context:component-scan/>标签不支持profile之类的属性,所以我不知道。

您可以设置以下内容:

  • Spring Beans XML文件 - 用于xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
       profile="your-profile">

    <context:component-scan base-package="your.package" />
</beans>
  • Java配置的@Configuration
@Configuration
@Profile("your-profile")
@Componentscan("your.package")
class AppConfig {
}

在每个组件中,您都可以对特定包使用组件扫描。

如果你不混合使用XML和Java配置,你可以在一个带有@ComponentScan注释的bean上使用@Profile和你的目标包吗?

与XML类似:您可以有两个不同的<beans ...>部分,每个部分都有不同的配置文件,并且在每个部分中您都定义了自己的<context:component-scan basePackage="..." />

@Configuration
@Profile("profile1")
@ComponentScan(basePackage="package1")
class Config1 {
}

@Configuration
@Profile("profile2")
@ComponentScan(basePackage="package2")
class Config2 {
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM