Spring知识点回顾(06)Profile 和 条件注解 @Conditional

1、设定环境中的active profiles

如:DispatcherServerlet的init-param

spring.profiles.active=production

spring.profiles.active=dev

如:

public class WebInit implements WebApplicationInitializer

{

  @Override

public void onStartup(ServletContext sc)

{

sc.setInitParameter("spring.profiles.active","dev");

}

}

2、开发中使用@profile注解类或方法,达到在不同情况下,实例化不同的bean

package com.tianhe.springconfig.annoconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.tianhe.springconfig.annoconfig.model.XxxService;
import com.tianhe.springconfig.annoconfig.model.YyyService;

@Configuration
public class SpringConfig {

@Bean

@Profile("Dev")
public XxxService xxxService() {
return new XxxService();
}

@Profile("Production")
public YyyService yyyService() {
return new YyyService();
}

}

上一篇:Python学习笔记开篇


下一篇:[Spring Unit Testing] Spring Unit Testing with a Java Context