@Profile根据不同环境注入bean
# 介绍
@Profile元注解是在不同的生产环境中,@Bean创建的SpringBean根据spring.profiles.active指定的环境不同创建不同环境的bean对象
# 一.@Profile元注解需要配合spring.profiles.active一起使用
# 二.首先在resources下创建三个配置文件
![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy0wMzZmMTM3ODlmOGQ0ZWJkLnBuZw?x-oss-process=image/format,png)
application.properties:
```
spring.profiles.active=prod
```
其他两个配置文件创建了就行!
# 三.创建需要注入的实体类
```
package com.wzq.dome.entity;
import lombok.Data;
/**
* @description:
* @author: Wzq
* @create: 2019-12-12 15:00
*/
@Data
public class ProFileEntity {
private String name;
}
```
# 四.根据spring.profiles.active指定不同的环境选择注入的bean
DomeApplication:代码如下
```
package com.wzq;
import com.wzq.dome.entity.ProFileEntity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
@SpringBootApplication
public class DomeApplication {
public static void main(String[] args) {
SpringApplication.run(DomeApplication.class, args);
}
@Bean(name = "proFileEntity")
@Profile("prod")
public ProFileEntity proFileEntity1(){
ProFileEntity proFileEntity = new ProFileEntity();
proFileEntity.setName("GoslingWu");
return proFileEntity;
}
@Bean(name = "proFileEntity")
@Profile("dev")
public ProFileEntity proFileEntity(){
ProFileEntity proFileEntity = new ProFileEntity();
proFileEntity.setName("wzq");
return proFileEntity;
}
}
```
# 五.使用
```
package com.wzq.dome.action;
import com.wzq.dome.entity.ProFileEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description:
* @author: Wzq
* @create: 2019-12-12 10:27
*/
@RestController
public class TestController {
@Autowired
ProFileEntity proFileEntity;
@RequestMapping("/test")
public String test(){
return proFileEntity.getName();
}
}
```
# 六.成功
![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy05Y2UyMjAzMmQ1NzI5MDMxLnBuZw?x-oss-process=image/format,png)
这是我的公众号 有最新的it咨询,和个人工作的记录:
![扫码_搜索联合传播样式-微信标准绿版.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy05YzcxMDlkZjgzMmJlOTc2LnBuZw?x-oss-process=image/format,png)
这是我的个人微信遇到问题欢迎,提问:
![微信图片_20190614200326.jpg](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy03NjJmYTFlOGFlOWZjMjk3LmpwZw?x-oss-process=image/format,png)
我的星球欢迎加入:
![微信图片_20191210093234.jpg](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1mYTlmMWExMjkwYTExMjhkLmpwZw?x-oss-process=image/format,png)
最后加上高质量的淘宝店:如有质量问题随时滴滴我,童叟无欺!
![微信图片_20191111194455.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1iZjlkMjQzYjg5OWIyYTA1LnBuZw?x-oss-process=image/format,png)
以上是 @Profile根据不同环境注入bean 的全部内容, 来源链接: utcz.com/z/511701.html