spring boot 注入 property的三种方式(推荐)

作者:mrr 时间:2023-01-23 05:10:27 

以前使用spring的使用要注入property要配置PropertyPlaceholder的bean对象。在springboot除  了这种方式以外还可以通过制定 配置ConfigurationProperties直接把property文件的 属性映射到 当前类里面。


@ConfigurationProperties(prefix = "mypro", merge = true, locations = { "classpath:my.properties" })


ConfigurationProperties prefix 属性指示property文件中属性的前缀是什么。我这里写的是mypro。


因此property文件的属性必须mypro.x.y=z的形式;


     配置好ConfigurationProperties 之后就可以把property文件的属性映射到当前类了。




mypro.a:1
mypro.b:2
abc.d:123

property 文件里面mypro前缀的有a 和b两个。因此我在当前类就可以新建这两个属性。


private int a;
private int b;

这些需要映射的属性一定要加上getter 和setter。因为spring是通过反射调用方法来修改属性值的

        以前使用spring注入property的方式也同样适用。以前是xml配置PropertyPlaceholder。现在使用@bean 或者直接@Component配置这个类。只要把PropertyPlaceholderConfigurer添加到bean工厂,就可以使用@Value 取值了。


@Component
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
public MyPropertyPlaceholderConfigurer(){
this.setIgnoreResourceNotFound(true);
  final List<Resource> resourceLst = new ArrayList<Resource>();
  resourceLst.add(new ClassPathResource("my.properties"));
  this.setLocations(resourceLst.toArray(new Resource[]{}));
}
}
@Value("abc.d")
private String test;

        另外的一种方法跟第二种差不多的。更像以前的xml配置PropertyPlaceholder。只是现在的配置是用@Configuration标注的类,用@Bean标注要配置的bean对象;


@Configuration
public class Testproperties {
@Bean
public PropertyPlaceholderConfigurer properties(){

final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
  ppc.setIgnoreResourceNotFound(true);
  final List<Resource> resourceLst = new ArrayList<Resource>();
  resourceLst.add(new ClassPathResource("my.properties"));
  ppc.setLocations(resourceLst.toArray(new Resource[]{}));
  return ppc;
}
}

以上所述是小编给大家介绍的spring boot 注入 property的三种方式网站的支持!

来源:https://my.oschina.net/u/778875/blog/1359216

标签:spring,boot,property
0
投稿

猜你喜欢

  • netty pipeline中的inbound和outbound事件传播分析

    2023-08-27 06:57:00
  • 【Java IO流】字节流和字符流的实例讲解

    2023-08-08 20:45:58
  • JFinal实现伪静态的方法

    2023-07-17 12:11:37
  • Spring Boot自动注入的原理分析

    2023-03-29 04:19:44
  • Android开发之android_gps定位服务简单实现

    2023-07-31 20:02:25
  • IDEA实现添加 前进后退 到工具栏的操作

    2021-08-30 21:34:48
  • springboot 使用自定义的aspect的示例代码

    2023-08-06 08:55:14
  • java多线程通过CompletableFuture组装异步计算单元

    2023-07-19 10:15:42
  • Java Callable接口实现细节详解

    2023-11-10 05:34:26
  • java 发送http和https请求的实例

    2023-11-29 12:46:52
  • Java框架---Spring详解

    2021-07-09 14:27:30
  • Java Set集合及其子类HashSet与LinkedHashSet详解

    2023-11-26 11:39:35
  • 解决问题:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

    2023-11-23 23:40:39
  • java实现学生信息管理系统

    2023-11-11 08:22:35
  • MyBatis字段名和属性名不一致的解决方法

    2022-12-15 18:15:22
  • C++程序中启动线程的方法

    2023-06-28 03:35:02
  • Java语言实现Blowfish加密算法完整代码分享

    2023-11-02 21:30:07
  • 完整的iOS新浪微博分享功能开发

    2023-06-24 14:14:08
  • 老生常谈Java 网络编程 —— Socket 详解

    2023-07-12 16:32:54
  • 解决grails服务端口冲突的办法(grails修改端口号)

    2023-09-12 01:00:03
  • asp之家 软件编程 m.aspxhome.com