前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring_总结_04_高级配置(一)之Profile

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

作者头像
shirayner
发布2018-08-10 10:06:15
2220
发布2018-08-10 10:06:15
举报
文章被收录于专栏:Java成神之路Java成神之路

一、前言

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

这一节,来总结一下profile。

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

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

二、概述

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

三、配置 Profile bean

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

1.Java配置

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

代码语言:javascript
复制
@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();
    }
}

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

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

2.Xml配置

代码语言:javascript
复制
<?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>

这里配置了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

代码语言:javascript
复制
<?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>

Servlet3.0及以上

代码语言:javascript
复制
public class WebInit implements WebApplicationInitializer {
    
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("spring.profiles.default", "dev");
    }
}

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

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

3.使用profile进行测试

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

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes ={PersistenceTestConfig.class})
@ActiveProfiles("dev")
public class PersistenceTest {
    
}

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、概述
  • 三、配置 Profile bean
    • 1.Java配置
      • 2.Xml配置
      • 四、激活 Profile
        • 1.激活策略
          • 2.web应用的配置
            • 3.使用profile进行测试
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档