属性配置方式
public static void main(String[] args) {
// SpringApplication.run(DemoApplication.class,args);
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
Properties properties = new Properties();
properties.setProperty("baidu","www.baidu.com");
springApplication.setDefaultProperties(properties);
springApplication.run(args);
/* SpringApplication springApplication = new SpringApplication(DemoApplication.class);
springApplication.addInitializers(new SecondInitializer());
springApplication.run(args);*/
}
Spring 框架优点:bean 感知不到容器的存在
使用场景:需要使用Spring 容器的 功能资源
引入缺点:bean 和容器强耦合
常用Aware
BeanNameAware 获得容器中bean名称
BeanClassLoaderAware 获得类加载器
BeanFactoryAware 获得bean 创建工厂
EnvironmentAware 获得环境变量
EmbeddedValueResolverAware 获取spring容器加载的 properties 文件属性值
ResourceLoaderAware 获得资源加载器
ApplicationEventPublisherAware 获取应用事件发布器
MessageSourceAware 或得文本信息
ApplicationContextAware 获得当前应用上下文
自定义Aware
public interface MyAware extends Aware {
void setFlag(Flag flag);
}
@Data
@Component
public class Flag {
private boolean canOperate = true;
}
public class MyAwareProcessor implements BeanPostProcessor {
private final ConfigurableApplicationContext configurableApplicationContext;
public MyAwareProcessor(ConfigurableApplicationContext configurableApplicationContext) {
this.configurableApplicationContext = configurableApplicationContext;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Aware){
if(bean instanceof MyAware){
((MyAware) bean).setFlag((Flag) configurableApplicationContext.getBean("flag"));
}
}
return bean;
}
}
@Component
public class ResultCommandLineRunner implements CommandLineRunner, EnvironmentAware,MyAware {
private Environment env;
private Flag flag;
@Override
public void run(String... args) throws Exception {
System.out.println(env.getProperty("baidu"));
}
@Override
public void setEnvironment(Environment environment) {
this.env = environment;
}
@Override
public void setFlag(Flag flag) {
this.flag = flag;
}
}
environment 如果初始化配置属性的
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 配置环境变量
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}