Springboot学习笔记(九)——Profiles
1.为什么要使用Profiles
在开发中,一般有两种环境
1,生产环境 [项目上线,客户在使用中,就是生产环境]
2,开发环境[就是开发环境,不解释]
有时候开发环境和生产环境的配置方法是不一样的,那么如何快速的切换呢,这里就要使用profiles文件。
2.使用@Profile注解来实现
在service包下创建一个TestService类来测试Profile;
代码如下:
public class TestService { private String password; TestService(String password) { this.password = password; } public String testProfiles() { return password; } }
在AppConfig类中添加如下代码:
@Configuration public class AppConfig { @Bean @Profile("dev") public TestService testServiceDev() { return new TestService("devpassword"); } @Bean @Profile("prob") public TestService testServiceProb() { return new TestService("probpassword"); } }
在controller类中添加一个映射方法。代码如下:
@RestController public class UserController { @Autowired private UserService userService; @Autowired private TestService testService; @RequestMapping("/") String home() { return userService.test(); } @RequestMapping("/profiles") String testProfiles() { return testService.testProfiles(); } }
在application.properties文件中添加下列属性
spring.profiles.active=prob
在浏览器中访问就会发现用的是@Profile("prob")这个bean
3.使用不同的xml方式来实现
分类:
Spring学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