一、作用:只有当在application.properties中配置了
spring.profiles.active=xxx
其中xxx为@Profile注解中的值
@Profile("xxx")
时,与@Profile注解一起使用的@Component、@Configuration注解才起作用
二、示例
1.配置类:
package cn.edu.tju.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.client.RestTemplate;
@Configuration
@Profile("prod")
public class MyConfig4 {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2、application.properties配置:
spring.profiles.active=prod
这种情况下,spring 容器中才会有RestTemplate类型的bean