网站开发的数据库设计实体是什么,商业软文怎么写,免费无代码开发平台本地部署,做一个简单的公司网站要多少钱在项目中经常会在容器启动时#xff0c;完成特定的初始化操作#xff0c;如资源文件的加载等。 一 实现的方式有三种#xff1a; 1.使用PostConstruct注解#xff0c;该注解作用于void方法上 2.在配置文件中配置init-method方法 bean idstudent class完成特定的初始化操作如资源文件的加载等。 一 实现的方式有三种 1.使用PostConstruct注解该注解作用于void方法上 2.在配置文件中配置init-method方法 bean idstudent classcom.demo.spring.entity.Student init-methodinit2property namename value景甜/propertyproperty nameage value28/propertyproperty nameschool refschool/property
/bean 3.将类实现InitializingBean接口 package com.demo.spring.entity;import javax.annotation.PostConstruct;import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;/*** author chenyk* date 2018年5月8日*/
Component(student)
public class Student implements InitializingBean{private String name;private int age;private School school;public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public School getSchool() {return school;}public void setSchool(School school) {this.school school;}//1.使用postconstrtct注解PostConstructpublic void init(){System.out.println(执行 init方法);}//2.在xml配置文件中配置init-method方法public void init2(){System.out.println(执行init2方法 );}//3.实现InitializingBean接口public void afterPropertiesSet() throws Exception {System.out.println(执行init3方法);}} 执行结果 执行 init方法2018-06-11 10:09:16,195 DEBUG [AbstractAutowireCapableBeanFactory.java:1671] : Invoking afterPropertiesSet() on bean with name student执行init3方法2018-06-11 10:09:36,459 DEBUG [AbstractAutowireCapableBeanFactory.java:1731] : Invoking init method init2 on bean with name student执行init2 方法 二 实现原理 由以上执行结果可知先执行PostConstruct注解的方法然后是实现了InitializingBean接口的afterPropertiesSet方法最后执行在配置文件中配置的init-method方法。至于为什么是这个顺序可以看源码 在 AbstractAutowireCapableBeanFactory 类中 protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {if (System.getSecurityManager() ! null) {AccessController.doPrivileged(new PrivilegedActionObject() {Overridepublic Object run() {invokeAwareMethods(beanName, bean);return null;}}, getAccessControlContext());}else {invokeAwareMethods(beanName, bean);}Object wrappedBean bean;if (mbd null || !mbd.isSynthetic()) {wrappedBean applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try { //调用初始化方法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;} 然后进入到 invokeInitMethods 方法中 protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)throws Throwable {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(new PrivilegedExceptionActionObject() {Overridepublic Object run() throws Exception {((InitializingBean) bean).afterPropertiesSet();return null;}}, getAccessControlContext());}catch (PrivilegedActionException pae) {throw pae.getException();}}else { //直接调用 InitializingBean 接口中的 afterPropertiesSet 方法((InitializingBean) bean).afterPropertiesSet();}}if (mbd ! null) {String initMethodName mbd.getInitMethodName();if (initMethodName ! null !(isInitializingBean afterPropertiesSet.equals(initMethodName)) !mbd.isExternallyManagedInitMethod(initMethodName)) { //进入该方法可知通过反射的方式调用init-method方法invokeCustomInitMethod(beanName, bean, mbd);}}} 所以初始化方法的执行顺序 afterPropertiesSet() init-method() 博客园的第一篇文章。感觉博客园很干净文章排版特别是插入代码格式看起来很舒服。就不吐槽csdn了。转载于:https://www.cnblogs.com/51life/p/9166009.html