spring属性赋值——@PropertySource加载外部配置文件示例

1、创建一个maven项目(创建maven项目过程省略),pom.xml文件引入如下依赖:

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>


2、创建Employee实体类,代码如下:

package com.rf.bean;

import org.springframework.beans.factory.annotation.Value;

public class Employee {
	//使用@Value赋值;
	//1、基本数值
	//2、可以写SpEL; #{}
	//3、可以写${};取出配置文件【properties】中的值(在运行环境变量里面的值)
	@Value("${employee.id}")//取出配置文件【properties】中的值
	private Integer id;
	@Value("张三")
	private String name;
	@Value("#{23-3}")
	private Integer age;
	
	private Integer did; //部门id
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public Integer getDid() {
		return did;
	}
	public void setDid(Integer did) {
		this.did = did;
	}
	
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", did=" + did + "]";
	}

}

3、创建employee.properties配置文件

employee.id=12

4、创建bean.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-5.2.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-5.2.xsd">
    <!--指定配置文件位置-->
  	<context:property-placeholder location="classpath:employee.properties"/>

</beans>

5、编写配置类,代码如下:

package com.rf.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.rf.bean.Employee;

//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;
//加载完外部的配置文件以后使用${}取出配置文件的值
@PropertySource(value="classpath:employee.properties") 
//标记这是一个配置类
@Configuration 
public class MyConfig {
	
	@Bean
	public Employee employee(){
		return new Employee();
	}
}

6、编写测试类,代码如下:

package com.rf.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.rf.config.MyConfig;

public class Test1 {
	
	@org.junit.Test	
	public void test1(){
		
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class);
		System.out.println("ioc容器创建完成了。。。。");
		Object bean = ctx.getBean("employee");
		System.out.println(bean);
		//关闭容器执行销毁方法 
		ctx.close();
	}
}

spring属性赋值——@PropertySource加载外部配置文件示例_第1张图片

你可能感兴趣的:(spring)