1. 程式人生 > >【Spring之配置propertie資原始檔】Maven整合spring profiles功能配置propertie資原始檔更靈活、簡單

【Spring之配置propertie資原始檔】Maven整合spring profiles功能配置propertie資原始檔更靈活、簡單

spring 框架的xml檔案如何讀取properties檔案資料

第一步:在spring配置檔案中

  注意:value可以多配置幾個properties檔案

<bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                     <list>
                            <value>/db.properties</value>
                           
                     </list>
              </property>
       </bean>

第二步:

  在src目錄下面建立db.properties檔案

user=sa
password=sa
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName=DB1

第三步:

  在spring的配置檔案中通過EL表示式的形式呼叫 

${user}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 
       <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                     <list>
                            <value>/db.properties</value>
                           
                     </list>
              </property>
       </bean>
 
       <bean id="datasource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName"
                     value="${driver}">
              </property>
              <property name="url"
                     value="${url}">
              </property>
              <property name="username" value="${user}"></property>
              <property name="password" value="${password}"></property>
       </bean>
       <bean id="sessionFactory"
              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              <property name="dataSource">
                     <ref bean="datasource" />
              </property>
              <property name="hibernateProperties">
                     <props>
                            <prop key="hibernate.dialect">
                                   org.hibernate.dialect.SQLServerDialect
                            </prop>
                     </props>
              </property>
              <property name="mappingResources">
                     <list>
                            <value>entity/Users.hbm.xml</value>
                     </list>
              </property>
       </bean>
       <bean id="UsersDAO" class="dao.UsersDAO">
              <property name="sessionFactory">
                     <ref bean="sessionFactory" />
              </property>
       </bean>
 
</beans>

spring為beans標籤提供了profile功能,以便專案的開發和生成環境分離。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <beans profile="dev,test"> <context:property-placeholder location="classpath:application.properties" /> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <property name="driverClass" value="${db.driver}"/> <property

相關推薦

Spring配置propertie原始檔Maven整合spring profiles功能配置propertie原始檔靈活簡單

spring 框架的xml檔案如何讀取properties檔案資料 第一步:在spring配置檔案中   注意:value可以多配置幾個properties檔案 <bean id="propertyConfigurer"

SpringMVCFreeMark(一)spring mvc中使用freemark的方法

    模板技術在現代的軟體開發中有著重要的地位,而目前最流行的兩種模板技術恐怕要算freemarker和velocity了,webwork2.2對兩者都有不錯的支援,也就是說在webwork2中你可以隨意選擇使用freemarker或velocity作為view,模板

Python3函數對象

