Spring_总结_04_高级配置(一)之Profile

简介: 一、前言 本文承接上一节:Spring_总结_03_装配Bean(四)之导入与混合配置 这一节,来总结一下profile。 我们在开发软件时,通常会进行跨环境部署。而在跨环境部署时,经常会遇到某些环境的配置并不适用于另一环境,导致应用无法正常运行。

一、前言

本文承接上一节:Spring_总结_03_装配Bean(四)之导入与混合配置

这一节,来总结一下profile。

我们在开发软件时,通常会进行跨环境部署。而在跨环境部署时,经常会遇到某些环境的配置并不适用于另一环境,导致应用无法正常运行。

而profile恰好能解决这个问题。

 

二、概述

Profile 为不同环境下使用不同的配置提供了支持(开发环境下的配置和生成环境下的配置肯定不同,如数据库、加密算法等配置)

 

三、配置 Profile bean

要使用profile,首先要将所有不同的bean定义整理到一个或者多个profile中,在将应用部署到每个环境时,要确保对应的profile处于激活状态。

1.Java配置

使用@Profile注解来指定bean所属的环境

@Configuration
public class DataSourceConfig {

    /**
     * 1.开发环境使用嵌入式数据源
     * @return
     */
    @Bean
    @Profile("dev")
    public DataSource embeddedDataSource(){
        return (DataSource) new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:schema.sql")
                .addScript("classpath:test-data.sql")
                .build();
    }


    /**
     * 2.生产环境使用JNDI数据源
     * @return
     */
    @Bean
    @Profile("prod")
    public DataSource jndiDataSource() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName("jdbc/myDS");
        jndiObjectFactoryBean.setResourceRef(true);
        jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
        return (DataSource) jndiObjectFactoryBean.getObject();
    }
}
View Code

如果方法上加了@Profile注解,则对应环境激活时,配置bean会被创建。否则,配置bean会被忽略掉

如代码中配置所示,当dev环境被激活时, embeddedDataSource 会被创建。而 jndiDataSource 会被忽视掉。

 

2.Xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/plugin"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/plugin http://www.springframework.org/schema/plugin/spring-plugin.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <beans profile="dev">
        <jdbc:embedded-database id="dataSource">
            <jdbc:script location="classpath:schema.sql"/>
            <jdbc:script location="classpath:test-data.sql"/>
        </jdbc:embedded-database>
    </beans>

    <beans profile="prod">
        <jee:jndi-lookup id="dataSource"
                         jndi-name="jdbc/myDatabase"
                         resource-ref="true"
                         proxy-interface="javax.sql.DataSource"/>
    </beans>
</beans>
View Code

这里配置了2个bean,但在实际运行中,只会创建一个bean,也就是相应环境的bean。

 

四、激活 Profile

1.激活策略

Spring在确定哪个profile 处于激活状态时,需要依赖两个独立的属性:

        spring.profiles.active  和 spring.profiles.default 

(1)如果设置了spring.profiles.active 属性,则它的值就会用来确定哪个profile是激活的

(2)如果没有设置spring.profiles.active属性,则Spring将会查找spring.profiles.default的值,用以确定激活的环境

(3)如果这两个属性均没有设置,则没有激活的profile,因此只会创建哪些没有定义在profile中的bean

 

 

有多种方式来设置这两个属性:

(1)作为Web应用的上下文参数

(2)作为DispatcherServlet的初始化参数

(3)作为JNDI条目

(4)作为环境变量

(5)作为JVM的系统属性

(6)在集成测试类上,使用@ActiveProfiles注解设置

 

2.web应用的配置

Servlet2.5以下:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <!--设置Spirng配置文件所在位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml,classpath:/spring/applicationContext-*.xml</param-value>
    </context-param>

    <!--1.为上下文设置的默认的profile-->
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>dev</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
         <servlet-name>appServlet</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <!--2.为DispatcherServlet设置默认的profile-->
         <init-param>
            <param-name>spring.profiles.default</param-name>
            <param-value>dev</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
</web-app>
View Code

 

Servlet3.0及以上

public class WebInit implements WebApplicationInitializer {
    
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("spring.profiles.default", "dev");
    }
}
View Code

 

开发时,按照上述配置,所有的开发人员从版本控制软件中拉取代码之后,就能使用开发环境的配置运行代码,而不需要任何额外的配置。

当应用程序部署到QA、生成、或者其他环境之中时,可根据情况使用系统属性、环境变量或者JNDI设置spring.profiles.active即可。

 

3.使用profile进行测试

在集成测试时,通过使用@AcitveProfiles注解,可以指定运行测试时激活哪个profile

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes ={PersistenceTestConfig.class})
@ActiveProfiles("dev")
public class PersistenceTest {
    
}
View Code

 

 

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=8rf7kovqfyoo

目录
相关文章
|
7天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
14 0
|
1月前
|
Java 测试技术 数据库
SpringBoot:@Profile注解和Spring EL
SpringBoot:@Profile注解和Spring EL
|
29天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
1月前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
|
1天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
13 0
|
1天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
4 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
7天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
21 3
|
7天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
14 1
|
7天前
|
XML Java 数据格式
进阶注解探秘:深入Spring高级注解的精髓与实际运用
进阶注解探秘:深入Spring高级注解的精髓与实际运用
25 2
|
7天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
17 0