bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Spring項目中如何使用Profile

Spring項目中如何使用Profile?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

我們提供的服務有:成都做網(wǎng)站、成都網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、沭陽ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的沭陽網(wǎng)站制作公司

前言

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

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

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

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

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

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

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

Profile bean的配置

通過注解@Profile配置

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

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

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

schema.sql

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

這里定義了一張Things表包含了兩個字段

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

test-data.sql

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

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

從Spring 3.1版本中就引入了bean profile的功能,可以讓你將不同的bean定義到一個或者多個profile里,然后在部署應用時告知要激活那個profile,則相應的bean就會被創(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標記為dev環(huán)境下要創(chuàng)建的bean。

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

         2. 如果當前是dev環(huán)境被激活了,那么對于沒有使用@Profile的bean都會被創(chuàng)建,被標記為其他的profile如prod,則不會創(chuà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

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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)境以及對應的profile。

profile激活

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

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

設置這兩個屬性的方式有很多:

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

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

作為JNDI條目

作為環(huán)境變量

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

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

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

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<web -app version="2.5"
...>

 //為上下文設置默認的profile
 <context-param>
 <param-name>spring.profile.default</param-name>
 <param-value>dev</param-value>
 </context-param>

...

 <servlet>
 ...
 //為Serlvet設置默認的profile
 <init-param>
  <param-name>spring-profiles.default</param-name>
  <param-value>dev</param-value>
 </init-prama>

...
<web-app>

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

另外對于測試,spring為什么提供了一個簡單的注解可以使用@ActiveProfiles,它可以指定運行測試的時候應該要激活那個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);
 }
 }

}

運行shouldBeEmbeddedDatasource方法,測試通過

Spring項目中如何使用Profile

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)站標題:Spring項目中如何使用Profile
URL地址:http://vcdvsql.cn/article38/peidsp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航微信小程序標簽優(yōu)化App開發(fā)域名注冊靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)