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

教育网站 模板泰州外贸网站建设

教育网站 模板,泰州外贸网站建设,吐鲁番seo快速排名,盐城网站建设制作工作室正文 一个 bean 经历了 createBeanInstance() 被创建出来#xff0c;然后又经过一番属性注入#xff0c;依赖处理#xff0c;历经千辛万苦#xff0c;千锤百炼#xff0c;终于有点儿 bean 实例的样子#xff0c;能堪大任了#xff0c;只需要经历最后一步就破茧成蝶了。…正文 一个 bean 经历了 createBeanInstance() 被创建出来然后又经过一番属性注入依赖处理历经千辛万苦千锤百炼终于有点儿 bean 实例的样子能堪大任了只需要经历最后一步就破茧成蝶了。这最后一步就是初始化也就是 initializeBean()所以这篇文章我们分析 doCreateBean() 中最后一步初始化 bean。 我回到之前的doCreateBean方法中如下 在populateBean方法下面有一个initializeBean(beanName, exposedObject, mbd)方法这个就是用来执行用户设定的初始化操作。我们看下方法体 最全面的Java面试网站 protected Object initializeBean(final String beanName, final Object bean, Nullable RootBeanDefinition mbd) {if (System.getSecurityManager() ! null) {AccessController.doPrivileged((PrivilegedActionObject) () - {// 激活 Aware 方法invokeAwareMethods(beanName, bean);return null;}, getAccessControlContext());}else {// 对特殊的 bean 处理Aware、BeanClassLoaderAware、BeanFactoryAwareinvokeAwareMethods(beanName, bean);}Object wrappedBean bean;if (mbd null || !mbd.isSynthetic()) {// 后处理器wrappedBean applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try {// 激活用户自定义的 init 方法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()) {// 后处理器wrappedBean applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean; }初始化 bean 的方法其实就是三个步骤的处理而这三个步骤主要还是根据用户设定的来进行初始化这三个过程为 激活 Aware 方法后置处理器的应用激活自定义的 init 方法 激Aware方法 我们先了解一下Aware方法的使用。Spring中提供了一些Aware接口比如BeanFactoryAware,ApplicationContextAware,ResourceLoaderAware,ServletContextAware等实现这些Aware接口的bean在被初始化后可以取得一些相对应的资源例如实现BeanFactoryAware的bean在初始化之后Spring容器将会注入BeanFactory实例而实现ApplicationContextAware的bean在bean被初始化后将会被注入ApplicationContext实例等。我们先通过示例方法了解下Aware的使用。 定义普通bean如下代码 public class HelloBean {public void say(){System.out.println(Hello);} }定义beanFactoryAware类型的bean public class MyBeanAware implements BeanFactoryAware {private BeanFactory beanFactory;public void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory beanFactory;}public void testAware(){//通过hello这个bean id从beanFactory获取实例 HelloBean hello (HelloBean)beanFactory.getBean(hello);hello.say();} }分享一份大彬精心整理的大厂面试手册包含计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设计模式、架构、校招社招分享等高频面试题非常实用有小伙伴靠着这份手册拿过字节offer~ 需要的小伙伴可以自行下载 http://mp.weixin.qq.com/s?__bizMzg2OTY1NzY0MQmid2247485445idx1sn1c6e224b9bb3da457f5ee03894493dbcchksmce98f543f9ef7c55325e3bf336607a370935a6c78dbb68cf86e59f5d68f4c51d175365a189f8#rd 进行测试 public class Test {public static void main(String[] args) {ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);MyBeanAware test (MyBeanAware)ctx.getBean(myBeanAware);test.testAware();} }?xml version1.0 encodingUTF-8 ? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdbean idmyBeanAware classcom.dabin.spring.MyBeanAware/beanbean idhello classcom.dabin.spring.HelloBean/bean /beans输出 Hello上面的方法我们获取到Spring中BeanFactory并且可以根据BeanFactory获取所有的bean,以及进行相关设置。还有其他Aware的使用都是大同小异看一下Spring的实现方式 private void invokeAwareMethods(final String beanName, final Object bean) { if (bean instanceof Aware) { if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } if (bean instanceof BeanClassLoaderAware) { ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader()); } if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this); } } }处理器的应用 BeanPostPrecessor我们经常看到Spring中使用这是Spring开放式架构的一个必不可少的亮点给用户充足的权限去更改或者扩展Spring,而除了BeanPostProcessor外还有很多其他的PostProcessor当然大部分都以此为基础集成自BeanPostProcessor。BeanPostProcessor在调用用户自定义初始化方法前或者调用自定义初始化方法后分别会调用BeanPostProcessor的postProcessBeforeInitialization和postProcessAfterinitialization方法使用户可以根据自己的业务需求就行相应的处理。 public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result beanProcessor.postProcessBeforeInitialization(result, beanName); if (result null) { return result; } } return result; }public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result beanProcessor.postProcessAfterInitialization(result, beanName); if (result null) { return result; } } return result; }激活自定义的init方法 客户定制的初始化方法除了我们熟知的使用配置init-method外还有使自定义的bean实现InitializingBean接口并在afterPropertiesSet中实现自己的初始化业务逻辑。 init-method与afterPropertiesSet都是在初始化bean时执行执行顺序是afterPropertiesSet先执行而init-method后执行。 在invokeInitMethods方法中就实现了这两个步骤的初始化调用。 protected void invokeInitMethods(String beanName, final Object bean, Nullable RootBeanDefinition mbd)throws Throwable {// 是否实现 InitializingBean// 如果实现了 InitializingBean 接口则只掉调用bean的 afterPropertiesSet()boolean isInitializingBean (bean instanceof InitializingBean);if (isInitializingBean (mbd null || !mbd.isExternallyManagedInitMethod(afterPropertiesSet))) {if (logger.isDebugEnabled()) {logger.debug(Invoking afterPropertiesSet() on bean with name beanName );}if (System.getSecurityManager() ! null) {try {AccessController.doPrivileged((PrivilegedExceptionActionObject) () - {((InitializingBean) bean).afterPropertiesSet();return null;}, getAccessControlContext());}catch (PrivilegedActionException pae) {throw pae.getException();}}else {// 直接调用 afterPropertiesSet()((InitializingBean) bean).afterPropertiesSet();}}if (mbd ! null bean.getClass() ! NullBean.class) {// 判断是否指定了 init-method()// 如果指定了 init-method()则再调用制定的init-methodString initMethodName mbd.getInitMethodName();if (StringUtils.hasLength(initMethodName) !(isInitializingBean afterPropertiesSet.equals(initMethodName)) !mbd.isExternallyManagedInitMethod(initMethodName)) {// 利用反射机制执行invokeCustomInitMethod(beanName, bean, mbd);}} }首先检测当前 bean 是否实现了 InitializingBean 接口如果实现了则调用其 afterPropertiesSet()然后再检查是否也指定了 init-method()如果指定了则通过反射机制调用指定的 init-method()。 init-method() public class InitializingBeanTest {private String name;public String getName() {return name;}public void setName(String name) {this.name name;}public void setOtherName(){System.out.println(InitializingBeanTest setOtherName...);this.name dabin;} }// 配置文件 bean idinitializingBeanTest classcom.dabin.spring.InitializingBeanTestinit-methodsetOtherNameproperty namename valuedabin123/ /bean执行结果: dabin我们可以使用 beans 标签的 default-init-method 属性来统一指定初始化方法这样就省了需要在每个 bean 标签中都设置 init-method 这样的繁琐工作了。比如在 default-init-method 规定所有初始化操作全部以 initBean() 命名。如下 我们看看 invokeCustomInitMethod 方法 protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd)throws Throwable {String initMethodName mbd.getInitMethodName();Assert.state(initMethodName ! null, No init method set);Method initMethod (mbd.isNonPublicAccessAllowed() ?BeanUtils.findMethod(bean.getClass(), initMethodName) :ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));if (initMethod null) {if (mbd.isEnforceInitMethod()) {throw new BeanDefinitionValidationException(Could not find an init method named initMethodName on bean with name beanName );}else {if (logger.isTraceEnabled()) {logger.trace(No default init method named initMethodName found on bean with name beanName );}// Ignore non-existent default lifecycle methods.return;}}if (logger.isTraceEnabled()) {logger.trace(Invoking init method initMethodName on bean with name beanName );}Method methodToInvoke ClassUtils.getInterfaceMethodIfPossible(initMethod);if (System.getSecurityManager() ! null) {AccessController.doPrivileged((PrivilegedActionObject) () - {ReflectionUtils.makeAccessible(methodToInvoke);return null;});try {AccessController.doPrivileged((PrivilegedExceptionActionObject) () -methodToInvoke.invoke(bean), getAccessControlContext());}catch (PrivilegedActionException pae) {InvocationTargetException ex (InvocationTargetException) pae.getException();throw ex.getTargetException();}}else {try {ReflectionUtils.makeAccessible(initMethod);initMethod.invoke(bean);}catch (InvocationTargetException ex) {throw ex.getTargetException();}} }我们看出最后是使用反射的方式来执行初始化方法。
http://www.dnsts.com.cn/news/48502.html

