• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
深水是沉默
别自制压力,我们没有必要跟着时间走, 只需跟着心态和能力走 随缘 尽力 问心无愧,其他的,交给天。
博客园    首页    新随笔    联系   管理    订阅  订阅
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 on 2019-11-08 10:22  深水是沉默  阅读(1182)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3