300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Spring Boot @PropertySource 加载配置文件 @ImportResource 导入Spring 配置文件

Spring Boot @PropertySource 加载配置文件 @ImportResource 导入Spring 配置文件

时间:2018-09-16 12:56:40

相关推荐

Spring Boot @PropertySource 加载配置文件 @ImportResource 导入Spring 配置文件

目录

@PropertySource 加载 properties 配置文件

@PropertySource 加载 yml 配置文件

@PropertySource 从本地磁盘加载文件

@ImportResource 导入Spring 配置文件

@PropertySource 加载 properties 配置文件

1、通过《Spring Boot @ConfigurationProperties 、@Value 注值》知道使用“@Value”与“@ConfigurationProperties”可以从全局配置文件“application.properties”或者“application.yml”中取值,然后为需要的属性赋值。

2、当应用比较大的时候,如果所有的内容都当在一个配置文件中,就会显得比较臃肿,同时也不太好理解和维护,此时可以将一个文件拆分为多个,使用 @PropertySource 注解加载指定的配置文件,注解常用属性如下:

3、下面演示使用 @PropertySource 注解 加载类路径下的 user.properties 配置文件,为 User.java POJO 对象的属性赋值。

/*** 用户···实体* {@code @Component} 将本来标识为一个 Spring 组件,因为只有是容器中的组件,容器才会为 @ConfigurationProperties 提供此注入功能* {@code @PropertySource} 指明加载类路径下的哪个配置文件来注入值* {@code @ConfigurationProperties} 表示 告诉 SpringBoot 将本类中的所有属性和配置文件中相关的配置进行绑定;* * prefix = "user1" 表示 将配置文件中 key 为 user 的下面所有的属性与本类属性进行一一映射注入值,如果配置文件中-* * 不存在 "user1" 的 key,则不会为 POJO 注入值,属性值仍然为默认值** @author wangmaoxiong* Created by Administrator on /7/11 0011.*/@Component@PropertySource(value = {"classpath:user.properties"})@ConfigurationProperties(prefix = "user1")public class UserProperty {private Integer id;private String lastName;private Integer age;private Date birthday;private List<String> colorList;private Map<String, String> cityMap;private Dog dog;

src/main/java/com/wmx/yuanyuan/pojo/UserProperty.java · 汪少棠/yuanyuan -

/*** 狗 实体类-----------普通 Java Bean* 被依赖项可以不加 @ConfigurationProperties 注解,但是必须提供 getter、setter 方法** @author wangmaoxiong* Created by Administrator on /7/11 0011.*/public class Dog {private Integer id;private String name;private Integer age;

src/main/java/com/wmx/yuanyuan/pojo/Dog.java · 汪少棠/yuanyuan -

4、在类路径下提供 user.properties 配置文件如下(.yml 文件也是同理):

user1.id=112user1.lastName=\u5F20\u65E0\u5FCCuser1.age=110user1.birthday=/07/12user1.colorList=red,yellow,green,blacnkuser1.cityMap.mapK1=\u957F\u6C99\u5E02user1.cityMap.mapK2=\u6DF1\u5733\u5E02user1.dog.id=7523user1.dog.name=\u72D7\u4E0D\u7406user1.dog.age=3

src/main/resources/user.properties · 汪少棠/yuanyuan -

5、测试运行:src/test/java/com/wmx/yuanyuan/PropertySourceTest.java · 汪少棠/yuanyuan -

@PropertySource 加载 yml 配置文件

1、仍然以为实体对象注入属性值为例进行演示:

/*** 用户···实体* {@code @Component} 将本来标识为一个 Spring 组件,因为只有是容器中的组件,容器才会为 @ConfigurationProperties 提供此注入功能* {@code @PropertySource} 指明加载类路径下的哪个配置文件来注入值* * factory:用于指定 @PropertySource 加载 yml 配置文件.* {@code @ConfigurationProperties} 表示 告诉 SpringBoot 将本类中的所有属性和配置文件中相关的配置进行绑定;* * prefix = "user" 表示 将配置文件中 key 为 user 的下面所有的属性与本类属性进行一一映射注入值,如果配置文件中-* * 不存在 "user" 的 key,则不会为 POJO 注入值,属性值仍然为默认值** @author wangmaoxiong* Created by Administrator on /7/11 0011.*/@Component@PropertySource(value = {"classpath:user.yml"}, factory = PropertySourceFactory.class)@ConfigurationProperties(prefix = "user")public class UserYml {private Integer id;private String lastName;private Integer age;private Date birthday;private List<String> colorList;private Map<String, String> cityMap;private Dog dog;//......}

src/main/java/com/wmx/yuanyuan/pojo/UserYml.java · 汪少棠/yuanyuan -

2、然后提供用于处理 yml 配置文件的属性资源工程如下:

import org.springframework.boot.env.YamlPropertySourceLoader;import org.springframework.core.env.PropertySource;import org.springframework.core.io.support.DefaultPropertySourceFactory;import org.springframework.core.io.support.EncodedResource;import java.io.IOException;import java.util.List;/*** 用于 @PropertySource 加载 yml 配置文件.** @author wangmaoxiong* @version 1.0* @date /5/25 20:45*/public class PropertySourceFactory extends DefaultPropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {if (resource == null) {return super.createPropertySource(name, resource);}List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());return sources.get(0);}}

src/main/java/com/wmx/yuanyuan/factory/PropertySourceFactory.java · 汪少棠/yuanyuan -

测试运行:src/test/java/com/wmx/yuanyuan/PropertySourceTest.java · 汪少棠/yuanyuan -

@PropertySource 从本地磁盘加载文件

1、PropertySource 不仅支持加载类路径下的文件,还支持加载本地磁盘上的文件。

/*** 用户···实体————读取本地磁盘上的配置文件* * @Component 将本来标识为一个 Spring 组件,因为只有是容器中的组件,容器才会为 @ConfigurationProperties 提供此注入功能* * @PropertySource 指明加载类路径下的哪个配置文件来注入值,既可以是类路径下,也可以上磁盘上* * @ConfigurationProperties 表示 告诉 SpringBoot 将本类中的所有属性和配置文件中相关的配置进行绑定;* * * prefix = "user1" 表示 将配置文件中 key 为 user 的下面所有的属性与本类属性进行一一映射注入值,如果配置文件中-* * * 不存在 "user1" 的 key,则不会为 POJO 注入值,属性值仍然为默认值* <p>* 磁盘路径可以是相对路径,绝对路径,也可以通过系统属性值指定变量* * 相对路径,文件在应用根目录下:@PropertySource(value = {"file:userDisk.properties"})* * 相对路径,文件在应用根目录下:@PropertySource(value = {"file:./userDisk.properties"})* * 绝对路径,在指定的路径下:@PropertySource(value = {"file:D:\\project\\IDEA_project\\yuanyuan\\userDisk.properties"})* * 通过系统属性值指定变量:@PropertySource(value = {"file:${user.dir}/userDisk.properties"})* * * user.dir:用户的当前工作目录** @author wangmaoxiong* Created by Administrator on /7/11 0011.*/@Component@PropertySource(value = {"file:${user.dir}/userDisk.properties"})@ConfigurationProperties(prefix = "user2")public class UserDisk {private Integer id;private String lastName;private Integer age;private Date birthday;private List<String> colorList;private Map<String, String> cityMap;private Dog dog;

src/main/java/com/wmx/yuanyuan/pojo/UserDisk.java · 汪少棠/yuanyuan -

磁盘文件(其它 yml、xml格式也是同理):userDisk.properties · 汪少棠/yuanyuan -

测试类:src/test/java/com/wmx/yuanyuan/PropertySourceTest.java · 汪少棠/yuanyuan -

@ImportResource 导入Spring 配置文件

1、@ImportResource 注解用来导入 Spring 的配置文件,如核心配置文件 "beans.xml",从而让配置文件里面的内容生效;

2、如果应用中仍然想采用以前 xml 文件的配置方式,如 "beans.xml" ,则使用 “@ImportResource” 注解轻松搞定。

3、将 @ImportResource 标注在一个配置类上,通常直接放置在应用启动类上,和 @SpringBootApplication 一起即可。

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ImportResource;/*** 应用启动类** @ImportResource 必须放置在配置类上,通常放在启动类即可,用 value 指明导入类路径下的那个 Spring 配置文件*/@ImportResource(value = {"classpath:beans.xml"})@SpringBootApplicationpublic class CocoApplication {public static void main(String[] args) {SpringApplication.run(CocoApplication.class, args);}}

4、然后就可以在类路径下提供原始的 beans.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd"><!-- 放入到Sping容器中,这是以前Spring的内容,不再累述--><bean id="userService" class="com.lct.service.UserService"/></beans>

5、启动应用控制台会打印:loading XML bean definitions from class path resource [beans. xml] 表示加载成功。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。