Spring框架生命周期十步demo

渡星河
2023-03-07 / 0 评论 / 13 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年03月07日,已超过561天没有更新,若内容或图片失效,请留言反馈。

spring.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="test" class="com.spring6.web.UserAction"/>-->
  <!--  <bean id="beanlife" class="com.spring6.service.BeanLife" init-method="init" destroy-method="destroy">
        <property name="name" value="你好"/>
    </bean>-->
    <bean class="com.spring6.service.BeanBeforePost"/>
    <bean id="beanTenLife" class="com.spring6.service.BeanTenLife" init-method="init" destroy-method="destroySingleton">
        <property name="name" value="你好"/>
    </bean>
</beans>

BeanTenLife类代码

package com.spring6.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

public class BeanTenLife implements BeanNameAware, BeanFactoryAware, BeanClassLoaderAware, InitializingBean,DisposableBean {
    private String name;

    public BeanTenLife() {
        System.out.println("构造函数启动");
    }

    public void setName(String name) {
        System.out.println("set注入启动");
        this.name = name;
    }

    public void getName() {
        System.out.println("使用Bean");
    }

    public void init() {
        System.out.println("bean初始化");
    }

    public void destroySingleton() {
        System.out.println("bean销毁");
    }
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("bean的加载器"+classLoader.getClass());
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactory工程"+beanFactory.getClass());

    }

    @Override
    public void setBeanName(String s) {
        System.out.println("setBeanName方法"+s);


    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("销毁前面执行此");
    }
}

测试类

package com.spring6.test;

import com.spring6.service.BeanLife;
import com.spring6.service.BeanTenLife;
import com.spring6.web.UserAction;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class FirstSpringTest {
    @Test
    public void Test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserAction test = (UserAction)context.getBean("test");
        test.deleteRecord();
    }

    /**
     * 七步生命周期
     */
    @Test
    public void BeanLife(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        BeanLife beanlife = context.getBean("beanlife", BeanLife.class);
        beanlife.getName();
        context.close();
    }
    /**
     * 十步生命周期
     */
    @Test
    public void BeanTenLife(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        BeanTenLife beanlife = context.getBean("beanTenLife", BeanTenLife.class);
        beanlife.getName();
        context.close();
    }
}


测试截图

2

评论 (0)

取消