Spring(11):注解实现Bean初始化和销毁

一、实现功能

通过注解的方法,实现java Bean的初始化以及销毁方法。

二、环境

1.Pom

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <!--单元测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

 

三、具体实现

1. 方法一:通过接口实现

(1)TestBean 类

package com.spring.ioc;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2019/10/27.
 */
@Component
public class TestBean implements InitializingBean,DisposableBean{
    public void destroy() throws Exception {
        System.out.println("TestBean.destroy");
    }

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

(2)TestConfiguration 类

package com.spring.ioc;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2019/10/27.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class TestConfiguration {

}


(3)测试代码

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

/**
 * Created by Administrator on 2019/10/27.
 */
public class TestCode {
    @Test
    public void test(){
        AbstractApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);

        TestBean testBean=context.getBean("testBean",TestBean.class);
        System.out.println("testBean = " + testBean);
        context.close();
    }
}

结果:

TestBean.afterPropertiesSet
testBean = com.spring.ioc.TestBean@123f1134
TestBean.destroy

2.方法二:直接在TestBean类中定义方法
(1)TestBean 类

package com.spring.ioc;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by Administrator on 2019/10/27.
 */
@Component
public class TestBean implements InitializingBean,DisposableBean{
    public void destroy() throws Exception {
        System.out.println("TestBean.destroy");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("TestBean.afterPropertiesSet");
    }
    @PostConstruct
    public void onInit(){
        System.out.println("TestBean onInit");
    }
    @PreDestroy
    public void onDestroy(){
        System.out.println("TestBean onDestroy");
    }
}

(2)TestConfiguration 类

package com.spring.ioc;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2019/10/27.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class TestConfiguration {

}

(3)测试

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

/**
 * Created by Administrator on 2019/10/27.
 */
public class TestCode {
    @Test
    public void test(){
        AbstractApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);

        TestBean testBean=context.getBean("testBean",TestBean.class);
        System.out.println("testBean = " + testBean);
        context.close();
    }
}

结果

TestBean onInit
TestBean.afterPropertiesSet

testBean = com.spring.ioc.TestBean@2c34f934
TestBean onDestroy
TestBean.destroy

3.方法三:自定义方法,方法不需要注解(只用于@Bean)

备注:这个需要使用@Bean注解,而@Component注解是没有这两个方法的。
(1)TestBean :添加方法,并且去除@Component方式,而使用方法方式交由spring管理

package com.spring.ioc;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by Administrator on 2019/10/27.
 */
//@Component
public class TestBean implements InitializingBean,DisposableBean{
    public void destroy() throws Exception {
        System.out.println("TestBean.destroy");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("TestBean.afterPropertiesSet");
    }
    @PostConstruct
    public void onInit(){
        System.out.println("TestBean onInit");
    }
    @PreDestroy
    public void onDestroy(){
        System.out.println("TestBean onDestroy");
    }

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

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


(2)TestConfiguration 类

package com.spring.ioc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2019/10/27.
 */
@Configuration
@ComponentScan("com.spring.ioc")
public class TestConfiguration {

    @Bean(initMethod = "onInit1",destroyMethod = "onDestroy1")
    public TestBean testBean(){
        return new TestBean();
    }
}


(4)    测试代码

package com.spring.ioc;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

/**
 * Created by Administrator on 2019/10/27.
 */
public class TestCode {
    @Test
    public void test(){
        AbstractApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);

        TestBean testBean=context.getBean("testBean",TestBean.class);
        System.out.println("testBean = " + testBean);
        context.close();
    }
}

结果:

TestBean onInit
TestBean.afterPropertiesSet
TestBean onInit1
testBean = com.spring.ioc.TestBean@14a2f921
TestBean onDestroy
TestBean.destroy
TestBean onDestroy1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值