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

增城网站定制开发公司一整套ppt背景图片

增城网站定制开发公司,一整套ppt背景图片,淄博网站电子商城平台建设,重庆工程信息官网接AnnotationConfigApplicationContext流程看实例化的beanPostProcessor-CSDN博客#xff0c;以具体实例看bean生命周期的一些执行阶段 bean生命周期流程 生命周期扩展处理说明实例化:createBeanInstance 构造方法#xff0c; 如Autowired的构造方法注入依赖bean 如UserSer…接AnnotationConfigApplicationContext流程看实例化的beanPostProcessor-CSDN博客以具体实例看bean生命周期的一些执行阶段 bean生命周期流程 生命周期扩展处理说明实例化:createBeanInstance 构造方法 如Autowired的构造方法注入依赖bean 如UserService构造方法注入AppDAO Autowired public UserService(AppDAO appDAO) {System.out.println(Autowired appDAO: appDAO);this.appDAO appDAO; } 属性设置populateBean 如setter方法注入的依赖 如Autoware的filed注入 如UserService中注入UserDAO Resource(name u) public void setUserDAO(UserDAO userDAO) {System.out.println(setUserDAO: userDAO);this.userDAO userDAO; } 初始化initializeBean 1. BeanPostProcessor#postProcessBeforeInitialization 2. PostConstruct 3. InitializingBean#afterPropertiesSet initMethod 4. BeanPostProcessor#postProcessAfterInitialization 上一步骤属性设置完后初始化方法前后都可以扩展。 例如BeanPostProcessor#postProcessBeforeInitialization直接改变了set注入的appDAO对象 Component public class ServiceBeanPostProcessor implements BeanPostProcessor {Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// Bean 初始化前的逻辑if (beanName.equals(userService)) {UserService userService (UserService)bean;AppDAO appDAO new AppDAOImpl2();userService.setAppDAO(appDAO);System.out.println(postProcessBeforeInitialization: : bean);}return bean;}销毁 1. PreDestroy 2. DisposableBean#destroy destroyMethod 附org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean /*** Actually create the specified bean. Pre-creation processing has already happened* at this point, e.g. checking {code postProcessBeforeInstantiation} callbacks.* pDifferentiates between default bean instantiation, use of a* factory method, and autowiring a constructor.* param beanName the name of the bean* param mbd the merged bean definition for the bean* param args explicit arguments to use for constructor or factory method invocation* return a new instance of the bean* throws BeanCreationException if the bean could not be created* see #instantiateBean* see #instantiateUsingFactoryMethod* see #autowireConstructor*/protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Nullable Object[] args)throws BeanCreationException {// Instantiate the bean.BeanWrapper instanceWrapper null;if (mbd.isSingleton()) {instanceWrapper this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper null) {instanceWrapper createBeanInstance(beanName, mbd, args);}final Object bean instanceWrapper.getWrappedInstance();Class? beanType instanceWrapper.getWrappedClass();if (beanType ! NullBean.class) {mbd.resolvedTargetType beanType;}// Allow post-processors to modify the merged bean definition.synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) {try {applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,Post-processing of merged bean definition failed, ex);}mbd.postProcessed true;}}// Eagerly cache singletons to be able to resolve circular references// even when triggered by lifecycle interfaces like BeanFactoryAware.boolean earlySingletonExposure (mbd.isSingleton() this.allowCircularReferences isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isTraceEnabled()) {logger.trace(Eagerly caching bean beanName to allow for resolving potential circular references);}addSingletonFactory(beanName, () - getEarlyBeanReference(beanName, mbd, bean));}// Initialize the bean instance.Object exposedObject bean;try {populateBean(beanName, mbd, instanceWrapper);exposedObject initializeBean(beanName, exposedObject, mbd);}catch (Throwable ex) {if (ex instanceof BeanCreationException beanName.equals(((BeanCreationException) ex).getBeanName())) {throw (BeanCreationException) ex;}else {throw new BeanCreationException(mbd.getResourceDescription(), beanName, Initialization of bean failed, ex);}}if (earlySingletonExposure) {Object earlySingletonReference getSingleton(beanName, false);if (earlySingletonReference ! null) {if (exposedObject bean) {exposedObject earlySingletonReference;}else if (!this.allowRawInjectionDespiteWrapping hasDependentBean(beanName)) {String[] dependentBeans getDependentBeans(beanName);SetString actualDependentBeans new LinkedHashSet(dependentBeans.length);for (String dependentBean : dependentBeans) {if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {actualDependentBeans.add(dependentBean);}}if (!actualDependentBeans.isEmpty()) {throw new BeanCurrentlyInCreationException(beanName,Bean with name beanName has been injected into other beans [ StringUtils.collectionToCommaDelimitedString(actualDependentBeans) ] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using getBeanNamesOfType with the allowEagerInit flag turned off, for example.);}}}}// Register bean as disposable.try {registerDisposableBeanIfNecessary(beanName, bean, mbd);}catch (BeanDefinitionValidationException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, Invalid destruction signature, ex);}return exposedObject;} 扩展执行 接上文AnnotationConfigApplicationContext流程看实例化的beanPostProcessor-CSDN博客来看这些BeanPostProcessor如何起作用 测试bean定义类如下 Service(userService) public class UserService implements InitializingBean, DisposableBean {private String userServiceName;private UserDAO userDAO;private AppDAO appDAO;/*** field 注入*/Autowiredprivate CommonService commonService;public UserService() {System.out.println(constructor);}/*** 类似于 InitializingBean 的 afterPropertiesSet() 方法*/PostConstructpublic void init() {System.out.println(PostConstruct userService);System.out.println(userDAO is not null: (userDAO ! null));System.out.println(appDAO is not null: (appDAO ! null));userServiceName selfUserService;}/*** setter注入* param userDAO*/Resource(name u)public void setUserDAO(UserDAO userDAO) {System.out.println(setUserDAO: userDAO);this.userDAO userDAO;}/*** 构造器注入* param appDAO*/Autowiredpublic UserService(AppDAO appDAO) {System.out.println(Autowired appDAO: appDAO);this.appDAO appDAO;}public void add(User user) {userDAO.save(user);}public UserDAO getUserDAO() {return userDAO;}/*** 类似于 DisposableBean 的 destroy() 方法*/PreDestroypublic void preDestroy() {System.out.println(preDestroy);}Overridepublic void afterPropertiesSet() throws Exception {System.out.println(afterPropertiesSet userService);}Overridepublic void destroy() throws Exception {System.out.println(destroy userService);}public void setUserServiceName(String userServiceName) {this.userServiceName userServiceName;}public void setAppDAO(AppDAO appDAO) {this.appDAO appDAO;}Overridepublic String toString() {return UserService{ userServiceName userServiceName \ , userDAO userDAO , appDAO appDAO , commonService commonService };} }InstantiationAwareBeanPostProcessorsSmartInstantiationAwareBeanPostProcessor 推断构造函数最后是AutowiredAnnotationBeanPostProcessor完成构造函数的推断 最后判断有Autowired的构造函数 完成实例化过程 InstantiationAwareBeanPostProcessorCommonAnnotationBeanPostProcessor populate阶段CommonAnnotationBeanPostProcessor完成setter注入 /*** setter注入* param userDAO*/ Resource(name u) public void setUserDAO(UserDAO userDAO) {System.out.println(setUserDAO: userDAO);this.userDAO userDAO; } org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#postProcessProperties Overridepublic PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {InjectionMetadata metadata findResourceMetadata(beanName, bean.getClass(), pvs);try {metadata.inject(bean, beanName, pvs);}catch (Throwable ex) {throw new BeanCreationException(beanName, Injection of resource dependencies failed, ex);}return pvs;} InstantiationAwareBeanPostProcessorAutowiredAnnotationBeanPostProcessor populate阶段AutowiredAnnotationBeanPostProcessor完成Autorwired的filed的注入具体可以再次阅读Spring5.1.3 Autorwired注解原理重新回顾-CSDN博客 BeanPostProcessor#postProcessBeforeInitialization initializeBean阶段会执行BeanPostProcessor的postProcessBeforeInitialization方法执行自定义的BeanPostProcessor 执行CommonAnnotationBeanPostProcessor解析PostConstruct注解 PostConstruct public void init() {System.out.println(PostConstruct userService);System.out.println(userDAO is not null: (userDAO ! null));System.out.println(appDAO is not null: (appDAO ! null));userServiceName selfUserService; } 执行InitializingBean的afterPropertiesSet方法 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeInitMethods protected void invokeInitMethods(String beanName, final Object bean, Nullable RootBeanDefinition mbd)throws Throwable {boolean isInitializingBean (bean instanceof InitializingBean);if (isInitializingBean (mbd null || !mbd.isExternallyManagedInitMethod(afterPropertiesSet))) {if (logger.isTraceEnabled()) {logger.trace(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 {((InitializingBean) bean).afterPropertiesSet();}}if (mbd ! null bean.getClass() ! NullBean.class) {String initMethodName mbd.getInitMethodName();if (StringUtils.hasLength(initMethodName) !(isInitializingBean afterPropertiesSet.equals(initMethodName)) !mbd.isExternallyManagedInitMethod(initMethodName)) {invokeCustomInitMethod(beanName, bean, mbd);}}} BeanPostProcessor#postProcessAfterInitialization InitializingBean最后阶段会执行BeanPostProcessor的postProcessAfterInitialization方法 本例 Component public class ServiceBeanPostProcessor implements BeanPostProcessor {Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// Bean 初始化前的逻辑if (beanName.equals(userService)) {UserService userService (UserService)bean;AppDAO appDAO new AppDAOImpl2();userService.setAppDAO(appDAO);System.out.println(postProcessBeforeInitialization: : bean);}return bean;}Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {// Bean 初始化后的逻辑if (beanName.equals(userService)) {System.out.println(postProcessAfterInitialization: : bean);}return bean;} }
http://www.w-s-a.com/news/859268/

