多平台统一管理软件接口,如何实现多平台统一管理软件接口
301
2024-02-03
本文讲解"Spring aware接口的作用是什么",希望能够解决相关问题。
通过aware接口可以获取Spring容器相关信息,但这样会与Spring容器耦合。
执行顺序从上到下。
类名作用BeanNameAware获得容器中bean名称BeanClassLoaderAware获得类加载器BeanFactoryAware获得bean创建工厂EnvironmentAware获得环境变量EmbeddedValueResolverAware获取spring容器加载的properties文件属性值ResourceLoaderAware获得资源加载器ApplicationEventPublisherAware获得应用事件发布器MessageSourceAware获得文本信息ApplicationContextAware获得当前应用上下文先定义一个继承Aware的接口,然后注册一个实现BeanPostProcessor接口的Bean,在postProcessBeforeInitialization中处理Aware接口的逻辑。
举一个例子。获取调用自定义Aware接口方法的时间。
4.1 定义继承Aware的接口public interface TimeAware extends Aware { void setTriggerTime(Date date); }4.2 注册实现BeanPostProcessor接口的Bean@Component public class AwarePostProcessor implements BeanPostProcessor { private final ConfigurableApplicationContext applicationContext; /** * 可写可不写,这个构造是为了获取applicationContext */ public AwarePostProcessor(ConfigurableApplicationContext applicationContext) { this.applicationContext = applicationContext; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof Aware) { if (bean instanceof TimeAware) { // 实现自定义Aware接口的逻辑,设置调用的时间 ((TimeAware)bean).setTriggerTime(new Date()); } } return bean; } }4.3 实现TimeAware接口,并测试@Import(AwarePostProcessor.class) public class Test implements TimeAware { Date date; @Override public void setTriggerTime(Date date) { this.date = date; } public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(Test.class); Test bean = context.getBean(Test.class); System.out.println(bean.date); } }结果:
1.先执行实现了下面三个aware接口的方法
BeanNameAwareBeanClassLoaderAwareBeanFactoryAware2.调用初始化方法
3.执行实现剩下aware接口的方法
5.1 初始化阶段的源码逻辑AbstractAutowireCapableBeanFactory#initializeBean
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareMethods(beanName, bean); return null; }, getAccessControlContext()); } else { /** * 调用Bean实现的Aware接口的方法,主要包括下面三个接口 * BeanNameAware ----> setBeanName() * BeanClassLoaderAware ----> setBeanClassLoader() * BeanFactoryAware ----> setBeanFactory() */ invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { /** 调用Bean对象的postProcessBeforeInitialization方法,此处会执行标注@PostConstruct注解的方法 */ // 此处会调用ApplicationContextAwareProcessor执行其他的aware方法. wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { /** * 执行Bean的初始化方法: * * 1.先判断Bean是否实现了InitializingBean接口,如果实现了InitializingBean接口,则调用Bean对象的afterPropertiesSet方法; * 2.然后判断Bean是否有指定init-method方法,如果指定了init-method方法,则调用bean对象的init-method指定的方法. */ invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { /** * 调用Bean对象的postProcessAfterInitialization方法 * * 如果需要创建代理,在该步骤中执行postProcessAfterInitialization方法的时候会去创建代理 * 调用AbstractAutoProxyCreator类的postProcessAfterInitialization方法,然后调用wrapIfNecessary方法去创建代理. * * * 另外还有一些Aware接口,也会在该步骤中执行,例如:ApplicationContextAwareProcessor后置处理器,对应的setApplicationContext方法会被执行. */ wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }5.2 实现前三个aware接口的处理调用initializeBean =>invokeAwareMethods
方法将前三个aware方法调用
AbstractAutowireCapableBeanFactory#invokeAwareMethodsprivate void invokeAwareMethods(String beanName, Object bean) { if (bean instanceof Aware) { if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } if (bean instanceof BeanClassLoaderAware) { ClassLoader bcl = getBeanClassLoader(); if (bcl != null) { ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl); } } if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this); } } }5.3 剩余实现aware接口的Bean的处理调用initializeBean =>applyBeanPostProcessorsBeforeInitialization=>BeanPostProcessor.postProcessBeforeInitialization进入ApplicationContextAwareProcessor#postProcessBeforeInitialization=>invokeAwareInterfaces
private void invokeAwareInterfaces(Object bean) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } }因此可以自定义aware接口,并且注册一个实现BeanPostProcessor的postProcessBeforeInitialization方法的Bean,处理调用aware方法时的处理逻辑。
怎么用Java绘制迷宫动画并显示:本文讲解"如何用Java绘制迷宫动画并显示",希望能够解决相关问题。一次性全部绘制出来实现代码import java.awt.*; public class AlgoVisualizer { pri ...
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~