提交 6d5d6593 编写于 作者: 武汉红喜's avatar 武汉红喜

PropertySourcesPlaceholderConfigurer read yaml

上级 2d3cde62
......@@ -56,6 +56,11 @@
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
......
package org.hongxi.whatsmars.spring.configurer;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
/**
* Created by shenhongxi on 2019/1/22.
*/
@ComponentScan(basePackages = "org.hongxi.whatsmars.spring.configurer")
@Configuration
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(Application.class);
ctx.refresh();
Config config = ctx.getBean(Config.class);
System.out.println(config.getName());
}
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application.yml"));
configurer.setProperties(yaml.getObject());
return configurer;
}
@Data
@Configuration
class Config {
@Value("${spring.application.name}")
private String name;
}
}
spring:
application:
name: whatsmars-spring
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册