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. RecyclerView中item无法充满的问题

    首先致谢:https://blog.csdn.net/yuanlvmao/article/details/51694211 咱们不是代码的生产者,只是代码的搬运工. 今天写了一个RecyclerVie ...

  2. Leetcode0133--Clone Graph 克隆无向图

    [转载请注明]:https://www.cnblogs.com/igoslly/p/9699791.html 一.题目 二.题目分析 给出一个无向图,其中保证每点之间均有连接,给出原图中的一个点 no ...

  3. 初始MongoDB------MongoDB的安装

    MongoDB在Windows的安装是很简单的,无论是安装包还是绿色包,安装出来的都是这些文件   重点是Bin中的东西   特别是前两个的执行文件 mongod进程就是启动MongoDB数据库的进程 ...

  4. H5 标签属性、input属性

    高亮文字: 全部商品只要<mark>6.18</mark> 结果:     加拼音文字: <ruby>變<rt>bian</rt></ ...

  5. css学习笔记---盒模型,布局

    1.外边距叠加 当一个元素出现在另一个元素上面时第一个元素的底边距与第二个元素的上边距发生叠加,元素被包含时也有可能会发生叠加(如果没有内边距和边框),如果一个空元素没有内边距和边框本身也会发生上下边 ...

  6. Java_Web三大框架之Hibernate操作数据库(三)

    使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射 ...

  7. JAVA趣味逻辑算法

    /**已知4位同学中的一位数学考了100分,当小李询问这4位是谁考了100分时,4个人的回答如下: A说:不是我. B说:是C C说:是D. D说:他胡说. 已知三个人说的是真话,一个人说的是假话.现 ...

  8. mysql_数据查询_连接查询

    连接查询 1.连接(join) 也称θ连接,从两个关系的笛卡尔积中选择属性间满足一定条件的元组. 等值连接:θ为“=”的连接运算称为等值连接.从关系R和S的广义笛卡尔积中选取A.B属性值相等的元组. ...

  9. js弹开页面并调用方法

    每次重新写一个功能的时候,都能发现以前写的并不太好,都可以改进,奇怪的是我还是我,为什么曾经的我就想不起来要这么写,比如下面两段代码 历史代码: if (infoTablePage != null) ...

  10. Python 之mysql类封装

    import pymysql class MysqlHelper(object): conn = None def __init__(self, host, username, password, d ...