Spring-IoC-DI-基于xml的依赖注入-使用set方法进行注入(案例十四:注入Properties对象属性)

案例十四:注入Properties对象属性

(1)、创建对象

public class JdbcProperties {
    private Properties properties;

    public JdbcProperties(Properties properties) {
        this.properties = properties;
    }

    public JdbcProperties() {
    }

    public Properties getProperties() {
        return properties;
    }

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

    @Override
    public String toString() {
        return "JdbcProperties{" +
                "properties=" + properties +
                '}';
    }
}

(2)、配置文件

<bean id="jdbc" class="com.orz.spring.bean.JdbcProperties">
    <property name="properties">
        <props>
            <prop key="username">root</prop>
            <prop key="password">2020</prop>
        </props>
    </property>
</bean>

(3)、测试

@Test
public void test1()
{
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
    JdbcProperties jdbc = applicationContext.getBean("jdbc", JdbcProperties.class);
    System.out.println(jdbc);
}

(4)、结果

JdbcProperties{properties={password=2020, username=root}}

 

 
posted @ 2020-10-19 08:45  orz江小鱼  阅读(148)  评论(0编辑  收藏  举报