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注解的更多相关文章

  1. Spring Boot动态注入删除bean

    Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...

  2. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  3. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  4. Spring Boot 2.X(十四):日志功能 Logback

    Logback 简介 Logback 是由 SLF4J 作者开发的新一代日志框架,用于替代 log4j. 主要特点是效率更高,架构设计够通用,适用于不同的环境. Logback 分为三个模块:logb ...

  5. Spring Boot (二)集成Jsp与生产环境部署

    一.简介 提起Java不得不说的一个开发场景就是Web开发,也是Java最热门的开发场景之一,说到Web开发绕不开的一个技术就是JSP,因为目前市面上仍有很多的公司在使用JSP,所以本文就来介绍一下S ...

  6. 4.Profile(多环境配置)

    在实际的项目开发中,一个项目通常会存在多个环境,例如,开发环境.测试环境和生产环境等.不同环境的配置也不尽相同,例如开发环境使用的是开发数据库,测试环境使用的是测试数据库,而生产环境使用的是线上的正式 ...

  7. maven 利用 profile 进行多环境配置

    我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml.maven 的 profile 等.这里介绍的就是如何利用 profi ...

  8. 使用maven的profile构建不同环境配置

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  9. spring boot:基于profile的多环境配置(spring boot 2.3.4)

    一,为什么要进行多环境配置? 1,没有人会在生产环境中进行开发和测试, 所以通常会有多个环境的划分: 工程师本地的开发环境 进行测试的测试环境 最终上线的生产环境 每个环境对应不同的数据库/缓存等数据 ...

随机推荐

  1. iOS SDWebImage加载大图导致内存崩溃解决方案

    static BOOL SDImageCacheOldShouldDecompressImages = YES; static BOOL SDImagedownloderOldShouldDecomp ...

  2. busybox下inittab中runlevel解析

    Order of scripts run in /etc/rc?.d ================================== 0. Overview. All scripts execu ...

  3. PHP性能之语言性能优化说明

    PHP语言性能优化优化啥? 如下图所示,PHP直接执行的是opcode,所以我们尽量减少扫描和转码解析. 这是我们第一个优化点,尽量使用PHP内置的函数代替我们的代码来实现同样的功能. 和我们自己写的 ...

  4. CentOS 源码安装svn

    一. 下载依赖包 1. apr源码包 http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.gz 2. apr-util源码包 h ...

  5. html5小趣味知识点系列(一)pubdate

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. android 自定义 listView

    目录: 1.主布局 ListView <?xml version="1.0" encoding="utf-8"?><RelativeLayou ...

  7. Android 适配(一)

    一.Android适配基础参数 1.常见分辨率(px)      oppx 2340x1080      oppR15 2280x1080      oppor11sp 2160*1080       ...

  8. web.xml配置中的log4jRefreshInterval

    采用spring框架的项目如何使用log4j在spring中使用log4j,有些方便的地方, 1.动态的改变记录级别和策略,即修改log4j.properties,不需要重启web应用,这需要在web ...

  9. React-Native在gitHub下载的Demo不能运行问题!!!

    1.目前找到的最可行的运行React-Native Demo的解决方案 请参考:http://blog.csdn.net/shubinniu/article/details/52873250 2.检查 ...

  10. JITWatch工具

    JITWatch,执行程序时监测Java HotSpot JIT编译器如何运作的工具,有助于做JVM的性能优化 wiki: https://github.com/AdoptOpenJDK/jitwat ...