變量 類型 程序語言 部分 函數對象 utf-8 cti 一個 title 函數對象 函數(Function)作為程序語言中不可或缺的一部分,但函數作為第一類對象(First-Class Object)卻是 Python 函數的一大特性。 那到底什麽是第一類對象(First

linux正則表達式

匹配 ext 表達 col oba 包括 部分 style 符號 一、grep grep家族grep、egrep、fgrepGlobally search a Regular Expression and Print根據我們提供的模式進行查找,並且將文件中匹配的行顯示出來模

JAVA反射方法的應用

JAVA之反射的應用 import java.util.HashMap; import java.util.Map; public class Child extends Parent { public int add(int a,int b){ return a+b; }

必看SpringBoot整合Spring Data Elasticsearch

原生java整合elasticsearch的API地址:(類似JDBC) https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.2/java-docs.html Elasticsearch提供的Java客戶

演算法陣列(一)求子陣列最大和的解決方法詳解

題目: 輸入一個整形陣列,數組裡有正數也有負數。 陣列中連續的一個或多個整陣列成一個子陣列,每個子陣列都有一個和。 求所有子陣列的和的最大值。 例如輸入的陣列為1, -2, 3, 10, -4, 7, 2, -5,和最大的子陣列為3, 10, -4, 7, 2, 因此

springboot整合spring security(一)

原文作者:王文健  來源:CSDN  轉自原文:https://blog.csdn.net/qq_29580525/article/details/79317969  但是原文有幾處錯誤,且本文也結合了其他自己的知識,可以說是上文的升級版本 一、Spring s

SpringbootSpringboot整合Jasypt,讓配置資訊保安最優雅方便的方式

1 簡介 在上一篇文章中,介紹了Jasypt及其用法,具體細節可以檢視【Java庫】如何使用優秀的加密庫Jasypt來保護你的敏感資訊?。如此利器,用之得當,那將事半功倍。本文將介紹Springboot整合Jasypt,實現配置資訊的安全,如資料庫連線、賬號和密碼、介面憑證資訊等。 Jasypt可以為Spri

手摸手,帶你搭建前後端分離商城系統03 整合Spring Security token 實現方案,完成主業務登入

## 【手摸手,帶你搭建前後端分離商城系統】03 整合Spring Security token 實現方案,完成主業務登入 上節裡面,我們已經將基本的前端 `VUE + Element UI` 整合到了一起。並且通過 `axios` 傳送請求到後端API。 解決跨域問題後、成功從後端獲取到資料。 本小結

SSH網上商城專案實戰11查詢和刪除商品功能的實現

  轉自:https://blog.csdn.net/eson_15/article/details/51360804   在第8節我們完成了查詢和刪除商品類別的功能,那麼現在實現查詢和刪除商品的功能就很好做了,原理和第8節一模一樣,只是修改一些引數,比如請求不同的a

SSH網上商城專案實戰12新增和更新商品功能的實現

  新增商品部分原理和新增商品類別是一樣的,不過要比商品類別複雜,因為商品的屬性有很多,對應的資料庫中的欄位也就多了,新增商品還有個選項是上傳圖片,這一小塊內容會在下一篇部落格中單獨說明,因為這涉及到一個知識點,就是Struts2實現檔案上傳功能。其他廢話不多說了,現在開始完善新增

SSH網上商城專案實戰18過濾器實現購物登入功能的判斷

    轉自:https://blog.csdn.net/eson_15/article/details/51425010     上一節我們做完了購物車的基本操作,但是有個問題是:當用戶點選結算

訊息佇列非同步訊息基本概念以及ActiveMQ整合Spring常用用法介紹

一 簡介 (1)非同步訊息: 所謂非同步訊息,跟RMI遠端呼叫、webservice呼叫是類似的,非同步訊息也是用於應用程式之間的通訊。但是它們之間的區別是: RMI、Hession/Burlap、webservice等遠端呼叫機制是同步的。也就是說,當客戶端呼叫遠端方法時,客戶端

使用maven整合spring+springmvc+mybatis

cti 監聽 package 基於接口 mysql 5.7 spring 整合 rri ext inter 使用maven整合spring+springmvc+mybatis 開發環境:     1.  jdk1.8     2.  eclipse4.7.0 (Oxygen

Maven整合Spring與Solr

  首先,在maven的pom.xml檔案中配置對spring和solrj客戶端的依賴: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

Maven 整合 spring profile實現多環境自動切換

profile主要用在專案多環境執行的情況下,比如開發環境、測試環境、線上生產環境。我負責的專案某個資料管理後臺涉及到包含測試環境在內的12個不同的執行環境,所以每次釋出都很蛋疼很糾結,配置改過來改過去,到最後有些環境都忘了怎麼配的。下面以這個專案為例介紹。準備條件:spri

Linux旅-7Linux使用者和組的配置檔案

一、使用者(user)的配置檔案 1.1配置檔案位置 /etc/passwd 1.2作用 記錄使用者的各種資訊。 1.3檔案每行的含義 使用者名稱:口令:使用者標識號:組標識號:註釋性描述:

bug記錄Eclipse執行Spring Boot專案讀取不到配置檔案

專案是spring boot專案, 編寫好程式碼後,我以spring  boot app的形式執行專案,結果控制檯報錯,大概是說mybatis的mapper注入失敗,原因是datasource沒找到。檢查配置檔案的資料庫配置以及mybaits的配置後,未發現錯誤。拿起八倍鏡再

spring框架28springpackagesToScan

spring的spring.jar的jar包內,在org.springframework.orm.hibernate3.annotation下,有一個AnnotationSessionFactoryBean類,其中有一個屬性叫做"packagesToScan",有個方法叫se