bean生命周期:
bean创建-----初始化-----销毁的过程
先区别一下Spring Bean的实例化和初始化两个阶段的主要作用:
1、实例化—-实例化的过程是一个创建Bean的过程,即调用Bean的构造函数,单例的Bean放入单例池中
2、初始化—-初始化的过程是一个赋值的过程,即调用Bean的setter,设置Bean的属性
容器管理Bean的生命周期:
我们可以自定义初始化和销毁的方法,容器在bean进行到当前生命周期时候来调用我们自定义初始化和销毁方法
构造(对象创建):
单实例:在容启动的时候创建
多实例: 在每次获取的时候创建
初始化:
对象创建完成,并赋值好,调用初始化方法
销毁:
单实例:容器关闭的时候
多实例:容器不会管理这个bean,容器不会调用销毁方法
1)指定初始化和销毁方法:
指定init-method 和 destory-method
2) 通过 Bean实现 InitializingBean (初始化) 和 DisposableBean 销毁
3) 使用JSR250:
PostConstruct :在Bean 创建属性赋值,执行初始化方法
PreDestroy : 在容器销毁bean 之前通知我们进行清理
4)BeanPostProcessor:bean的后置处理器;bean初始化前后调用
postProcessBeforeInitialization :在init-method或afterPropertiesSet Bean创建初始化之前 进行调用。
postProcessAfterInitialization :在init-method或afterPropertiesSet Bean创建初始化之后 进行调用。
1)指定初始化和销毁方法:
指定init-method 和 destory-method
package com.lm.Spring.config;
import com.lm.Spring.bean.Car;
import com.lm.Spring.bean.Cat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;
/**
* describe: bean生命周期
*
* bean创建-----初始化-----销毁的过程
*
* 容器管理Bean的生命周期:
* 我们可以自定义初始化和销毁的方法,容器在bean进行到当前生命周期时候来调用我们自定义初始化和销毁方法
*
* 构造(对象创建)
* 单实例:在容启动的时候创建
* 多实例: 在每次获取的时候创建
* 初始化:
* 对象创建完成,并赋值好,调用初始化方法
*
* 销毁:
* 单实例:容器关闭的时候
* 多实例:容器不会管理这个bean,容器不会调用销毁方法
*
* 1)指定初始化和销毁方法:
* 指定init-method 和 destory-method
* 2) 通过 Bean实现 InitializingBean (初始化) 和 DisposableBean 销毁
*
* @author lm
* @date 2018/10/25
*/
@ComponentScan("com.lm.Spring.bean")
@Configuration
public class MyConfigOfLiteCycle {
@Bean(initMethod = "init",destroyMethod = "destory")
public Car car(){
return new Car();
}
}
2) 通过 Bean实现 InitializingBean (初始化) 和 DisposableBean 销毁
InitializingBean 当BeanFactory创建对象,并且属性赋值完调用afterPropertiesSet()方法
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory;
/**
* Interface to be implemented by beans that need to react once all their
* properties have been set by a BeanFactory: for example, to perform custom
* initialization, or merely to check that all mandatory properties have been set.
*
* <p>An alternative to implementing InitializingBean is specifying a custom
* init-method, for example in an XML bean definition.
* For a list of all bean lifecycle methods, see the
* {@link BeanFactory BeanFactory javadocs}.
*
* @author Rod Johnson
* @see BeanNameAware
* @see BeanFactoryAware
* @see BeanFactory
* @see org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName
* @see org.springframework.context.ApplicationContextAware
*/
public interface InitializingBean {
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
void afterPropertiesSet() throws Exception;
}
DisposableBean:BeanFactory 销毁的时候把单实例Bean 销毁
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory;
/**
* Interface to be implemented by beans that want to release resources
* on destruction. A BeanFactory is supposed to invoke the destroy
* method if it disposes a cached singleton. An application context
* is supposed to dispose all of its singletons on close.
*
* <p>An alternative to implementing DisposableBean is specifying a custom
* destroy-method, for example in an XML bean definition.
* For a list of all bean lifecycle methods, see the
* {@link BeanFactory BeanFactory javadocs}.
*
* @author Juergen Hoeller
* @since 12.08.2003
* @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName
* @see org.springframework.context.ConfigurableApplicationContext#close
*/
public interface DisposableBean {
/**
* Invoked by a BeanFactory on destruction of a singleton.
* @throws Exception in case of shutdown errors.
* Exceptions will get logged but not rethrown to allow
* other beans to release their resources too.
*/
void destroy() throws Exception;
}
package com.lm.Spring.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
* describe:
*
* @author lm
* @date 2018/10/25
*/
@Component
public class Cat implements InitializingBean,DisposableBean {
public Cat() {
System.out.println("Cat.constructor");
}
@Override
public void destroy() throws Exception {
System.out.println("Cat.destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Cat.afterPropertiesSet");
}
}
3) 使用JSR250:
PostConstruct :在Bean 创建属性赋值,执行初始化方法
PreDestroy : 在容器销毁bean 之前通知我们进行清理
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}
package com.lm.Spring.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* describe:
*
* @author lm
* @date 2018/10/25
*/
public class Car {
public Car() {
System.out.println("Car.constructor");
}
@PostConstruct
public void init(){
System.out.println("Car.init");
}
@PreDestroy
public void destory(){
System.out.println("Car.destory");
}
}
4)BeanPostProcessor:bean的后置处理器;bean初始化前后调用
postProcessBeforeInitialization :在init-method或afterPropertiesSet Bean创建初始化之前 进行调用。
postProcessAfterInitialization :在init-method或afterPropertiesSet Bean创建初始化之后 进行调用。
package com.lm.Spring.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* describe: 后置处理器
*
* @author lm
* @date 2019/2/24
*/
@Component
public class MyBeanPostProcess implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcess.postProcessBeforeInitialization"+" beanName:"+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcess.postProcessAfterInitialization"+" beanName:"+beanName);
return bean;
}
}
Bean 生命周期原理:
遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization,一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessors
populateBean(beanName,mbd,instanceWrapp);给bean进行属性赋值
initializeBean
{
applyBeanPostProcessorBeforeInitialization(wrappedBean,beanName)
invokeInitMethods(beanName,wrappedBean,mbd)执行自定义初始化
applyBeanPostProcessorAfterInitialization(wrappedBean,beanName)
}
Spring 底层 对BeanPostProcessor 的使用:
ApplicationContextAware 获取IOC 容器接口 ,AutowiredAnnotationBeanPostProcessor 注解接口,@Async
package com.lm.Spring.bean;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* describe:
*
* @author lm
* @date 2018/10/24
*/
public class Blue implements ApplicationContextAware {
private ApplicationContext applicationContext;
@SuppressWarnings("static-access")
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.support;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.EmbeddedValueResolver;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.lang.Nullable;
import org.springframework.util.StringValueResolver;
/**
* {@link org.springframework.beans.factory.config.BeanPostProcessor}
* implementation that passes the ApplicationContext to beans that
* implement the {@link EnvironmentAware}, {@link EmbeddedValueResolverAware},
* {@link ResourceLoaderAware}, {@link ApplicationEventPublisherAware},
* {@link MessageSourceAware} and/or {@link ApplicationContextAware} interfaces.
*
* <p>Implemented interfaces are satisfied in order of their mention above.
*
* <p>Application contexts will automatically register this with their
* underlying bean factory. Applications do not use this directly.
*
* @author Juergen Hoeller
* @author Costin Leau
* @author Chris Beams
* @since 10.10.2003
* @see org.springframework.context.EnvironmentAware
* @see org.springframework.context.EmbeddedValueResolverAware
* @see org.springframework.context.ResourceLoaderAware
* @see org.springframework.context.ApplicationEventPublisherAware
* @see org.springframework.context.MessageSourceAware
* @see org.springframework.context.ApplicationContextAware
* @see org.springframework.context.support.AbstractApplicationContext#refresh()
*/
class ApplicationContextAwareProcessor implements BeanPostProcessor {
private final ConfigurableApplicationContext applicationContext;
private final StringValueResolver embeddedValueResolver;
/**
* Create a new ApplicationContextAwareProcessor for the given context.
*/
public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
}
@Override
@Nullable
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
AccessControlContext acc = null;
if (System.getSecurityManager() != null &&
(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
}
if (acc != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareInterfaces(bean);
return null;
}, acc);
}
else {
invokeAwareInterfaces(bean);
}
return bean;
}
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
if (bean instanceof EnvironmentAware) {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
}
if (bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
}
if (bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
}
if (bean instanceof MessageSourceAware) {
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}
}