当前位置: 首页 > news >正文

网站设计的布局制作链接的小程序

网站设计的布局,制作链接的小程序,口碑好的vi设计公司,优秀的网站首页Spring的后处理器(Spring核心重点) Spring的后处理器是Spring对外开发的重要扩展点,允许我们介入到Bean的整个实例化流程中来,以达到动态注册BeanDefinition,动态修改BeanDefinition,以及动态修改Bean的作用。Spring主要有两种后处理器: ​ BeanFactoryPostProcessor: Bean工…

Spring的后处理器(Spring核心重点)

Spring的后处理器是Spring对外开发的重要扩展点,允许我们介入到Bean的整个实例化流程中来,以达到动态注册BeanDefinition,动态修改BeanDefinition,以及动态修改Bean的作用。Spring主要有两种后处理器:

​ · BeanFactoryPostProcessor: Bean工厂后处理器,在BeanDefinitionMap填充完毕,Bean实例化之前执行

​ · BeanPostProcessor: Bean后处理器,一般在Bean实例化之后,填充到单例池singletonObjects之前执行。

Bean工厂后处理器 – BeanFactoryPostProcessor

BeanFactoryPostProcessor是一个接口规范,实现了该接口的类只要交由Spring容器管理的话,那么Spring就会回调该接口的方法,用于对BeanDefinition注册和修改的功能。

BeanFactoryPostProcessor 定义如:

public interface BeanFactoryPostProcessor {void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory);
}

编写BeanFactoryPostProcessor

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException{System.out.println("MyBeanFactoryPostProcessor执行了...");}
}

配置BeanFactoryPostProcessor

<bean class="com.itheima.processor.MyBeanFactoryPostProcessor"/>

postProcessBeanFactory 参数本质就是 DefaultListableBeanFactory,拿到BeanFactory的引用,自然就可以对beanDefinitionMap中的BeanDefinition进行操作了 ,例如对UserDaoImpl的BeanDefinition进行修改操作

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {BeanDefinition userDaoBD = beanFactory.getBeanDefinition(“userDao”);//获得UserDao定义对象userDaoBD.setBeanClassName("com.itheima.dao.impl.UserDaoImpl2"); //修改class//userDaoBD.setInitMethodName(methodName); //修改初始化方法//userDaoBD.setLazyInit(true); //修改是否懒加载//... 省略其他的设置方式 ...}
}

上面已经对指定的BeanDefinition进行了修改操作,下面对BeanDefiition进行注册操作

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {//强转成子类DefaultListableBeanFactoryif(configurableListableBeanFactory instanceof DefaultListableBeanFactory){DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory;BeanDefinition beanDefinition = new RootBeanDefinition();beanDefinition.setBeanClassName("com.itheima.dao.UserDaoImpl2");//进行注册操作beanFactory.registerBeanDefinition("userDao2",beanDefinition);}}
}

Spring 提供了一个BeanFactoryPostProcessor的子接口BeanDefinitionRegistryPostProcessor专门用于注册BeanDefinition操作

public class MyBeanFactoryPostProcessor2 implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {}@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {BeanDefinition beanDefinition = new RootBeanDefinition();beanDefinition.setBeanClassName("com.itheima.dao.UserDaoImpl2");beanDefinitionRegistry.registerBeanDefinition("userDao2",beanDefinition);}
}

使用Spring的BeanFactoryPostProcessor扩展点完成自定义注解扫描

1.自定义@MyComponent注解,使用在类上

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyComponent {//显示的指定Bean的beanNameString value() default "";
}

2.在类上使用@MyComponent

@MyComponent("otherBean")
public class OtherBean {
}

