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,没有人会在生产环境中进行开发和测试, 所以通常会有多个环境的划分: 工程师本地的开发环境 进行测试的测试环境 最终上线的生产环境 每个环境对应不同的数据库/缓存等数据 ...
随机推荐
- jsonp 小结
JSONP是JSON with Padding的略称. 它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实 ...
- .net 字符串驻留
.net中的string表达的是常量字符串. JIT编译器编译时判断遇到的常量字符串是否在内部散列表中,如果不在,添加进去.当第一次执行到含字符串的方法时,CLR会检查该字符串是否在内部的一个散列表中 ...
- 多媒体开发之---H264 RTSP交互过程
OPTIONS rtsp://192.168.1.154:8557/h264 RTSP/1.0 CSeq: 1 User-Agent: VLC media player (LIVE555 Stream ...
- STM32 Option Bytes位 重置为出厂设置
STM32 Option Bytes位 重置为出厂设置 JLINK 按照说明,在IAR安装目录下找到指定的运行程序JLinkSTM32.exe(D:\Program Files (x86)\IAR S ...
- C语言基础知识【环境设置】
直接使用绿色版的VC++6.0就ok,后期我会写一个具体的使用教程
- ShareSDK 微博空间分享
本文转载至 http://blog.csdn.net/learnios/article/details/8992346 ShareSDK微博分享空间分享新浪微博腾讯微博 第一步:首先导入ShareSD ...
- python 基础 9.12 索引
#/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/24 4:48 #@Auther :liuzhenchuan #@File :索引 ...
- [CMD]重启电脑
https://zhidao.baidu.com/question/686086701903450132.html bat是批处理,可以调用关机命令关机. 制作方法如下: 打开记事本程序: 输入如下内 ...
- 网站存储session的方案
1: ASP.NET State Service是什么 用来管理 Session 的,正常来说,Session 位于IIS进程中(其实可以理解成在服务器的内存中),当IIS重启或程序池回收会自动清空S ...
- apache .htaccess实现301重定向
<IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On #301定向 RewriteCond %{HTTP_HO ...