spring為java.util.Properties類型的屬性進(jìn)行賦值過程解析
這篇文章主要介紹了spring為java.util.Properties類型的屬性進(jìn)行賦值過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
Properties 類表示了一個(gè)持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。在spring中可以用其存儲(chǔ)連接數(shù)據(jù)庫的相關(guān)信息。
DataSource.java
package com.gong.spring.beans;
import java.util.Properties;
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.gong.spring.beans.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
</beans>
Main.java
package com.gong.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1.創(chuàng)建spring的IOC容器對(duì)象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.從容器中獲取Bean實(shí)例
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.toString());
}
}
輸出:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java8 stream sort自定義復(fù)雜排序案例
這篇文章主要介紹了java8 stream sort自定義復(fù)雜排序案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Java解壓縮zip - 解壓縮多個(gè)文件或文件夾實(shí)例
本篇文章主要介紹了Java解壓縮zip - 解壓縮多個(gè)文件或文件夾實(shí)例,非常具有實(shí)用價(jià)值,有需要的可以了解一下。2016-12-12
Spring Boot 基于注解的 Redis 緩存使用詳解
本篇文章主要介紹了Spring Boot 基于注解的 Redis 緩存使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
JAVA代碼實(shí)現(xiàn)MongoDB動(dòng)態(tài)條件之分頁查詢
這篇文章主要介紹了JAVA如何實(shí)現(xiàn)MongoDB動(dòng)態(tài)條件之分頁查詢,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

