Spring基础:快速入门spring(6):properties文件方式的值注入

上一篇文章里我们学习了普通的值得注入, 在spring的配置文件里设定value即可轻松的将值注入到程序当中,但是这种方式依然Hardcoding的感觉太强,在这篇文章中我们将学习如何使用property文件进行注入值的操作。

这里写图片描述

定义注入对象的get/set方法

向Student类里面加入name和country两个字段并生成get/set方法(Eclipse右键菜单/IntelliJ的Alt+Insert均可自动生成)

package com.liumiao.demo.spring;

public class Student implements Person {
    private String name;
    private String country;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("Student: set method: SetName:"+name);
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        System.out.println("Student: set method: SetCountry:"+country);
        this.country = country;
    }

    private TeachingService teachingService;
    public Student(){
        System.out.println("Student Default construct is called...");
    }
    public void setTeachingService(TeachingService service){
        teachingService=service;
    }
    @Override
    public String sayhello(){
        return "Hello, I am a student.";
    }
    @Override
    public String provideTeachingService(){
        return "I teach how to study...";
    }
}

设定property文件

设定person.properties文件,路径和spring的配置文件同一目录,详细内容如下
person.name=liumiao.cn
person.country=ChinaPRC

配置spring设定文件

如下修改配置文件, 注意如下事项

注意事项详细
No.1context:property-placeholder装载properties文件
No.2使用${key名称}方式设到value中取代Hardcoding
No.3key名称需要与properties文件中的键值对相匹配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="classpath:com/liumiao/demo/spring/Person.properties" />
    <bean id="thePerson" class="com.liumiao.demo.spring.Student">
        <property name="teachingService" ref="theService"></property>
        <property name="name" value="${person.name}" />
        <property name="country" value="${person.country}" />
    </bean>

    <bean id="theService" class="com.liumiao.demo.spring.SwimmingTeachingService">
    </bean>

</beans>

修改TestDemo

package com.liumiao.demo.spring;

import com.liumiao.demo.spring.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    public TestDemo() {
    }

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/liumiao/demo/spring/spring-cfg.xml");
        Student person = (Student) context.getBean("thePerson", Person.class);
        System.out.println(person.sayhello());
        System.out.println(person.provideTeachingService());
        System.out.println(person.getCountry());
        System.out.println(person.getName());
        context.close();
    }
}

执行结果

TestDemo无需修改可以直接确认结果。

十一月 26, 2016 9:59:57 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sat Nov 26 21:59:57 CST 2016]; root of context hierarchy
十一月 26, 2016 9:59:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/liumiao/demo/spring/spring-cfg.xml]
十一月 26, 2016 9:59:58 下午 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
信息: Loading properties file from class path resource [com/liumiao/demo/spring/Person.properties]
Student Default construct is called...
Student: set method: SetName:liumiao.cn
Student: set method: SetCountry:ChinaPRC
Hello, I am a student.
十一月 26, 2016 9:59:58 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
I teach how to study...
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sat Nov 26 21:59:57 CST 2016]; root of context hierarchy
ChinaPRC
liumiao.cn

这样通过修改properties文件就可以动态修改注入的值了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值