Spring 注入properties文件总结

Spring提供了多种方式来注入properties文件,本文做一个简单的总结。

在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"/>

    <!-- 配置数据源 -->
    <bean abstract="true" name="parentDatasource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${ds1.jdbc.driverClassName}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="1" />
        <!-- 连接池最大使用连接数量 -->
        <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" />
        <!-- 监控数据库 -->
        <!-- <property name="filters" value="stat" /> -->
        <property name="filters" value="mergeStat" />
    </bean>

    <!-- 配置数据源 -->
    <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>


    <!-- 配置数据源 -->
    <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"/>

<!-- 配置数据源 -->
    <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);
    }
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值