91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring bean 生命周期事件

發布時間:2020-07-24 11:23:16 來源:網絡 閱讀:1568 作者:我叫袁蒙蒙 欄目:編程語言

整體流程圖

spring bean 生命周期事件

程序示例

maven依賴

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

</dependencies>

配置類

@Configuration
@ComponentScan("per.ym.processor, per.ym.service")
public class LifeCycleConfig {

    @Bean(initMethod = "init", destroyMethod = "destroy")
    public LifeCycleBean lifeCycleBean() {
        return new LifeCycleBean();
    }
}

實例bean

public class LifeCycleBean implements InitializingBean, BeanNameAware, BeanFactoryAware, BeanClassLoaderAware {

    public LifeCycleBean() {
        System.out.println("consturctor......");
    }

    public void init() {
        System.out.println("init......");
    }

    @Autowired
    public void setService(PersonService service) {
        System.out.println("setService......");
    }

    public void destroy() {
        System.out.println("destroy......");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet......");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setBeanFactory......");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("setBeanName......");
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("setBeanClassLoader......");
    }

}

InstantiationAwareBeanPostProcessor

package per.ym.processor;

@Component
public class MyInstantiationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessBeforeInstantiation......");
        }
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessAfterInstantiation......");
        }
        return true;
    }

    @Override
    public PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessPropertyValues......");
        }
        return pvs;
    }

}

BeanPostProcessor

package per.ym.processor;

@Component
public class MyPostProcess implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessBeforeInitialization......");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessAfterInitialization......");
        }
        return bean;
    }

}

service

package per.ym.service;

@Service
public class Service {
}

測試類

public class Test_Normal {

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(LifeCycleConfig.class);

    @Test
    public void test() {
        applicationContext.getBean("lifeCycleBean");
        applicationContext.close();
    }
}

測試結果:

postProcessBeforeInstantiation......
consturctor......
postProcessAfterInstantiation......
postProcessPropertyValues......
setService......
setBeanName......
setBeanClassLoader......
setBeanFactory......
postProcessBeforeInitialization......
afterPropertiesSet......
init......
postProcessAfterInitialization......
destroy......
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

青州市| 南江县| 黄陵县| 宁远县| 饶平县| 青川县| 许昌县| 河曲县| 贵德县| 邓州市| 调兵山市| 佛坪县| 六安市| 和平区| 汉源县| 宝应县| 苗栗县| 荆州市| 宜兰县| 宁海县| 从化市| 江城| 寿阳县| 栾川县| 衡阳县| 靖远县| 东乌珠穆沁旗| 易门县| 靖边县| 黔江区| 沁阳市| 苏尼特右旗| 金湖县| 平泉县| 堆龙德庆县| 资源县| 垦利县| 右玉县| 澄城县| 靖江市| 自治县|