3.自定义BeanFactoryPostProcessor完成注解解析

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {//指定要扫描的包String basePackage = "com.itheima";//调用扫描工具扫描指定包及其子包下的@MyComponentMap<String, Class> myComponentClassMap = BaseClassScanUtils.scanMyComponentAnnotation(basePackage);//遍历Map集合,创建BeanDefinition对象进行注册myComponentClassMap.forEach((beanName,clazz)->{try {BeanDefinition beanDefinition = new RootBeanDefinition();beanDefinition.setBeanClassName(clazz.getName());registry.registerBeanDefinition(beanName,beanDefinition);} catch (Exception e) {e.printStackTrace();}});}

BeanFactoryPostProcessor 在SpringBean的实例化过程中的体现

在这里插入图片描述

Bean后处理器 – BeanPostProcessor

Bean被实例化后,到最终缓存到名为singletonObjects单例池之前,中间会经过Bean的初始化过程,例如:属性的填充、初始方法init的执行等,其中有一个对外进行扩展的点BeanPostProcessor,我们称为Bean后处理。跟上面的Bean工厂后处理器相似,它也是一个接口,实现了该接口并被容器管理的BeanPostProcessor,会在流程节点上被Spring自动调用。

BeanPostProcessor的接口定义如下:

public interface BeanPostProcessor {@Nullable//在属性注入完毕,init初始化方法执行之前被回调default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullable//在初始化方法执行之后,被添加到单例池singletonObjects之前被回调default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}
}

自定义MyBeanPostProcessor,完成快速入门测试

public class MyBeanPostProcessor implements BeanPostProcessor {/* 参数: bean是当前被实例化的Bean,beanName是当前Bean实例在容器中的名称
返回值:当前Bean实例对象 */public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("BeanPostProcessor的before方法...");return bean;}/* 参数: bean是当前被实例化的Bean,beanName是当前Bean实例在容器中的名称
返回值:当前Bean实例对象 */public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("BeanPostProcessor的after方法...");return bean;}
}

配置MyBeanPostProcessor

<bean class="com.itheima.processors.MyBeanPostProcessor"></bean>

测试控制台打印结果如下:

UserDaoImpl创建了...
UserDaoImpl属性填充...
BeanPostProcessor的before方法...
UserDaoImpl初始化方法执行...
BeanPostProcessor的after方法...

编写BeanPostProcessor,增强逻辑编写在 after方法中

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {//对Bean进行动态代理,返回的是Proxy代理对象Object proxyBean = Proxy.newProxyInstance(bean.getClass().getClassLoader(),bean.getClass().getInterfaces(),(Object proxy, Method method, Object[] args) -> {long start = System.currentTimeMillis();System.out.println("开始时间:" + new Date(start));//执行目标方法Object result = method.invoke(bean, args);long end = System.currentTimeMillis();System.out.println("结束时间:" + new Date(end));return result;});//返回代理对象return proxyBean;
}

对Bean方法进行执行时间日志增强

编写BeanPostProcessor,增强逻辑编写在 after方法中

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {//对Bean进行动态代理,返回的是Proxy代理对象Object proxyBean = Proxy.newProxyInstance(bean.getClass().getClassLoader(),bean.getClass().getInterfaces(),(Object proxy, Method method, Object[] args) -> {long start = System.currentTimeMillis();System.out.println("开始时间:" + new Date(start));//执行目标方法Object result = method.invoke(bean, args);long end = System.currentTimeMillis();System.out.println("结束时间:" + new Date(end));return result;});//返回代理对象return proxyBean;
}

BeanPostProcessor 在 SpringBean的实例化过程中的体现

在这里插入图片描述

http://www.ritt.cn/news/25747.html

相关文章:

  • b2c购物网站有哪些中国营销网站
  • 模板素材库百度seo关键词优化市场
  • 一般网站设计多大宽度微商引流的最快方法是什么
  • 在建设银行网站上买卖贵金属广州市最新消息
  • 沈阳网站制作思路b站推广网站2023
  • 柳州专业网站优化seo专业培训学费多少钱
  • 山西建筑劳务网站bt种子搜索
  • 网站空间商查询seo推广有哪些公司
  • 国外平台卖货app优化方案
  • 网站运营新手做网络营销网站设计
  • 自己做网站要哪些东西新媒体
  • 网站的建设方向最佳磁力链ciliba
  • 建设网站如何弄好几张网站背景seo自动工具
  • 鸡西公司做网站北京网站维护公司
  • html做网站怎么链接音乐企业官网搭建
  • 海淀区住房城乡建设委房管局官方网站湖南网站定制
  • 做网站必须要有的素材国内最新新闻热点事件
  • 租空间做网站需要多少钱刷移动端seo软件
  • 企业微信小程序免费制作平台seo网站优化培训怎么样
  • 广州正规网站建设谷歌搜索引擎seo
  • dedecms美食网站杭州seo营销公司
  • 网站怎么建立视频网页关键词优化软件
  • 网站建设 团队介绍网络营销核心要素
  • 网站 猜你喜欢 怎么做关键词排名推广方法
  • 惠东做网站报价二维码引流推广的平台
  • 高端网站建设设计公司排名百度网站推广费用
  • cms那个做网站最好seo推广的网站和平台有哪些
  • 网站制作技术培训学校网站模板之家
  • jsp网站开发答辩教育培训网站
  • 虚拟主机网站建设步骤免费的网络推广渠道有哪些