Spring-Bean的生命周期

本文阅读 1 分钟
首页 代码,Java 正文

img

✨博客主页:👉 不会压弯的小飞侠

✨欢迎关注:👉点赞👍收藏⭐留言✒

✨系列专栏:👉 spring专栏

✨如果觉得博主的文章还不错的话,请三连支持一下博主。

✨欢迎大佬指正,一起学习!一起加油! img

✨生命周期

  • 生命周期︰从创建到消亡的完整过程
  • bean生命周期: bean从创建到销毁的整体过程
  • bean生命周期控制:在bean创建后到销毁前做一些事情

🔥提供生命周期的控制方法

package com.study.dao.impl;
import com.study.dao.BookDao;

public class BookDaoImpl implements BookDao { 
    public void save() { 
        System.out.println("book dao save...");
    }

    private void destroy() { 
        System.out.println("destroy...");
    }

    private void init() { 
        System.out.println("init...");
    }
}

🔥配置生命周期的控制方法

<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy"></bean>

🔥实现InitializingBean, DisposableBean接口

package com.study.service.impl;

import com.study.dao.BookDao;
import com.study.dao.impl.BookDaoImpl;
import com.study.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BookServiceImpl implements BookService, InitializingBean, DisposableBean { 
    private BookDao bookDao;
    public void save() { 
        System.out.println("BookServiceImpl...");
    }

    public void setBookDao(BookDao bookDao) { 
        this.bookDao = bookDao;
    }

    public void destroy() throws Exception { 
        System.out.println("Service Destroy...");

    }

    public void afterPropertiesSet() throws Exception { 
        System.out.println("Service afterPropertiesSet...");

    }


}

🔥BookDao接口

package com.study.dao;

public interface BookDao { 
    void save();
}

🔥BookDaoImpl实现类

package com.study.dao.impl;
import com.study.dao.BookDao;

public class BookDaoImpl implements BookDao { 
    public void save() { 
        System.out.println("book dao save...");
    }

    private void destroy() { 
        System.out.println("destroy...");
    }

    private void init() { 
        System.out.println("init...");
    }
}

🔥BookService接口

package com.study.service;

public interface BookService { 
    void save();
}

🔥BookServiceImpl实现类

package com.study.service.impl;

import com.study.dao.BookDao;
import com.study.dao.impl.BookDaoImpl;
import com.study.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BookServiceImpl implements BookService, InitializingBean, DisposableBean { 
    private BookDao bookDao;
    public void save() { 
        System.out.println("BookServiceImpl...");
    }

    public void setBookDao(BookDao bookDao) { 
        this.bookDao = bookDao;
    }

    public void destroy() throws Exception { 
        System.out.println("Service Destroy...");

    }

    public void afterPropertiesSet() throws Exception { 
        System.out.println("Service afterPropertiesSet...");

    }


}

🔥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="bookDao" class="com.study.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy"></bean>
  <!--<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl"></bean>-->
  <bean id="bookService" class="com.study.service.impl.BookServiceImpl">
    <property name="bookDao" ref="bookDao"></property>
  </bean>
</beans>

🔥Test测试类

package com.study;

import com.study.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test { 
    public static void main(String[] args) { 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bean = context.getBean(BookDao.class);
        bean.save();
        context.close();
        //context.registerShutdownHook();

    }
}
/* init... book dao save... destroy... */


/* init... Service afterPropertiesSet... book dao save... Service Destroy... destroy... */
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125414145
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复