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,没有人会在生产环境中进行开发和测试, 所以通常会有多个环境的划分: 工程师本地的开发环境 进行测试的测试环境 最终上线的生产环境 每个环境对应不同的数据库/缓存等数据 ...
随机推荐
- CentOS6.9 下编译安装MySQL5.7.19
官网:https://www.mysql.com/ 下载地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.19.tar.gz 一.准备工作 ...
- Currency Exchange - poj 1860
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22111 Accepted: 7986 Description Seve ...
- maven 工程聚合插件
<!-- war包生成插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <a ...
- div+css 画三角形
<style type="text/css"> .rightdirection { width:0;height:0; line-height:0; b ...
- Selenium 应用 WebDriverWait 和 expected_conditions(待验证)
收藏在我的收藏看不到,只能copy了,转载至http://www.cnblogs.com/yicaifeitian/p/4749149.html 哈哈,我始终相信贴出来总会有人看.WebDriverW ...
- 嵌入式开发之手机arm汇总---科普手机arm
http://www.leiphone.com/news/201406/1102-zzl-arm.html
- 登录shell与非登录shell读取文件过程
登录shell与非登录shell读取文件过程登录:/etc/profile→/etc/profile.d/*.sh ~/.bash_profile非登录:~/.bash_profile→ ...
- 关于移动端border 1像素在不同分辨率下边显示粗细不一样的处理
最近开发发现一个很有趣的问题 就是我如果给一个元素加上一个像素的 border 在不同的分辨率的情况下显示的不同 在高清屏幕(尤其是ios 喽 不鄙视国产) 据说在6plus下会变成3px 这个我 ...
- ios -仿微信有多级网页时,显示返回跟关闭按钮
@property (nonatomic, copy) NSString * url; @interface WebViewController ()<UIWebViewDelegate,UIG ...
- 验证-- email类型输入框(电子邮件地址)--multiple
如果需要一个用来填写电子邮件地址的输入框,可以使用email类型.这样浏览器可以帮我们验证格式是否正确,而不需要自己写验证规则.原文:HTML5新控件 - email类型输入框(电子邮件地址) 1,只 ...