欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年01月24日 16:48:41   作者:Ricky_Fung  
本篇談?wù)凷pring 注入properties文件總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

在Spring配置文件中引入

方式一

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

<?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" />
    <!-- 獲取連接最大等待時(shí)間 -->
    <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" />
    <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="25200000" />
    <!-- 打開removeAbandoned功能 -->
    <property name="removeAbandoned" value="true" />
    <!-- 1800秒,也就是30分鐘 -->
    <property name="removeAbandonedTimeout" value="1800" />
    <!-- 關(guān)閉abanded連接時(shí)輸出錯誤日志 -->
    <property name="logAbandoned" value="true" />
    <!-- 監(jiān)控?cái)?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

#
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>
    <!-- 設(shè)置編碼格式 -->
    <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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于SHA算法原理與常用實(shí)現(xiàn)方式

    關(guān)于SHA算法原理與常用實(shí)現(xiàn)方式

    這篇文章主要介紹了關(guān)于SHA算法原理與常用實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Spring根據(jù)XML配置文件注入屬性的方法

    Spring根據(jù)XML配置文件注入屬性的方法

    下面小編就為大家?guī)硪黄猄pring根據(jù)XML配置文件注入屬性的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • java實(shí)現(xiàn)Dijkstra算法

    java實(shí)現(xiàn)Dijkstra算法

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Dijkstra算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • springmvc不進(jìn)入Controller導(dǎo)致404的問題

    springmvc不進(jìn)入Controller導(dǎo)致404的問題

    這篇文章主要介紹了springmvc不進(jìn)入Controller導(dǎo)致404的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring BeanDefinition使用介紹

    Spring BeanDefinition使用介紹

    BeanDefinition是Spring框架中非常核心的概念,BeanDefinition是定義Bean的配置元信息接口,Spring根據(jù)BeanDefinition來定義Bean對象,簡單說就是對Bean信息的定義
    2023-01-01
  • SpringMVC統(tǒng)一異常處理實(shí)例代碼

    SpringMVC統(tǒng)一異常處理實(shí)例代碼

    這篇文章主要介紹了SpringMVC統(tǒng)一異常處理實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java可重入鎖的實(shí)現(xiàn)原理與應(yīng)用場景

    Java可重入鎖的實(shí)現(xiàn)原理與應(yīng)用場景

    今天小編就為大家分享一篇關(guān)于Java可重入鎖的實(shí)現(xiàn)原理與應(yīng)用場景,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • java中BIO、NIO、AIO都有啥區(qū)別

    java中BIO、NIO、AIO都有啥區(qū)別

    這篇文章主要介紹了java中BIO、NIO、AIO都有啥區(qū)別,IO模型就是說用什么樣的通道進(jìn)行數(shù)據(jù)的發(fā)送和接收,Java共支持3種網(wǎng)絡(luò)編程IO模式:BIO,NIO,AIO,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • SpringBoot淺析安全管理之Shiro框架

    SpringBoot淺析安全管理之Shiro框架

    安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理Shiro框架的使用
    2022-08-08
  • idea中引入了gb2312編碼的文件的解決方法

    idea中引入了gb2312編碼的文件的解決方法

    這篇文章主要介紹了idea中引入了gb2312編碼的文件的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評論