@Profile

可以用在@Component类(包括@Configuration类)或@Bean的方法上

自定义@Profile

下面的代码与@Profile("production")的作用是一样的

@Target({ElementType.TYPE, ElementType.METHOD}
@Retention(RetentionPolicy.RUNTIME)
@Profile("production")
public @interface Production{
}

如果一个@Configuration类被标记为@Profile,那么所有的@Bean方法和@Import相关联的类将会避开除非一个或多个指定的profile被激活。如果一个@Component和@Configuration类被标记为@Profile({"p1", "p2"}),这个类将不会被处理和注册除非'p1'或'p2'被激活。

如果指定@Profile({"p1", "!p2"}), 如果'p1'被激活者'p2'没有被激活

@Profile也可以被声明在方法级别上

@Configuration
public class AppConfig{

    @Bean("student")
    @Profile("dev")
    public Student dev(){
        Student stu = new Student();

        stu.setName("dev");

        return stu;
    }

    @Bean("student")
    @Profile("prod")
    public Student prod(){
        Student stu = new Student();

        stu.setName("prod")

        return stu;
    }
}

重载方法应该指定一指的profile,如果不一致那么第一个声明的profile将会匹配。

解决方法:向上面那样指定不一样的方法名,指定同样的@bean名字,向上面的例子那样

XML bean定义profile

spring-prod.xml

<beans profile="prod">
    <bean id="stu" class="com.jianglei2.bean.Student"
        p:name="jianglei_prod" p:lovel="dengyi_prod" p:age="18"/>
</beans>

spring-dev.xml

<beans profile="dev">
    <bean id="stu" class="com.jianglei2.bean.Student"
        p:name="jianglei" p:love="dengyi" p:age="18"/>
</beans>

Test类

@RunWith(SpringJunit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/jianglei2/spring-prod.xml", "classpath:spring/jianglei2/spring-dev.xml"})
@ActiveProfiles("dev")
public class ConfigMulitTest{

    @Autowired
    public Student student;
}

或者在一个类里面

spring-dev-prod-combine.xml

<beans>
    <beans profile="prod">
        <bean id="stu" class="com.jianglei2.bean.Student"
              p:name="jianglei_prod" p:age="18" p:love="dengyi_prod"/>
    </beans>

    <beans profile="dev">
        <bean id="stu" class="com.jianglei2.bean.Student"
              p:name="jianglei_dev" p:age="18" p:love="jianglei_dev"/>
    </beans>

</beans>

Test类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/jianglei2/spring-dev-prod-combine.xml")
@ActiveProfiles("dev")
public class ConfigCombineTest {

    @Autowired
    public Student stu;

}

Activating a profile

@Configruation配置类

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.regiester(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh(); //这一步刷新很重要

xml配置

FileSystemXmlApplicationContext ctx = new FileSystemXmlApplication("classpath:something.xml")

ctx.getEnvironment().setActiveProfiles("development");
ctx.refresh(); //一定要刷新

另外,profile可以通过spring.profiles.active再多处声明

system environment variables

JVM system properties

servlet context paramters in web.xml

甚至在entry in JNDI

在集成测试,激活可以通过spring-test模块的@ActiveProfiles

注意:profiles不是或者的关系,其可以一次激活多个profiles。

通过编程的方式

ctx.getEnviroment().setActiveProfiles("profile1", "profile2");

声明的方式,spring.profiles.active可以接受多个由逗号分割的profiles名字

-Dspring.profiles.active="profile1, profile2"

results matching ""

    No results matching ""