相关文章:

  • 最经典最常用的网站推广方式是免费海报在线制作网站
  • 音乐网站用dw怎么做wordpress 漂浮
  • 公司网站建设深怎么做个网站演示
  • 做php网站教程我的世界皮肤做壁纸的网站
  • 软件网站设计上海快速优化排名
  • 网站托管服务方案wordpress左边导航栏
  • 网站建设费用算广告费吗郑州市二七区建设局网站
  • 优秀网站ui设计dede网站模板下载
  • 网站建设可实施性报告微信小程序api是什么意思
  • 天津 做网站怎么做网站卖产品
  • 大庆建设中等职业技术学校网站wordpress多租户
  • 上海网站设计印刷拆除中企动力高端网站建设
  • 网站推广优势兰州市城关区风险区
  • 桃源县建设局网站网上申报流程
  • 网站是哪个公司做的大型网站开发实战
  • 简单的网站制作教育机构举报电话
  • 广州建设网站公司哪家好做网站市场报价步登顶
  • 做爰全过程免费的视频的网站浙江省建设工程检测协会网站
  • 济南模版网站个人网站建设程序设计
  • 做网站个人怎么赚钱吗wordpress首页模板修改那个文件
  • 福州招聘网站有哪几个配资网站建设是什么意思
  • 网站建设加盟哪个好torrentkitty磁力猫引擎
  • 重庆专业做网站的公司腾讯地图如何标注自己店铺位置
  • 网站申请界面怎么做wordpress图片懒加载js
  • 天津网站设计开发深圳建网站开发费用
  • 建网站需要多少资金怎么做电子商务营销
  • 青岛同拓网站建设有限公司从什么网站找做游戏的代码
  • 用django做的网站社交网站平台怎么做
  • 阿里云怎样做公司网站wordpress邮件找客户端
  • 怎么做集团网站营销型网站设计特点