spring為java.util.Properties類型的屬性進行賦值過程解析
這篇文章主要介紹了spring為java.util.Properties類型的屬性進行賦值過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應(yīng)值都是一個字符串。在spring中可以用其存儲連接數(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容器對象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.從容器中獲取Bean實例
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.toString());
}
}
輸出:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 基于注解的 Redis 緩存使用詳解
本篇文章主要介紹了Spring Boot 基于注解的 Redis 緩存使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
JAVA代碼實現(xiàn)MongoDB動態(tài)條件之分頁查詢
這篇文章主要介紹了JAVA如何實現(xiàn)MongoDB動態(tài)條件之分頁查詢,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下2020-07-07

