Java 属性配置解析

一、属性配置介绍

17.默认属性配置方式

修改启动文件

package com.gf.springboot;

import com.gf.springboot.ioc.ann.MyBeanImport;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

import java.util.Properties;

@SpringBootApplication
@ComponentScan(basePackages = {"com.*.*"})
@Import(MyBeanImport.class)
public class SpringbootApplication {

    public static void main(String[] args) {
//        SpringApplication.run(SpringbootApplication.class, args);
        SpringApplication springApplication = new SpringApplication(SpringbootApplication.class);
        Properties properties = new Properties();
        properties.setProperty("cherish.website.url","cherish_url_1");
        springApplication.setDefaultProperties(properties);
        springApplication.run(args);
    }

}

新建文件ResultCommandLineRunner

package com.gf.springboot.pro;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class ResultCommandLineRunner implements CommandLineRunner, EnvironmentAware {

    private Environment env;
    @Override
    public void run(String... args) throws Exception {
        System.out.println(env.getProperty("cherish.website.url"));
    }

    @Override
    public void setEnvironment(Environment environment) {
        env = environment;
    }
}

结果

16.@PropertySource绑定配置

新建一个demo.properties配置文件

cherish.website.url=cherish_url_2

修改启动文件,添加@PropertySource注解

package com.gf.springboot;

import com.gf.springboot.ioc.ann.MyBeanImport;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

import java.util.Properties;

@SpringBootApplication
@ComponentScan(basePackages = {"com.*.*"})
@Import(MyBeanImport.class)
@PropertySource({"demo.properties"})
public class SpringbootApplication {

    public static void main(String[] args) {
//        SpringApplication.run(SpringbootApplication.class, args);
        SpringApplication springApplication = new SpringApplication(SpringbootApplication.class);
        Properties properties = new Properties();
        properties.setProperty("cherish.website.url","cherish_url_1");
        springApplication.setDefaultProperties(properties);
        springApplication.run(args);
    }

}

结果

这里注解的优先级高于默认配置

14.15.application.properties优先级高于application.yml

新建application.yml

cherish:
  website:
    url:
      cherish_url_3

运行结果

在application.properties文件中添加配置

运行结果

由此application.properties优先级高于application.yml

12.13application-default

新建application-default.yml

cherish:
  website:
    url:
      cherish_url_5

运行结果

新建application-default.properties文件中添加配置

cherish.website.url=cherish_url_6

运行结果

 

二、Spring Aware介绍

Spring框架的优点:Bean感知不到容器的存在

使用场景:需要使用Spring容器的功能资源

缺点:容器和bean进行强耦合

三、Environment解析

四、Spring profile介绍

五、Spring profile解析

六、总结

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值