设为首页 加入收藏

TOP

sping入门介绍-bean标签的属性
2023-07-26 08:16:31 】 浏览:41
Tags:sping -bean

bean标签的属性

 1 、基础属性

<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>

  id : 在容器中唯一

  class : 类全路径

2、scope :对象作用范围

singleton :单例  默认 
prototype : 多例 
request :
session :
globalSession : 


3、bean的初始方法和销毁方法 标签属性

init-method :初始化方法 在创建对象时执行

destroy-method : 销毁方法 在对象销毁时执行

接口:

    public void init();
    public void destroy();

实现类:

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

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

spring容器文件:

<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" init-method="init" destroy-method="destroy"></bean>

测试:

    @Test
    public void initAndDestroyTest(){
        //加载容器
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        app.destroy();//容器销毁方法
    }

结果:

 

 

3、spring bean 生命周期:

  bean 标签的scope属性:

  singleton :单例  默认 加载容器中时,创建对象,整个容器只创建一个对象,容器销毁时bean销毁
  prototype : 多例 加载容器时不创建对象,获取时创建对象,有java虚拟机的垃圾回收机制销毁

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇用GPU来运行Python代码 下一篇《Terraform 101 从入门到实践》 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目