Spring中Bean的生命周期

会调用以下11个方法:

 一、创建service、serviceImpl以及processor如下:

public interface UserService {
   boolean login(String username, String password);
}
package com.mj.service.impl;

import com.mj.service.UserService;
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 UserServiceImpl implements UserService, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
    private Integer age;

    public UserServiceImpl() {
        System.out.println("01 ---- UserServiceImpl ");
    }

    public void setAge(Integer age) {
        this.age = age;
        System.out.println("02 ---- setAge ");
    }

    /**
     * 在初始化完毕之后调用
     */
    public void init() {
        System.out.println("07 ---- init-method ");
    }

    public void dealloc() {
        System.out.println("11 ---- destroy-method ");
    }

    public boolean login(String username, String password) {
        System.out.println("09 -----login  ");
        return false;
    }

    public void setBeanName(String s) {
        System.out.println("03 -----BeanNameAware - setBeanName " + s);
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("04 -----ApplicationContextAware - setApplicationContext " + applicationContext);
    }

    public void destroy() throws Exception {
        System.out.println("10 -----DisposableBean - destroy");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("06 -----InitializingBean - afterPropertiesSet");
    }
}
package com.mj.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("05 ---- BeanPostProcessor - before" + beanName);
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("08 ---- BeanPostProcessor - after" + beanName);
        return bean;
    }
}

二、配置文件设置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="userService"
          class="com.mj.service.impl.UserServiceImpl"
          p:age="23"
          init-method="init"
          destroy-method="dealloc"/>

    <bean class="com.mj.processor.MyProcessor"/>
</beans>

三、测试类编写

public class UserTest {

    @Test
    public void test() {
        // 创建容器
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = ctx.getBean("userService", UserService.class);
        boolean result = service.login("小马哥","123456");
        // 关闭容器
        ctx.close();
    }
}

注:以上效果需要bean的scope是sigleton并且在测试类中手动关闭容器。如果不关闭容器,容器不会销毁,不会打印10和11的步骤。如果scope是prototype则,生命周期不由容器控制,也不会打印10和11步骤。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RiversTree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值