• 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

  • 相关阅读:
    git将已存在的项目转换成git项目&托管git服务器
    联合索引
    MyISAM和InnoDb的关系
    NPOIHelper
    C# CRC各种转换
    appcloud 微信分享大图片
    C# 微信JSSDK 获取配置信息
    简易delegate委托
    GPS、谷歌、百度、高德坐标相互转换
    反射执行方法WINFROM
  • 原文地址:https://www.cnblogs.com/xiaobug/p/11818530.html
Copyright © 2020-2023  润新知