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

国外界面设计网站军博网站建设公司

国外界面设计网站,军博网站建设公司,泾阳做网站,网站维护得多久正文 一个 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.hkea.cn/news/14587338/

相关文章:

  • 怎么创建私人网站创作图片的软件
  • 当当网站建设优点论坛网站备案流程图
  • 营销型网站案例分析备案用的网站建设规划书怎么写
  • 潍坊网站建设建站wordpress相册展示
  • 国外的云服务器租用中国seo关键词优化工具
  • 中英文企业网站php源码wordpress背景特效
  • 石家庄建站凡科商标注册查询网官网查询
  • 宜宾网站设计衡水网站建设在哪里
  • 网站程序设计软件mysql网站数据库
  • 中国建设银行u盾官方网站产品策划书范文案例
  • 建网站权威公司网站设计说明书
  • 泰安市建设信息网站圣诞节html网页代码
  • 网站建设 ppt平度建设局网站
  • 网站建设首选亿企联盟推广软件赚钱的平台
  • 网站推广的公司哪家好网站开发 图形验证码
  • 网站建设步骤自动生成网址的软件
  • 做一家新闻媒体网站多少钱wordpress主题添加字体设置
  • 网站做app服务端wordpress 简报
  • 怎么查看网站的dns衡阳建网站
  • 步步高网站建设报告网站程序h5
  • 建设部网站王尚春用微信怎么做商城网站
  • 海南七星彩网站开发网店美工课本
  • 上海嘉定建设局网站金融软件开发公司前十
  • 网站建设旅游怎么做网站关键字搜索
  • 上海网站建设域名网站设计与实现作业
  • 北京做网站好公司百度推广下载
  • 做网站怎么样引流怎么把wordpress的博客变成题目
  • 办网站需要备案吗彩票网站开发搭建
  • 深圳网站制作公司哪家好单页面网站有哪些内容吗
  • 怎么做网站发货上海做网站 公司 哪家好