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持久属性集注入值的更多相关文章

  1. 【Java基础】Java中的持久属性集Properties

    Properties 类的介绍 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串.一个属性列表可包含另一个属性列 ...

  2. Java之属性集(Properties类)

    Properties概述 java.util.Properties类 继承于 Hashtable ,来表示一个持久的属性集.它使用键值结构存储数据,每个键及其对应值都是一个字符串.该类也被许多Java ...

  3. Spring(3.2.3) - Beans(5): 集合属性的注入

    如果 Bean 的属性是个集合,则可以使用 <list/>.<set/>.<map/> 和 <props/> 元素向 List.Set.Map 和 Pr ...

  4. Spring Security http标签的use-expressions="true"属性

    如果声明为true,那么在access属性要用hasRole()这样写: <intercept-url pattern="/secure/extreme/**" access ...

  5. java Properties (属性集)

    加载Properties Properties downloadLog = new Properties(); try { //加载logFile文件 downloadLog.load(new Fil ...

  6. <c:out>标签中有一个escapeXml属性 如果为escapeXml="false",则将其中的html、xml解析出来。

    <td><c:out value="${s.name}" escapeXml="false"></c:out></td ...

  7. [Java] 解决spring的xml标签内不能自由增加说明的难题,方便调试、部署时进行批量屏蔽

    作者:zyl910 以往我们想在spring的xml配置文件中增加说明文本时,只能使用xml注释(<!-- 注释 -->).这对于"调试.部署时想批量屏蔽部分bean" ...

  8. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  9. Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明

    Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...

随机推荐

  1. CSS——background综合运用

    搜索栏图标: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  2. 将vim的UltiSnips的快捷键彻底从tab键中分离

    在我之前的<<vim之补全1>>和<<vim之补全2>>中曾经成功的将vim的supertab和UltiSnips共用一个tab键, 这样做的优点的两种 ...

  3. 【译】x86程序员手册14-5.1段转换

    5.1 Segment Translation 段转换 Figure 5-2 shows in more detail how the processor converts a logical add ...

  4. Python 之selenium+phantomJS斗鱼抓取案例

    from selenium import webdriver from bs4 import BeautifulSoup import time if __name__ == '__main__': ...

  5. css知识框架

  6. 零基础学习Python培训,应该选择哪个培训班?

    近几年中,Python一直是市场上最受欢迎的编程语言之一.它语法自然,入门简单,同时应用范围又极广,无论是大火的人工智能.大数据还是传统的web开发.自动化运维,Python都能够大展拳脚.根据职友集 ...

  7. 新建python的虚拟环境

    1.mkvirutalenv --python=E:\Users\00\AppData\Local\Programs\Python\Python37-32\python.exe article_spi ...

  8. IE低版本和高级浏览器对文本输入事件兼容

    1 一般 使用oninput 事件可以监控文本输入事实触发 2 兼容需要使用onpropertychange . 3 兼容写法   var evenInput=DOM元素.oninput || DOM ...

  9. 从0开始复习JS---1、函数复习

    1. 写一个函数,实现对数字数组的排序. function get_order(array){ for(var i = 0; i <array.length-1; i++){ for(var j ...

  10. 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 5

    项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...