欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

谈谈Spring 注入properties文件总结

程序员文章站 2024-03-08 13:32:46
spring提供了多种方式来注入properties文件,本文做一个简单的总结。 在spring配置文件中引入 方式一 通过

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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。