最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring入門實(shí)戰(zhàn)之Profile詳解

 更新時(shí)間:2017年02月12日 09:09:26   投稿:daisy  
什么是spring profile?簡單講profile就是一組配置,不同profile提供不同組合的配置,程序運(yùn)行時(shí)可以選擇使用哪些profile來適應(yīng)環(huán)境。下面這篇文章主要介紹了Spring中Profile實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考借鑒。

前言

Spring中的Profile功能其實(shí)早在Spring 3.1的版本就已經(jīng)出來,它可以理解為我們在Spring容器中所定義的Bean的邏輯組名稱,只有當(dāng)這些Profile被激活的時(shí)候,才會(huì)將Profile中所對應(yīng)的Bean注冊到Spring容器中。

看到Profile這個(gè)關(guān)鍵字,或許你從來沒有正眼瞧過他,又或者腦海中有些模糊的印象,比如除了這里Springmvc中的Profile,maven中也有Profile的標(biāo)簽。

從字面意思來看,Profile表示側(cè)面,那什么情況下才會(huì)用到側(cè)面這個(gè)功能呢,而側(cè)面具體又有什么含義呢

打一個(gè)比方,對于數(shù)據(jù)庫的配置問題,在開發(fā)的眼中可以使用嵌入的數(shù)據(jù)庫,并且加載測試數(shù)據(jù)(后面會(huì)給出代碼示例)。但是在測試的眼中,可能會(huì)配一個(gè)數(shù)據(jù)庫連接池類似這樣

@Bean(destroyMethod="close")
public DataSource dataSource () {
 BasicDataSource dataSource = new BasicDataSource();
 dataSource.setUrl("jdbc:h2:tcp://dbserver/~/test");
 dataSource.setDriverClassName("org.h2.Driver");
 dataSource.setUsername("sa");
 dataSource.setPassword("password");
 dataSource.setInitialSize(20);
 dataSource.setMaxActive(30);
 return dataSource; 
}

當(dāng)然還有產(chǎn)品環(huán)境下的配置等等。對于這種百花齊放的配置方式你還能說什么,默默的為這一套套的環(huán)境都部署相應(yīng)的配置文件啊,沒有profile這套我們一直都是這么做。

但是現(xiàn)在有了Profile,我們就多了一種選擇,一種更加智能省心的配置方式。通過Profile配置,Spring可以在根據(jù)環(huán)境在運(yùn)行階段來決定bean的創(chuàng)建與否,先舉例如下,主要從Profile bean的配置和激活來展開。

Profile bean的配置

通過注解@Profile配置

對于上面比方中的第一種情況,在開發(fā)環(huán)境中我們配置一個(gè)數(shù)據(jù)源可能是這樣的

@Bean(destroyMethod = "shutdown")
public DataSource embeddedDataSource() {
 return new EmbeddedDatabaseBuilder()
 .addScript("classpath:schema.sql")
 .addScript("classpath:test-data.sql")
 .build();
 }

這里會(huì)使用EmbeddedDatabaseBuilder創(chuàng)建一個(gè)嵌入式數(shù)據(jù)庫,模式定義在類文件下的schema.sql文件中

schema.sql

create table Things (
 id identity,
 name varchar(100)
);

這里定義了一張Things表包含了兩個(gè)字段

除了模式文件,還需要通過test-data.sql加載測試數(shù)據(jù)

test-data.sql

insert into Things (name) values ('A')

對于這個(gè)@Bean完全不知道是放在開發(fā)的環(huán)境下創(chuàng)建還是產(chǎn)品的環(huán)境下。所以我們這里可以使用注解@Profile幫助我們?yōu)檫@個(gè)bean打上標(biāo)識(shí)。

從Spring 3.1版本中就引入了bean profile的功能,可以讓你將不同的bean定義到一個(gè)或者多個(gè)profile里,然后在部署應(yīng)用時(shí)告知要激活那個(gè)profile,則相應(yīng)的bean就會(huì)被創(chuàng)建。

比如這里

@Configuration
@Profile("dev")
public class DevelopmentProfileConfig {

 @Bean(destroyMethod = "shutdown")
 public DataSource embeddedDataSource() {
 return new EmbeddedDatabaseBuilder()
 .setType(EmbeddedDatabaseType.H2)
 .addScript("classpath:schema.sql")
 .addScript("classpath:test-data.sql")
 .build();
 }
}

通過@Profile("dev")為EmbedderDataSource bean標(biāo)記為dev環(huán)境下要?jiǎng)?chuàng)建的bean。

