一、属性配置介绍
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解析
六、总结