【SSM-Spring】Spring中bean的十一级台阶生命周期

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1.类

package com.ioc;

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.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public  class Man implements BeanNameAware, ApplicationContextAware, BeanPostProcessor, InitializingBean, DisposableBean {
    private String name;
public String getName() {
    return name;
}

public void setName(String name) {
    System.out.println("第二步 类属性的set方法 设置属性");
    this.name = name;
}

public Man(){
    System.out.println("第一步: 类的构造方法 ");
}

public void setUp(){
    System.out.println(" 第七步 执行 bean 标签中 init-mothod方法   man 被创建");
}
public void setDown(){
    System.out.println("第11步 bean 标签中destory-method方法 man 被销毁了");
}

@Override
public void setBeanName(String s) {
    System.out.println("第三步 实现BeanNameAware 执行 setBeanName");
}


@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    System.out.println("第四步 实现BeanFactoryAware  了解工厂信息");
}

@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
    System.out.println("第5步 实现BeanPostProcess before初始化前执行方法");
    return o;
}

@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
    System.out.println("第8步 实现 BeanPostProcessor after初始化后执行方法");
    return o;
}

@Override
public void afterPropertiesSet() throws Exception {
    System.out.println("第6步 实现InitializeingBean 属性设置后执行方法");
}

public void run(){
    System.out.println("第9步 执行自身的方法");
}

@Override
public void destroy() throws Exception {
    System.out.println("第10步 实现DisposaleBean 执行销毁bean的方法");
}
}

2.Test

package com.test;

import com.ioc.Man;
import com.ioc.UserService;
import com.ioc.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo1 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml"
        );
        Man man =(Man) context.getBean("manbean");

        man.run();
        context.close();

    }
}

3.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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="ubean" class="com.ioc.UserServiceImpl">
     <property name="name" value="hello world Spring"></property>
 </bean>

    <bean id="manbean" class="com.ioc.Man"
          init-method="setUp" destroy-method="setDown">
        <property name="name" value="设置属性"/>
    </bean>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值