一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 談?wù)凷pring 注入properties文件總結(jié)

談?wù)凷pring 注入properties文件總結(jié)

2020-08-01 15:24Ricky_Fung Java教程

本篇談?wù)凷pring 注入properties文件總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring提供了多種方式來注入properties文件,本文做一個簡單的總結(jié)。

在Spring配置文件中引入

方式一

通過<context:property-placeholder />標(biāo)簽

?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?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" />
    <!-- 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="25200000" />
    <!-- 打開removeAbandoned功能 -->
    <property name="removeAbandoned" value="true" />
    <!-- 1800秒,也就是30分鐘 -->
    <property name="removeAbandonedTimeout" value="1800" />
    <!-- 關(guān)閉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>
 
  <!-- 配置事務(wù)管理器 -->
  <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

?
1
2
3
4
5
6
7
8
9
10
#
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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

?
1
2
3
name=ricky
age=27
password=root

2、applicationContext.xml

?
1
2
3
4
5
6
7
8
9
10
11
<!-- 使用注解注入properties中的值 -->
  <bean id="config"
     class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
      <list>
        <value>classpath:config.properties</value>
      </list>
    </property>
    <!-- 設(shè)置編碼格式 -->
    <property name="fileEncoding" value="UTF-8"></property>
  </bean>

3、使用@Value注解

?
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
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/top_code/article/details/52170200

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 肥胖女性大bbbbbb视频女厕 | 手机看片自拍 | 亚洲AV无码国产精品午夜久久 | 女同变态 中文字幕 | 四虎永久免费在线观看 | 三级全黄的视频 | 精品欧美一区二区三区四区 | 日本不卡免免费观看 | 亚洲精品动漫在线观看 | 国产亚洲欧美在线中文bt天堂网 | 免费看一级毛片 | 五月天精品视频播放在线观看 | 荡女淫春2古装 | 福利片免费一区二区三区 | 精品国产一区二区三区久久久蜜臀 | 日本高清免费不卡在线 | 成年人福利视频 | 精品一区二区三区免费毛片 | 日本欧美强乱视频在线 | 丫鬟粗大狠狠贯穿h | s8sp加密路线和免费路线首页 | 99久久精品免费精品国产 | 欧美区一区| 99热精品在线观看 | 摄像头东北对白清晰 | 亚洲欧美日韩精品 | 极品美女aⅴ高清在线观看 极品ts赵恩静和直男激战啪啪 | 亚洲欧美在线观看首页 | 大吊小说 | 女人麻豆国产香蕉久久精品 | 四虎在线永久视频观看 | 成人在线第一页 | 亚洲成色WWW久久网站夜月 | 五月色天在线视频综合观看 | 午夜伦理yy44008影院 | 韩国三级视频网站 | 古代翁熄系小说辣文 | 美女扒开肌肌让男人桶 | 俄罗斯妈妈k8影院在线观看 | 日韩天堂视频 | 亚洲码在线观看 |