spring-注解---LifeCylce
Posted 周无极
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-注解---LifeCylce相关的知识,希望对你有一定的参考价值。
(2) spring-注解---LifeCylce
package com.zwj.bean; import org.springframework.stereotype.Component; @Component public class Car { public Car(){ System.out.println("car constructor..."); } public void init(){ System.out.println("car ... init..."); } public void detory(){ System.out.println("car ... detory..."); } }
package com.zwj.bean; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; @Component public class Cat implements InitializingBean,DisposableBean { public Cat(){ System.out.println("cat constructor..."); } public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("cat...destroy..."); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("cat...afterPropertiesSet..."); } }
package com.zwj.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class Dog implements ApplicationContextAware { //@Autowired private ApplicationContext applicationContext; public Dog(){ System.out.println("dog constructor..."); } //对象创建并赋值之后调用 @PostConstruct public void init(){ System.out.println("Dog....@PostConstruct..."); } //容器移除对象之前 @PreDestroy public void detory(){ System.out.println("Dog....@PreDestroy..."); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.applicationContext = applicationContext; } }
package com.zwj.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; /** * 后置处理器:初始化前后进行处理工作 * 将后置处理器加入到容器中 * @author lfy */ @Component public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean); return bean; } }
package com.zwj.config; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import com.zwj.bean.Car; /** * bean的生命周期: * bean创建---初始化----销毁的过程 * 容器管理bean的生命周期; * 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法 * * 构造(对象创建) * 单实例:在容器启动的时候创建对象 * 多实例:在每次获取的时候创建对象\\ * * BeanPostProcessor.postProcessBeforeInitialization * 初始化: * 对象创建完成,并赋值好,调用初始化方法。。。 * BeanPostProcessor.postProcessAfterInitialization * 销毁: * 单实例:容器关闭的时候 * 多实例:容器不会管理这个bean;容器不会调用销毁方法; * * * 遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization, * 一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization * * BeanPostProcessor原理 * populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值 * initializeBean * { * applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); * invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化 * applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); *} * * * * 1)、指定初始化和销毁方法; * 通过@Bean指定init-method和destroy-method; * 2)、通过让Bean实现InitializingBean(定义初始化逻辑), * DisposableBean(定义销毁逻辑); * 3)、可以使用JSR250; * @PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法 * @PreDestroy:在容器销毁bean之前通知我们进行清理工作 * 4)、BeanPostProcessor【interface】:bean的后置处理器; * 在bean初始化前后进行一些处理工作; * postProcessBeforeInitialization:在初始化之前工作 * postProcessAfterInitialization:在初始化之后工作 * * Spring底层对 BeanPostProcessor 的使用; * bean赋值,注入其他组件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor; * * @author lfy * */ @ComponentScan("com.zwj.bean") @Configuration public class MainConfigOfLifeCycle { //@Scope("prototype") @Bean(initMethod="init",destroyMethod="detory") public Car car(){ return new Car(); } }
package com.zwj.test; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.zwj.config.MainConfigOfLifeCycle; public class IOCTest_LifeCycle { @Test public void test01(){ //1、创建ioc容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class); System.out.println("容器创建完成..."); //applicationContext.getBean("car"); //关闭容器 applicationContext.close(); } } /** car constructor... postProcessBeforeInitialization...car=>com.zwj.bean.Car@41ee392b car ... init... postProcessAfterInitialization...car=>com.zwj.bean.Car@41ee392b cat constructor... postProcessBeforeInitialization...cat=>com.zwj.bean.Cat@667a738 cat...afterPropertiesSet... postProcessAfterInitialization...cat=>com.zwj.bean.Cat@667a738 dog constructor... postProcessBeforeInitialization...dog=>com.zwj.bean.Dog@400cff1a Dog....@PostConstruct... postProcessAfterInitialization...dog=>com.zwj.bean.Dog@400cff1a 容器创建完成... */
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zwj</groupId> <artifactId>spring-annotation</artifactId> <version>0.0.1-SNAPSHOT</version> <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> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
以上是关于spring-注解---LifeCylce的主要内容,如果未能解决你的问题,请参考以下文章
Spring注解处理Ajax请求-JSON格式[系统架构:Spring+SpringMVC+MyBatis+MySql]
Spring Boot实战笔记-- Spring高级话题(组合注解与元注解)