spring boot: 一般注入说明(四) Profile配置,Environment环境配置 @Profile注解
1.通过设定Environment的ActiveProfile来设置当前context所需要的环境配置,在开发中使用@Profile注解类或方法,达到不同情况下选择实例化不同的Bean.
2.使用jvm的spring.profiles.acitve的参数来配置环境
3.web项目设置在Servlet的context pramater
servlet2.5一下的配置
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring-profiles.active<param-name>
<param-value>production</param-value>
</init-param> </servlet>
servlet3.0以上的配置
public class WebInit implements WebApplicationInitializer{ @Override
public void onStartup(ServletContext container) throws ServletException{
container.setInitParameter("spring.profiles.default", "dev");
}
}
我没有配置
实例:
DemoBean文件:
package ch2.profile; public class DemoBean { private String content; public DemoBean(String content)
{
this.content = content;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} }
profileConfig文件
package ch2.profile;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Configuration; //声明本类是一个配置类
@Configuration
public class ProfileConfig { //这个一个Bean对象
@Bean
//profile为dev时实例化devDemoBean
@Profile("dev")
public DemoBean devDemoBean()
{
return new DemoBean("from development profile ");
} //这是一个Bean对象
@Bean
//profile为pro时实例化prDemoBean
@Profile("prod")
public DemoBean proDemoBean()
{
return new DemoBean("from production profile");
} }
Main文件:
package ch2.profile;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String args[])
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//先将活动的profile注册为prod
context.getEnvironment().setActiveProfiles("prod");
//后注册Bean配置类,不然会报Bean未定义的错误
context.register(ProfileConfig.class);
//刷新容器
context.refresh(); //注册Bean
DemoBean demoBean = context.getBean(DemoBean.class);
//打印
System.out.println(demoBean.getContent()); context.close(); } }
运行结果:
from production profile
spring boot: 一般注入说明(四) Profile配置,Environment环境配置 @Profile注解的更多相关文章
- Spring Boot动态注入删除bean
Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...
- Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...
- Spring Boot 2.X(十四):日志功能 Logback
Logback 简介 Logback 是由 SLF4J 作者开发的新一代日志框架,用于替代 log4j. 主要特点是效率更高,架构设计够通用,适用于不同的环境. Logback 分为三个模块:logb ...
- Spring Boot (二)集成Jsp与生产环境部署
一.简介 提起Java不得不说的一个开发场景就是Web开发,也是Java最热门的开发场景之一,说到Web开发绕不开的一个技术就是JSP,因为目前市面上仍有很多的公司在使用JSP,所以本文就来介绍一下S ...
- 4.Profile(多环境配置)
在实际的项目开发中,一个项目通常会存在多个环境,例如,开发环境.测试环境和生产环境等.不同环境的配置也不尽相同,例如开发环境使用的是开发数据库,测试环境使用的是测试数据库,而生产环境使用的是线上的正式 ...
- maven 利用 profile 进行多环境配置
我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml.maven 的 profile 等.这里介绍的就是如何利用 profi ...
- 使用maven的profile构建不同环境配置
基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...
- spring boot:基于profile的多环境配置(spring boot 2.3.4)
一,为什么要进行多环境配置? 1,没有人会在生产环境中进行开发和测试, 所以通常会有多个环境的划分: 工程师本地的开发环境 进行测试的测试环境 最终上线的生产环境 每个环境对应不同的数据库/缓存等数据 ...
随机推荐
- centos7 ACL
Linux文件权限与属性详解 之 ACL Linux文件权限与属性详解 之 一般权限Linux文件权限与属性详解 之 ACLLinux文件权限与属性详解 之 SUID.SGID & SBI ...
- <Netty>(入门篇)TIP黏包/拆包问题原因及换行的初步解决之道
熟悉TCP编程的读者可能都知道,无论是服务端还是客户端,当我们读取或者发送消息的时候,都需要考虑TCP底层的粘包/拆包机制.木章开始我们先简单介绍TCP粘包/拆包的基础知识,然后模拟一个没有考虑TCP ...
- jsonp 小结
JSONP是JSON with Padding的略称. 它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实 ...
- windows db2 添加用户权限
http://www.csharpwin.com/csharpspace/12086r9069.shtml 在windows上DB2数据库安装的时候会创建一个系统管理员 的账户,默认为DB2ADMIN ...
- eclipse 连接 hadoop集群
1 网上找插件 或者 自己编译 放到eclipse plugin里面 2 重启eclipse 配置MapReduceLocation 通吃端口为9001 9000 看你自己的配置 3 新建mapRed ...
- ASP.NET动态网站制作(16)-- SQL数据库(2)
前言:SQL数据库的第二节课,继续讲解基本的语句及用法. 内容: 1.insert插入语句 insert into Book(bookName,bookPrice,bookAuthor) value ...
- 微信小程序之如何注册微信小程序
所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/546 ...
- MongoDBTemplate多条件查询的问题
问题: 在使用Spring Data MongoDB 进行条件查询数据时,发现条件判断不起作用,结果会返回所有的数据. Criteria criteria = new Criteria(); crit ...
- 已备份数据库的磁盘结构版本号为611,server支持版本号为539,无法还原或升级数据库
提供的是bak文件是2005备份的,还原到本地的sqlserver2000,提示:已备份数据库的磁盘上结构版本号为611.服务器支持版本号539,无法还原或升级数据库. 网上找了下,原因是611是sq ...
- bzoj1185【HNOI2007】最小矩形覆盖
1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec Memory Limit: 162 MBSec Special Judge Submit: 1114 Solv ...