相关文章:

  • 外贸网站推广中山网站流量团队
  • 网站前端设计培训做一份网站的步zou
  • 网站备案拍照茶叶网页设计素材
  • wordpress 手机商城模板关键词优化软件有哪些
  • 网站301做排名python做的网站如何部署
  • 昆山做企业网站工信部网站 备案
  • 做英文的小说网站有哪些网站做qq登录
  • 湖州建设局招投标网站深圳广告公司集中在哪里
  • 重庆主城推广网站建设商城网站建设预算
  • 宁波品牌网站推广优化公司开发公司工程部工作总结
  • 长沙建站模板微信网站建设方案
  • 不让网站在手机怎么做门户网站 模板之家
  • 网站建设及推广图片wordpress文章摘要调用
  • 手机版网站案例全国信息企业公示系统
  • 模仿别人网站建设银行广州招聘网站
  • 沧州网站建设沧州内页优化
  • 代加工网站有哪些专门做网站关键词排名
  • 郑州做景区网站建设公司软件开发者模式怎么打开
  • 长沙企业网站建设哪家好做app一般多少钱
  • 南宁一站网网络技术有限公司网站开发技术应用领域
  • 公司网站建设方案ppt专业构建网站的公司
  • 深圳网站建设方维网络网站框架设计好后怎么做
  • 合肥网站建设过程网站栏目建设调研
  • 手机访问网站页面丢失北京电商平台网站建设
  • 郑州网站怎么推广中山 网站关键词优化
  • 国外试用网站空间网站建设与管理题目
  • 淄博网赢网站建设网站设计的技术选择
  • 建外贸网站 东莞厦门做网站最好的公司
  • 为您服务网站新网站做百度推广
  • 电子商务免费网站建设网站制作哪个好薇