为了账号安全,请及时绑定邮箱和手机立即绑定

Bean的生命周期

标签:
Java

配置Bean的初始化和销毁的方法:

配置初始化和销毁的方法:

 * init-method=”setup”

 * destroy-method=”teardown”


执行销毁的时候,必须手动关闭工厂,而且只对scope=”singleton”有效.


##### Bean的生命周期的11个步骤:

1. instantiate bean对象实例化

2. populate properties 封装属性

3. 如果Bean实现BeanNameAware 执行 setBeanName

4. 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext

5. 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization

6. 如果Bean实现InitializingBean 执行 afterPropertiesSet 

7. 调用<bean init-method="init"> 指定初始化方法 init

8. 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization

9. 执行业务处理

10. 如果Bean实现 DisposableBean 执行 destroy

11. 调用<bean destroy-method="customerDestroy"> 指定销毁方法 customerDestroy


在CustomerService类的add方法之前进行权限校验?


<!-- demo4 bean生命周期--><bean id="customerservice" class="cn.spring.demo4.CustomerServiceimpl" init-method="setup" destroy-method="teardown"><property name="name" value="&lt;property name=&quot;name&quot; value=&quot;&quot;&gt;"/></bean><bean class="cn.spring.demo4.myBeanPostProcessor"></bean><!-- demo4 bean生命周期-->


CustomerService.java 接口函数
package cn.spring.demo4;public interface CustomerService {public void add();public void find();}


CustomerServiceimpl.java
package cn.spring.demo4;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class CustomerServiceimpl implements CustomerService,BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean   {private String name;//实现自己业务逻辑public void add() {System.out.println("第9步添加用户");}public void find() {System.out.println("第9步查找用户");}public void setName(String name){System.out.println("第2步: 封装属性"+name);this.name = name;}public CustomerServiceimpl() {super();// TODO Auto-generated constructor stubSystem.out.println("第1步:对象实例化");}public void setBeanName(String name) {// TODO Auto-generated method stubSystem.out.println("第3步:注入配置文件名称"+name);}public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {// TODO Auto-generated method stubSystem.out.println("第4步:注入ApplicationContext"+applicationContext);}public void afterPropertiesSet() throws Exception {// TODO Auto-generated method stubSystem.out.println("第6步:属性设置后执行……");}public void setup(){System.out.println("第7步:调用手动设置的初始化方法");}public void destroy() throws Exception {// TODO Auto-generated method stubSystem.out.println("第10步:调用的销毁的方法");}public void teardown(){System.out.println("第11步:调用手动设置的销毁方法");}}


myBeanPostProcessor.javapackage cn.spring.demo4;import java.lang.reflect.Method;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.cglib.proxy.InvocationHandler;import org.springframework.cglib.proxy.Proxy;public class myBeanPostProcessor implements BeanPostProcessor {/* * @bean:实例对象 *  * @beanName:在配置文件中配置的类的标识 */public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {// 可以在这里增强System.out.println("第5步,初始化之前");return bean;}public Object postProcessAfterInitialization(final Object bean,String beanName) throws BeansException {System.out.println("第8步:初始化后执行...");// 动态代理:if (beanName.equals("customerservice")) {Object proxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(),new InvocationHandler() {// 调用目标方法的时候,调用invoke方法.public Object invoke(Object proxy, Method method,Object[] args) throws Throwable {if ("add".equals(method.getName())) {System.out.println("权限校验...");Object result = method.invoke(bean, args);System.out.println(System.currentTimeMillis());return result;}return method.invoke(bean, args);}});return proxy;}return bean;}}


package cn.spring.demo4;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest4 {@Testpublic void demo1() {ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext("applicationContext.xml");CustomerService customerservice=(CustomerService) applicationcontext.getBean("customerservice");customerservice.add();customerservice.find();applicationcontext.close();}}


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消