简体   繁体   English

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

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

I want to set a profile name to a whole package and I don't know how.我想将配置文件名称设置为整个 package,但我不知道如何设置。 If where is no easy way then I have to mark every class in the package and sub packages with @Profile annotation.如果哪里没有简单的方法,那么我必须用@Profile注释标记 package 和子包中的每个 class。

<context:component-scan/> tag does not support attribute like profile so I have no idea. <context:component-scan/>标签不支持profile之类的属性,所以我不知道。

You can set profile for: 您可以设置以下内容:

  • Spring Beans XML file - for xml configuration 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>
  • @Configuration class for Java config Java配置的@Configuration
@Configuration
@Profile("your-profile")
@Componentscan("your.package")
class AppConfig {
}

In each of them you can use component scanning for your a particular package. 在每个组件中,您都可以对特定包使用组件扫描。

If you don't mix XML and Java config you could use @Profile on a bean which has @ComponentScan annotation with your target package maybe? 如果你不混合使用XML和Java配置,你可以在一个带有@ComponentScan注释的bean上使用@Profile和你的目标包吗?

Similarly with XML: you can have two different <beans ...> sections, each with different profile and in each of the section you define your own <context:component-scan basePackage="..." /> 与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