注意:1. @Profile被加載類級別上,如果dev profile沒有被激活,那么類中對應(yīng)的所有bean就不會(huì)被創(chuàng)建

         2. 如果當(dāng)前是dev環(huán)境被激活了,那么對于沒有使用@Profile的bean都會(huì)被創(chuàng)建,被標(biāo)記為其他的profile如prod,則不會(huì)創(chuàng)建相應(yīng)的bean

         3. 從3.2開始@Profile不僅僅可以加載類級別上,還可以加載方法上,具體代碼如下

package com.myapp;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jndi.JndiObjectFactoryBean;

@Configuration
public class DataSourceConfig {
 
 @Bean(destroyMethod = "shutdown")
 @Profile("dev")
 public DataSource embeddedDataSource() {
 return new EmbeddedDatabaseBuilder()
 .setType(EmbeddedDatabaseType.H2)
 .addScript("classpath:schema.sql")
 .addScript("classpath:test-data.sql")
 .build();
 }

 @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();
 }

}

通過xml配置文件配置

除了簡單的注解方式,我們哈可以通過在xml配置文件中聲明的方式,具體配置如下

datasource-config.xml

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

 <beans profile="dev">
 <jdbc:embedded-database type="H2">
 <jdbc:script location="classpath:schema.sql" />
 <jdbc:script location="classpath:test-data.sql" />
 </jdbc:embedded-database>
 </beans>
 
 <beans profile="prod">
 <jee:jndi-lookup 
 lazy-init="true"
 jndi-name="jdbc/myDatabase"
 resource-ref="true"
 proxy-interface="javax.sql.DataSource" />
 </beans>
</beans>

這里分別聲明了兩種環(huán)境以及對應(yīng)的profile。

profile激活

雖然我們已經(jīng)配置好了profile,但是如何激活相應(yīng)的環(huán)境呢。這里我們需要兩個(gè)屬性spring.profile.active以及spring.profile.default

如果spring.profile.active被賦值了,則spring.profile.default就不會(huì)起作用,如果spring.profie.active沒有賦值,則使用默認(rèn)的spring.profile.default設(shè)置的值。當(dāng)然,如果兩者都沒有設(shè)置的話,則只會(huì)創(chuàng)建那些定義在相應(yīng)的profile中的bean。

設(shè)置這兩個(gè)屬性的方式有很多:

作為DispactcherServlet的初始化參數(shù)

作為Web應(yīng)用上下文參數(shù)

作為JNDI條目

作為環(huán)境變量

作為JVM的系統(tǒng)屬性

在集成測試類上,使用@ActiveProfiles注解設(shè)置

比如我們在web.xml中可以聲明代碼如下

<?xml version="1.0" encoding="UTF-8"?>
<web -app version="2.5"
...>

 //為上下文設(shè)置默認(rèn)的profile
 <context-param>
 <param-name>spring.profile.default</param-name>
 <param-value>dev</param-value>
 </context-param>

...

 <servlet>
 ...
 //為Serlvet設(shè)置默認(rèn)的profile
 <init-param>
  <param-name>spring-profiles.default</param-name>
  <param-value>dev</param-value>
 </init-prama>

...
<web-app>

這樣就可以指定需要啟動(dòng)那種環(huán)境,并準(zhǔn)備相應(yīng)的bean。

另外對于測試,spring為什么提供了一個(gè)簡單的注解可以使用@ActiveProfiles,它可以指定運(yùn)行測試的時(shí)候應(yīng)該要激活那個(gè)profile。比如這里的測試類DevDataSourceTest

package profiles;

import static org.junit.Assert.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.myapp.DataSourceConfig;

public class DataSourceConfigTest {

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes=DataSourceConfig.class)
 @ActiveProfiles("dev")
 public static class DevDataSourceTest {
 @Autowired
 private DataSource dataSource;
 
 @Test
 public void shouldBeEmbeddedDatasource() {
 assertNotNull(dataSource);
 JdbcTemplate jdbc = new JdbcTemplate(dataSource);
 List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {
 @Override
 public String mapRow(ResultSet rs, int rowNum) throws SQLException {
  return rs.getLong("id") + ":" + rs.getString("name");
 }
 });
 
 assertEquals(1, results.size());
 assertEquals("1:A", results.get(0));
 }
 }

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes=DataSourceConfig.class)
 @ActiveProfiles("prod")
 public static class ProductionDataSourceTest {
 @Autowired
 private DataSource dataSource;
 
 @Test
 public void shouldBeEmbeddedDatasource() {
 // should be null, because there isn't a datasource configured in JNDI
 assertNull(dataSource);
 }
 }
 
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("classpath:datasource-config.xml")
 @ActiveProfiles("dev")
 public static class DevDataSourceTest_XMLConfig {
 @Autowired
 private DataSource dataSource;
 
 @Test
 public void shouldBeEmbeddedDatasource() {
 assertNotNull(dataSource);
 JdbcTemplate jdbc = new JdbcTemplate(dataSource);
 List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {
 @Override
 public String mapRow(ResultSet rs, int rowNum) throws SQLException {
  return rs.getLong("id") + ":" + rs.getString("name");
 }
 });
 
 assertEquals(1, results.size());
 assertEquals("1:A", results.get(0));
 }
 }

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("classpath:datasource-config.xml")
 @ActiveProfiles("prod")
 public static class ProductionDataSourceTest_XMLConfig {
 @Autowired(required=false)
 private DataSource dataSource;
 
 @Test
 public void shouldBeEmbeddedDatasource() {
 // should be null, because there isn't a datasource configured in JNDI
 assertNull(dataSource);
 }
 }

}

