zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    670. Maximum Swap
    126. Word Ladder II
    695. Max Area of Island
    689. Maximum Sum of 3 Non-Overlapping Subarrays
    667. Beautiful Arrangement II
    前端开发-css
    前端开发-html
    mysql补充
    pythonl操作数据库
    mysql索引原理
  • 原文地址:https://www.cnblogs.com/xiaobug/p/11818530.html
Copyright © 2011-2022 走看看