Spring 通过配置文件注入 properties文件

当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷

1. spring 配置文件需要导入

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans 
3  xmlns:util="http://www.springframework.org/schema/util"
4  http://www.springframework.org/schema/util 
5  http://www.springframework.org/schema/util/spring-util-3.1.xsd">
6     

2.配置文件路径

<util:properties id="exception" location="classpath:config/exceptiontype.properties"/>  

3.在使用的类添加属性注入

@Component
public class ExceptionConverter extends AbstractExceptionInterceptor {
    

    @Value("#{exception}")
    private Properties expt;
public void setKey(){ String code=expt.getProperty("这里是Properties文件的key"); } }

好啦,做个笔记!!!

好记性不如烂笔头

 

原文地址:https://www.cnblogs.com/kzhan/p/4980564.html