運(yùn)行shouldBeEmbeddedDatasource方法,測試通過

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • 一文詳解Maven的setting文件

    一文詳解Maven的setting文件

    maven?是目前java?常見的一款包管理工具,通過?maven?我們可以很方便的對項(xiàng)目進(jìn)行編譯、打包、部署等操作,本文將詳細(xì)帶大家了解一下Maven?setting文件,需要的朋友可以參考下
    2024-02-02
  • Java接收text/event-stream格式數(shù)據(jù)的詳細(xì)代碼

    Java接收text/event-stream格式數(shù)據(jù)的詳細(xì)代碼

    這篇文章主要介紹了java接收text/event-stream格式數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • 一文帶你了解Java創(chuàng)建型設(shè)計(jì)模式之原型模式

    一文帶你了解Java創(chuàng)建型設(shè)計(jì)模式之原型模式

    原型模式其實(shí)就是從一個(gè)對象在創(chuàng)建另外一個(gè)可定制的對象,不需要知道任何創(chuàng)建的細(xì)節(jié)。本文就來通過示例為大家詳細(xì)聊聊原型模式,需要的可以參考一下
    2022-09-09
  • 解決Spring在Thread中注入Bean無效的問題

    解決Spring在Thread中注入Bean無效的問題

    這篇文章主要介紹了解決Spring在Thread中注入Bean無效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 利用java+mysql遞歸實(shí)現(xiàn)拼接樹形JSON列表的方法示例

    利用java+mysql遞歸實(shí)現(xiàn)拼接樹形JSON列表的方法示例

    這篇文章主要給大家介紹了關(guān)于利用java+mysql遞歸實(shí)現(xiàn)拼接樹形JSON列表的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起看看吧。
    2017-08-08
  • java-制表符\t的使用說明

    java-制表符\t的使用說明

    這篇文章主要介紹了java-制表符\t的使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Mybatis結(jié)果生成鍵值對的實(shí)例代碼

    Mybatis結(jié)果生成鍵值對的實(shí)例代碼

    這篇文章主要介紹了Mybatis結(jié)果生成鍵值對的實(shí)例代碼,以及MyBatis返回Map鍵值對數(shù)據(jù)的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下
    2017-02-02
  • SpringBoot @Value注解支持配置自動(dòng)刷新能力擴(kuò)展方式

    SpringBoot @Value注解支持配置自動(dòng)刷新能力擴(kuò)展方式

    本文介紹了如何通過自定義注解和BeanPostProcessor實(shí)現(xiàn)SpringBoot中@Value注解的配置自動(dòng)刷新能力,主要步驟包括:定義一個(gè)支持動(dòng)態(tài)刷新的注解,實(shí)現(xiàn)配置的動(dòng)態(tài)變更,以及通過BeanPostProcessor掃描并刷新使用@Value注解的變量
    2024-12-12
  • 聊一聊new對象與Spring對bean的初始化的差別

    聊一聊new對象與Spring對bean的初始化的差別

    這篇文章主要介紹了聊一聊new對象與Spring對bean的初始化的差別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 淺談Java虛擬機(jī)對內(nèi)部鎖的四種優(yōu)化方式

    淺談Java虛擬機(jī)對內(nèi)部鎖的四種優(yōu)化方式

    這篇文章主要介紹了淺談Java虛擬機(jī)對內(nèi)部鎖的四種優(yōu)化方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10

最新評論

武川县| 栾城县| 岚皋县| 芜湖县| 荃湾区| 安新县| 南漳县| 郴州市| 浮山县| 平江县| 同心县| 嘉善县| 济南市| 弥勒县| 鄂温| 南宁市| 黔西| 开化县| 福安市| 兴国县| 仪陇县| 元谋县| 汉中市| 英超| 武邑县| 安国市| 岢岚县| 赣榆县| 宜兴市| 宜宾市| 汪清县| 清徐县| 淮滨县| 岳西县| 陇南市| 资中县| 天长市| 蓬溪县| 博野县| 洪泽县| 廉江市|