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. NPOI 计算单元格高度

    需求 要导出一个Excel,第一行是不定长字符串,合并单元格(A-G)已知,现要计算第一行的高度. 思路 已知,NPOI的宽度单位是1/256个英文字符,在本例中,A列的宽度是2048,即 2048 ...

  2. shell学习五十七天----linux任务管理,针对上一讲的总结和扩展

    linux任务管理 在linux下有两类任务管理,各自是一次性和周期性.一次性是at和batch,周期性又分为系统不论什么和用户任务. 一次性任务: 1.命令格式:at [选项] time 2.选项: ...

  3. MYSQL 随机选取几条数据

    SELECT * FROM tablename AS r1 JOIN (SELECT ROUND(RAND() *(SELECT MAX(id)FROM tablename)) AS id) AS r ...

  4. 使用javac,手动编译一个java文件的方法

    参考<Tomcat与Java Web开发技术详解>中的命令: javac -classpath c:\tomcat\lib\servlet-api.jar                  ...

  5. 【Mac系统】之Mysql数据库遇到修改数字密码的问题(SQL语法错误:ERROR 1064 (42000),密码策略等问题:ERROR 1819 (HY000))

    安装完Mysql也进行了第一次初始化密码以及修改密码规则(请参考文章),但是我想后续再改密码,出现了下面几个问题: #SQL语句错误问题 ERROR 1064 (42000): You have an ...

  6. 【转】【FTP】之windows8.1上搭建FTP服务器方法

    参考地址:<windows8.1上搭建FTP服务器方法>

  7. 小图拼接大图MATLAB实现

    小图拼接大图MATLAB实现 1.实现效果图 原图 效果图 2.代码 files = dir(fullfile('D:\document\GitHub\homework\digital image p ...

  8. python 求下个月的最后一天

    [1]根据当前月求上个月.下个月的最后一天 (1)求当前月最后一天 (2)求前一个月的最后一天 (3)求下一个月的最后一天 学习示例与应用实例,代码如下: #!/usr/bin/python3 #-* ...

  9. 【转】利用Python中的mock库对Python代码进行模拟测试

    出处 https://www.toptal.com/python/an-introduction-to-mocking-in-python http://www.oschina.net/transla ...

  10. unity批量修改AssetBundleName与Variant

    批量修改指定路径下的资源的AssetBundleName与Variant. 脚本代码如下: using System.Collections; using System.Collections.Gen ...