spring:使用<prop>标签为Java持久属性集注入值
spring:使用<prop>标签为Java持久属性集注入值
使用 spring 提供的<prop>为Java持久属性集注入值,也就是向 java.util.Properties 对象中注入值。
<props> 是简化了的 <map> ,该元素对应配置类型为 java.util.Properties 的对象依赖。
因为Properties 只能指定 String 类型的键和值,所以 <props> 的配置简化很多,只有固定的格式。
1.创建 TestUtil 类,并添加一个 Properties 类型的属性,然后添加 getter 和 setter 方法。代码如下:
package com.importnew;
import java.util.Properties;
public class TestUtil {
private Properties prop;
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
}
2.编写 spring 配置文件,对 TestUtil 进行配置,并对 Properties 属性赋值。代码如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="testUtil" class="com.importnew.TestUtil" >
<property name="prop">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
3.编写测试类 TestSpring,代码如下:
package test; import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.importnew.TestUtil; public class TestSpring { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestUtil testUtil = (TestUtil) context.getBean("testUtil");
Properties prop = testUtil.getProp();
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println("driver = "+ driver +"。url = " + url +"。username = "
+username+"。password = " + password+"。");
}
}
———————————————————————————————————————————————————
备注:<props>元素。每个 <props> 可以嵌套多个 <prop> , <prop> 内部没有任何元素可以使用,只能是字符串,这是由 java.util.Properties 的语义决定的。
////end
spring:使用<prop>标签为Java持久属性集注入值的更多相关文章
- 【Java基础】Java中的持久属性集Properties
Properties 类的介绍 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串.一个属性列表可包含另一个属性列 ...
- Java之属性集(Properties类)
Properties概述 java.util.Properties类 继承于 Hashtable ,来表示一个持久的属性集.它使用键值结构存储数据,每个键及其对应值都是一个字符串.该类也被许多Java ...
- Spring(3.2.3) - Beans(5): 集合属性的注入
如果 Bean 的属性是个集合,则可以使用 <list/>.<set/>.<map/> 和 <props/> 元素向 List.Set.Map 和 Pr ...
- Spring Security http标签的use-expressions="true"属性
如果声明为true,那么在access属性要用hasRole()这样写: <intercept-url pattern="/secure/extreme/**" access ...
- java Properties (属性集)
加载Properties Properties downloadLog = new Properties(); try { //加载logFile文件 downloadLog.load(new Fil ...
- <c:out>标签中有一个escapeXml属性 如果为escapeXml="false",则将其中的html、xml解析出来。
<td><c:out value="${s.name}" escapeXml="false"></c:out></td ...
- [Java] 解决spring的xml标签内不能自由增加说明的难题,方便调试、部署时进行批量屏蔽
作者:zyl910 以往我们想在spring的xml配置文件中增加说明文本时,只能使用xml注释(<!-- 注释 -->).这对于"调试.部署时想批量屏蔽部分bean" ...
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明
Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...
随机推荐
- html5——语义标签
传统布局 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- JavaScript定时器及其他
By Abyssly Jun 20 2014 Updated:Jun 20 2014 平时工作中不可避免地要嵌套网页,对JavaScript的深入了解还是很有必要滴.而JavaScript中一个容易让 ...
- Java数组数据类型
Java数组数据类型 数组是多个相同类型的数据的组合,数组中的元素可以是任何类型的数据: 一维数组 package com.ahabest.array; public class ArratTest ...
- springboot配置多数据源(JdbcTemplate方式)
在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host.需要使用多种数据库(MySql.Oracle.SqlServer…) 如果使用springboot开发,可做如下配置: Co ...
- iview表单密码自定义验证
From中定义 ref="passwordForm" 获取dom节点 :model="passwordForm" 关联表单数据对象 :rules=&quo ...
- vue跨域问题解决(生产环境)
vue跨域问题解决(使用webpack打包的) 配置代理:(config下index.js文件) module.exports = { dev: { env: require('./dev.env') ...
- 匈牙利算法求最大匹配(HDU-4185 Oil Skimming)
如下图:要求最多可以凑成多少对对象 大佬博客: https://blog.csdn.net/cillyb/article/details/55511666 https://blog.csdn.net/ ...
- [LeetCode] 887. Super Egg Drop 超级鸡蛋掉落
You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is iden ...
- 3D全景之ThreeJs
3D全景之ThreeJs 一.前言 随着H5越来越多的被应用到各个领域,3D也越来越频繁的出现在各个H5案例中,今天我们就来讨论一下3D全景的实现. 据百度百科上介绍:720全景是视角超过人的正常视角 ...
- 【codeforces 765E】Tree Folding
[题目链接]:http://codeforces.com/problemset/problem/765/E [题意] 给你一棵树; 可以把一个节点的两条相同长度的链合并成一条链; 且这两条相同长度的链 ...