Spring Boot中属性的获取

Spring Boot要从application.yml(application.properties)中获取变量的值,比如application.properties中有下面的属性:
huahua=lili
1.那么要获得这个属性,有以下几种方式
1.1、通过context获取
@SpringBootApplication
public class SpringBootConfigApplication {
public static void main(String[] args) {
ConfigurableApplicationContext application =
SpringApplication.run(
SpringBootConfigApplication.class, args);
System.out.println(application.getEnvironment()
.getProperty(“huahua”));
application.close();
}
}
1.2、通过注入Environment对象获取
@Component
public class UserConfig {
@Autowired
private Environment env;

	    public void show(){
	        System.out.println("name = " + env.getProperty("huahua"));
	    }
	}
	
	@SpringBootApplication
	public class SpringBootConfigApplication {
	    public static void main(String[] args) {
	        ConfigurableApplicationContext application = 
                SpringApplication.run(
                SpringBootConfigApplication.class, args);
        application.getBean(UserConfig.class).show();
        application.close();
    }
}												
		通过env.getProperty()方法可以获取一个整数,比如huahua属性是一个整数。那么可以通过evn的重载方法getProperty(String prop, Class clazz)获取。
		System.out.println(env.getProperty("huahua", Integer.class));

1.3、通过@Value()注解
@Component
public class UserConfig {

    @Value("${huahua}")
    private String huahua;

    public void show(){
        System.out.println("name = " + huahua);
    }
}
SpringBootConfigApplication类保持不变。然后运行它,仍然可以达到统一的效果。
需要注意的是如果通过@Value注解的形式注解一个没有的属性,那么是会启动报错的,而通过Environment对象获取一个没有的属性,会返回null。

1.4、属性的默认值
比如我们的classpath下没有host的这个属性,那么我们运行下面的代码
@SpringBootApplication
public class SpringBootConfigApplication {
    @Value("${host:com.dragon}")
    private String host;
    public static void main(String[] args) {
        ConfigurableApplicationContext application = 
            SpringApplication.run(
            SpringBootConfigApplication.class, args);
        System.out.println(application.getEnvironment()
            .getProperty("host"));
        System.out.println(application.getBean(
            SpringBootConfigApplication.class).host);
        application.close();
    }
}

在上面的示例中,我们将@Value注解了一个默认值。同样Environment对象也是可以有默认值的。看下面的代码

@SpringBootApplication
public class SpringBootConfigApplication {
    @Value("${host:com.dragon}")
    private String host;
    public static void main(String[] args) {
        ConfigurableApplicationContext application = SpringApplication.run(SpringBootConfigApplication.class, args);
        System.out.println(application.getEnvironment().getProperty("host","dragon.com.cn"));
        System.out.println(application.getBean(SpringBootConfigApplication.class).host);
        application.close();
    }
}

除了上面的2种设置默认值,我们还有下面的方式可以设置默认值

SpringApplication application = new SpringApplication(SpringBootConfigApplication.class);
Map<String, Object> defaultProperties = new HashMap<>();
defaultProperties.put("host", "192.168.1.100");
application.setDefaultProperties(defaultProperties);
然后我们来看启动类

@SpringBootApplication
public class SpringBootConfigApplication {

    @Value("${host:com.dragon}")
    private String host;

    public static void main(String[] args) {    
        SpringApplication application = new SpringApplication(SpringBootConfigApplication.class);
        Map<String, Object> defaultProperties = new HashMap<>();
            defaultProperties.put("host", "192.168.1.100");
            application.setDefaultProperties(defaultProperties);

            ConfigurableApplicationContext context = application.run(args);
        System.out.println(context.getEnvironment().getProperty("host","dragon.com.cn"));
        System.out.println(context.getBean(SpringBootConfigApplication.class).host);    
        context.close();
    }
}

运行该类,其打印如下

也就是说通过application.setDefaultProperties()设置的方式,优先级最高:
获取属性,但是指定默认值,有3种方式:

a、@Value("${host:com.dragon}")
b、context.getEnvironment().getProperty("host","dragon.com.cn")
SpringApplication application = new SpringApplication(SpringBootConfigApplication.class);
Map<String, Object> defaultProperties = new HashMap<>();
defaultProperties.put("host", "192.168.1.100");
application.setDefaultProperties(defaultProperties);
1.5、配置文件里面获取配置项
配置文件里面除了配置普通的外,还可以使用当前配置的文件。如application.yml有如下配置:

propera: a
properb: 20
content: "propera: ${propera}, properb: ${properb}"
那么content的值就是 propera: a, properb: 20
发布了54 篇原创文章 · 获赞 21 · 访问量 3082
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览