谈谈Spring 注入properties文件总结
本篇谈谈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);
}
} 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
谈谈Spring 注入properties文件总结的更多相关文章
- spring boot 在框架中注入properties文件里的值(Spring三)
前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...
- 如何通过Spring读取Properties文件
1 在Spring中配置文件中, 配置配置文件的引用 <util:properties id="settings" location="/WEB-INF/c ...
- spring 读取properties文件--通过注解方式
问题: 需要通过properties读取页面的所需楼盘的名称.为了以后便于修改. 解决: 可以通过spring的 PropertiesFactoryBean 读取properties属性,就不需要自己 ...
- Spring自动注入properties文件
实现spring 自动注入属性文件中的key-value. 1.在applicationContext.xml配置文件中,引入<util />命名空间. xmlns:util=" ...
- Spring 通过配置文件注入 properties文件
当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷 1. spring 配置文件需要导入 <?xml versio ...
- Spring获取properties文件中的属性
1.前言 本文主要是对这两篇blog的整理,感谢作者的分享 Spring使用程序方式读取properties文件 Spring通过@Value注解注入属性的几种方式 2.配置文件 applicatio ...
- Java-马士兵设计模式学习笔记-工厂模式-模拟Spring读取Properties文件
一.目标:读取properties文件,获得类名来生成对象 二.类 1.Movable.java public interface Movable { void run(); } 2.Car.java ...
- spring boot properties文件与yaml文件的区别
编写是没有提示的话在pom中添加依赖,如下: <!-- 配置文件处理器 编写配置时会有提示 --> <dependency> <groupId>org.spring ...
- springboot使用@Value注入properties文件中的值,中文乱码
最近开发一个需求,讲一个中文值配置在properties文件中,然后代码中使用@Value注解进行注入使用,然而出现了如下状况: 中文出现乱码,将代码修改如下: String str = new St ...
随机推荐
- java代码------计算器核心位置添加
总结:点击等号时,什么代码 else if(str.equals("-")){ ready=true; if(c=='\0'){ num1=Double.parseDouble(j ...
- 如何通过.Net Compact Framework来获得应用程序的当前路径
在Win CE上是没有驱动器的概念的,所以要想使用System.IO.Directory.GetCurrentDirectory()来获得当前路径的话,在CF中会遇到未知错误. 应该使用Path. ...
- 福利向:几款给力的Unity脚本插件推荐
转自:http://www.gamelook.com.cn/2016/09/264877 Unity的Asset Store中除了拥有非常强大的Unity编辑器扩展工具之外,还有一些让开发过程事半功倍 ...
- (转) C#中Timer使用及解决重入(多线程同时调用callback函数)问题
原文链接: http://www.cnblogs.com/hdkn235/archive/2014/12/27/4187925.html
- 《Java核心技术》 -- 读书笔记 ② - 类 | 对象 | 接口
对象vs对象变量 “对象” 描述的是一个类的具体实例,他被java虚拟机分配在 "堆" (Heap)中. “对象变量” 为一个对象的引用(对象变量的值=记载着具体对象的位置/地址) ...
- 简单租房子实例详解---(session、ajax、json前后台数据处理、分页)
本次实例我们结合session.ajax.json前后台数据处理.分页技术做一个租房信息系统 一共有五个界面:包括 管理员和用户的登录界面 登录界面的后台 <?php session_start ...
- linux加程序是否当掉检测脚本
cd $(dirname $) source ~/.bash_profile SYSTEM_TIME=`date '+%Y-%m-%d %T'` count=`ps -ef |grep "p ...
- 用 Sqlmap 识别 WAF
命令: ┌─[root@sch01ar]─[~] └──╼ #sqlmap -u "http://www.sch01ar.com/" --identify-waf --batch ...
- 深入浅出 Java Concurrency (12): 锁机制 part 7 信号量(Semaphore)
Semaphore 是一个计数信号量.从概念上讲,信号量维护了一个许可集.如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可.每个 release() 添加一个许可,从而可能 ...
- 5 matplotlib-绘制精美的图表
matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备, ...