談談Spring 注入properties文件總結(jié)
spring提供了多種方式來注入properties文件,本文做一個簡單的總結(jié)。
在Spring配置文件中引入
方式一
通過<context:property-placeholder />標簽
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:mysql.properties" ignore-unresolvable="true"/>
<!-- 配置數(shù)據(jù)源 -->
<bean abstract="true" name="parentDatasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${ds1.jdbc.driverClassName}" />
<!-- 初始化連接大小 -->
<property name="initialSize" value="1" />
<!-- 連接池最大使用連接數(shù)量 -->
<property name="maxActive" value="100" />
<!-- 連接池最小空閑 -->
<property name="minIdle" value="20" />
<!-- 獲取連接最大等待時間 -->
<property name="maxWait" value="30000" />
<!-- <property name="poolPreparedStatements" value="true" /> -->
<!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
<property name="validationQuery" value="SELECT 1" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打開removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分鐘 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 關閉abanded連接時輸出錯誤日志 -->
<property name="logAbandoned" value="true" />
<!-- 監(jiān)控數(shù)據(jù)庫 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!-- 配置數(shù)據(jù)源 -->
<bean name="dataSource1" init-method="init" destroy-method="close" parent="parentDatasource">
<property name="url" value="${ds1.jdbc.url}" />
<property name="username" value="${ds1.jdbc.username}" />
<property name="password" value="${ds1.jdbc.password}" />
</bean>
<!-- 配置數(shù)據(jù)源 -->
<bean name="dataSource2" init-method="init" destroy-method="close" parent="parentDatasource">
<property name="url" value="${ds2.jdbc.url}" />
<property name="username" value="${ds2.jdbc.username}" />
<property name="password" value="${ds2.jdbc.password}" />
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1" />
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
方式二
通過<util:properties />
1、MySQL.properties
# ds1.jdbc.driverClassName=com.mysql.jdbc.Driver ds1.jdbc.url=jdbc:mysql://localhost:3306/process?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull ds1.jdbc.username=root ds1.jdbc.password=root ds2.jdbc.driverClassName=com.mysql.jdbc.Driver ds2.jdbc.url=jdbc:mysql://localhost:3306/process?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull ds2.jdbc.username=root ds2.jdbc.password=root
2、applicationContext.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false">
<util:properties id="db" location="classpath:mysql.properties"/>
<!-- 配置數(shù)據(jù)源 -->
<bean name="dataSource1" init-method="init" destroy-method="close" parent="parentDatasource">
<property name="url" value="#{db['ds1.jdbc.url']}" />
<property name="username" value="#{db['ds1.jdbc.username']}" />
<property name="password" value="#{db['ds1.jdbc.password']}" />
</bean>
</beans>
在代碼中注入
方式一
1、config.properties
name=ricky age=27 password=root
2、applicationContext.xml
<!-- 使用注解注入properties中的值 -->
<bean id="config"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
<!-- 設置編碼格式 -->
<property name="fileEncoding" value="UTF-8"></property>
</bean>
3、使用@Value注解
package com.ricky.codelab.springmvc.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* ${DESCRIPTION}
*
* @author Ricky Fung
* @create 2016-08-08 15:49
*/
@Component("userService")
public class UserServiceImpl implements IUserService {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Value("#{config[name]}")
private String name;
@Value("#{config[age]}")
private Integer age;
@Value("#{config[password]}")
private String password;
@Override
public void login(String username){
System.out.println("name:"+name+",age="+age+",password="+password);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 在SpringBoot下讀取自定義properties配置文件的方法
- SpringBoot獲取yml和properties配置文件的內(nèi)容
- 詳解Spring Boot加載properties和yml配置文件
- 詳解SpringMVC加載配置Properties文件的幾種方式
- 詳解spring boot 使用application.properties 進行外部配置
- Spring Boot中配置文件application.properties使用
- Spring Boot2.0 @ConfigurationProperties使用詳解
- spring boot使用i18n時properties文件中文亂碼問題的解決方法
- Spring加載properties文件的方法
- Spring中配置和讀取多個Properties文件的方式方法
- 詳解Spring加載Properties配置文件的四種方式
- Spring中屬性文件properties的讀取與使用詳解
- spring boot application properties配置實例代碼詳解
- Spring Boot的properties配置文件讀取
- spring boot中的properties參數(shù)配置詳解
- Spring用代碼來讀取properties文件實例解析
- Spring加載properties文件的兩種方式實例詳解
- spring無法讀取properties文件數(shù)據(jù)問題詳解
相關文章
使用Maven創(chuàng)建和管理多模塊項目的詳細步驟
使用Maven進行多模塊項目管理是一種常見的做法,它可以幫助你組織大型項目,使其結(jié)構(gòu)更加清晰,便于維護和構(gòu)建,以下是使用Maven創(chuàng)建和管理多模塊項目的詳細步驟,需要的朋友可以參考下2024-10-10
Java如何使用遞歸查詢多級樹形結(jié)構(gòu)數(shù)據(jù)(多級菜單)
這篇文章主要介紹了Java如何使用遞歸查詢多級樹形結(jié)構(gòu)數(shù)據(jù)(多級菜單),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring Boot Admin Server管理客戶端過程詳解
這篇文章主要介紹了Spring Boot Admin Server管理客戶端過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
這篇文章主要介紹了SpringBoot調(diào)第三方WebService接口的操作代碼(.wsdl與.asmx類型 ),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
如何利用Java輸出鏈表中倒數(shù)第k個結(jié)點
這篇文章主要給大家介紹了關于如何利用Java輸出鏈表中倒數(shù)第k個結(jié)點的相關資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友可以參考下2021-12-12
Springboot詳解整合SpringSecurity實現(xiàn)全過程
Spring Security基于Spring開發(fā),項目中如果使用Springboot作為基礎,配合Spring Security做權限更加方便,而Shiro需要和Spring進行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領域很常用2022-07-07

