Spring笔记---如何向bean注入Properties

1 新建一个类DataSource:

package com.gm.spring_test;

import java.util.Properties;

public class DataSource {
private Properties properties;

@Override
public String toString() {
return "Properties [properties=" + properties + "]";
}

public Properties getProperties() {
return properties;
}

public void setProperties(Properties properties) {
this.properties = properties;
}

}

2 修改配置文件spring-conf.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.gm.spring_test.DataSource">
<property name="properties">
<props>
<prop key="数学">20</prop>
<prop key="语文">21</prop>
<prop key="英语">22</prop>
</props>
</property>
</bean>
</beans>


3 使用bean:
ApplicationContext context = new ClassPathXmlApplicationContext("spring-conf.xml");
DataSource dataSource = (DataSource) context.getBean("DataSource");
System.out.println(dataSource);

————————————————
版权声明:本文为CSDN博主「gongmin123456789」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37825799/article/details/78904557

posted @ 2019-11-08 10:22  深水是沉默  阅读(1165)  评论(0编辑  收藏  举报