springboot 源码(六)属性配置介绍spring aware 介绍(感知类)

属性配置方式

	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;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值