SSM学习4:bean的生命周期

  目录

建立空项目 

建立一个模块:maven项目

建立数据层dao和业务层service的接口和实现类文件

BookDao.class

BookDaoimpl.class

BookService.class

BookServiceimpl.class

建立测试类app.class

在pom.xml导入spring的两个坐标

建立配置文件applicationContext.xml,

测试结果


  • init-method:指定类中的初始化方法名称
  • destory-method:指定类中销毁方法名称

建立空项目 

建立一个模块:maven项目

建立数据层dao和业务层service的接口和实现类文件

目录如下:

BookDao.class

package com.itheima.dao;

public interface BookDao {
    public void save();

}

BookDaoimpl.class

package com.itheima.dao.impl;

import com.itheima.dao.BookDao;

public class BookDaoimpl implements BookDao {
    public void save() {
        System.out.println("BookDao运行了");
    }
//测试生命周偶器
    public void init(){
        System.out.println("初始化成功");
    }
    public void destroy(){
        System.out.println("程序关闭");
    }
}

BookService.class

package com.itheima.service;

public interface BookService {
    public void save();
}

BookServiceimpl.class

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BookServiceimpl implements BookService, InitializingBean, DisposableBean {
    private BookDao book;

    public void save() {
        System.out.println("BookService运行了");
        book.save();


    }
    //alt+insert快速生成set方法,再去配置文件中让数据层和业务层有关联

    public void setBook(BookDao book) {
        this.book = book;
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("service结束");

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("service初始化了");
    }
}

建立测试类app.class

package com.itheima;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App3 {
    public static void main(String[] args) {
        //获取ioc容器
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

        //测试bean的作用范围
        BookDao aa = (BookDao) ctx.getBean("aa");
        aa.save();
        //关闭程序
        //利用关闭钩子
        ctx.registerShutdownHook();
       ctx.close();//暴力关闭



    }
}

在pom.xml导入spring的两个坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

建立配置文件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、-->
<!--    起别名name,作用范围prototype
-->
    <bean id="bookdao1" name="aa bb cc" class="com.itheima.dao.impl.BookDaoimpl" init-method="init" destroy-method="destroy" />
    <bean id="bookservice1"  name="kk" class="com.itheima.service.impl.BookServiceimpl">

<!--                配置service和dao发生关系-->
<!--        <property name="book" ref="aa"></property>-->
        <property name="book" ref="aa"></property>
<!--        <property name="book" ref=bb"></property>-->
    </bean>
</beans>

测试结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值