java通过spring获取配置文件_Spring读取配置文件的方式总结

一、基于XML配置的方式

1.使用 PropertyPlaceholderConfigurer

- 在 applicationContext.xml 中配置:

或者:

- 之后就可以在代码中访问了:

@Componentpublic classTestComponent {

@Value("${jdbc.url}")privateString url;

}

2.使用 PropertiesFactoryBean

- 注册 bean

classpath*:db.properties

- 使用

privateString url;

@Resource(name= "dbProperties")privateProperties properties;

@PostConstructpublic voidinit() {

url= properties.getProperty("jdbc.url");

}

3.使用 ResourceBundleMessageSource

- 注册 bean

classpath:db.properties

- 访问

可以使用如下方法来访问:

((ApplicationContext)context).getMessage("jdbc.url", null, null);

或者:

@Componentpublic classBeanTester {

@Autowired

private MessageSource messageSource;public voidexecute(){

String url= this.messageSource.getMessage("jdbc.url", null, null);

}

}

二、基于Java配置的方式

1.使用 PropertySource

1.1通过 Environment 来获取

@Configuration//另有 PropertySources 包含多个 @PropertySource 来配置多个配置文件

@PropertySource("classpath:db.properties")public classPropertySource {

@Autowired

Environment env;public voidexecute() {//你可以按照如下的方式获取属性值

String url = this.env.getProperty("jdbc.url");

}

}

1.2通过 PropertySourcesPlaceholderConfigurer 来获取

@Configuration

@PropertySource("classpath:db.properties")public classPropertySource {

@Beanpublic staticPropertySourcesPlaceholderConfigurer placeHolderConfigurer() {return newPropertySourcesPlaceholderConfigurer();

}

}

调用:

@Componentpublic classTestComponent {

@Value("${jdbc.url}")privateString url;

}

2.使用 PropertiesFactoryBean

- 注册 bean:

@BeanpublicPropertiesFactoryBean propertiesFactoryBean() {

PropertiesFactoryBean propertiesFactoryBean= newPropertiesFactoryBean();//可以配置多个配置文件,也可以使用setLocation值设置单个配置文件

Resource[] resources = new ClassPathResource[]{new ClassPathResource("db.properties")};

propertiesFactoryBean.setLocations(resources);returnpropertiesFactoryBean;

}

- 使用:

@Componentpublic classBeanTester {privateString url;

@Resource(name= "propertiesFactoryBean")privateProperties properties;

@PostConstructpublic voidinit() {//取到jdbc.url对应的值并赋给了全局变量url

url = properties.getProperty("jdbc.url");

}

}

三、SpringBoot

1. application.xml (或.yml)中有如下配置内容:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test

1.1 可以直接通过 @Value 注解获取:

@SpringBootApplication

@RestControllerpublic classApplaction {

@Value("${spring.datasource.url}")privateString url;

}

1.2可以通过 Environment 来获取:

@SpringBootApplication

@RestControllerpublic classApplaction {

@AutowiredprivateEnvironment env;public voidtest() {//获取配置文件中的属性

env.getProperty("spring.datasource.url");

}

}

2.如果是用户自定义的配置文件,我们可以使用 @ConfigurationProperties 注解来获取:

如你在resources/resources下有一个配置文件 author.properties:

author.name=EricChan

author.sex=male

我们可以通过 @ConfigurationProperties 来将 properties 属性和一个 Bean 及其属性关联,从而实现类型安全的配置:

@Getter

@Setter

@ToString

@Component

@PropertySource(value= "classpath:resources/author.properties", encoding = "UTF-8")

@ConfigurationPropertiespublic classAuthor {privateString name;privateString sex;

}

之后我们就可以通过将该 Bean 注入到其他需要使用的地方就可以获取了,比如:

@RestControllerpublic classTestController {public finalAuthor author;//将该 bean 注入进来

public TestController(finalAuthor author) {this.author =author;

}public voidtest() {

System.out.println(author);

}

}

还有种通过注册监听器的方式可以来实现,实现 ApplicationListener,但觉得该方式比较麻烦,不太实用,在这里暂不做介绍,有兴趣的可以自己百度谷歌就行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值