网站维护方式,asp网站没有数据库连接,建设门户网站发展前景2018,wordpress手机模板怎么用InitializingBean
1、InitializingBean接口为bean提供了初始化方法的方式#xff0c;它只包括afterPropertiesSet方法#xff0c;凡是继承该接口的类#xff0c;在初始化bean的时候都会执行该方法。 2、spring初始化bean的时候#xff0c;如果bean实现了InitializingBean接…InitializingBean
1、InitializingBean接口为bean提供了初始化方法的方式它只包括afterPropertiesSet方法凡是继承该接口的类在初始化bean的时候都会执行该方法。 2、spring初始化bean的时候如果bean实现了InitializingBean接口会自动调用afterPropertiesSet方法。 3、在Spring初始化bean的时候如果该bean实现了InitializingBean接口并且同时在配置文件中指定了init-method系统则是先调用afterPropertieSet()方法然后再调用init-method中指定的方法。
1、Spring为bean提供了两种初始化bean的方式实现InitializingBean接口实现afterPropertiesSet方法或者在配置文件中通过init-method指定两种方式可以同时使用。 2、实现InitializingBean接口是直接调用afterPropertiesSet方法比通过反射调用init-method指定的方法效率要高一点但是init-method方式消除了对spring的依赖。 3、如果调用afterPropertiesSet方法时出错则不调用init-method指定的方法 spring初始化bean有两种方式 第一实现InitializingBean接口继而实现afterPropertiesSet的方法 第二反射原理配置文件使用init-method标签直接注入bean
相同点 实现注入bean的初始化。
不同点 1实现的方式不一致。 2接口比配置效率高但是配置消除了对spring的依赖。而实现InitializingBean接口依然采用对spring的依赖。 执行顺序优先级 构造方法 postConstruct afterPropertiesSet init方法。 Component
public class InitializingBeanTest implements InitializingBean {public InitializingBeanTest(){System.out.println(构造方法);}Overridepublic void afterPropertiesSet() throws Exception {System.out.println(测试InitializingBean afterPropertiesSet);}PostConstructpublic void testPostConstruct(){System.out.println(测试PostConstruct);}public void init(){System.out.println(测试init方法执行...);}Bean(initMethod init)public InitializingBeanTest test() {return new InitializingBeanTest();}
}