码迷,mamicode.com
首页 > 编程语言 > 详细

(002)spring容器中bean初始化、销毁时执行的方法及其3种实现方式

时间:2019-11-02 17:57:48      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:rop   sed   spring容器   ESS   err   rri   spl   creat   itme   

spring容器中bean初始化或者销毁时会执行一些方法,有3种实现方式。

1、实现InitializingBean、DisposableBean接口,在bean的属性设置后和bean销毁时分别执行afterPropertiesSet和destroy方法

  pom.xml文件

技术图片
<?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>com.edu.spring</groupId>
    <artifactId>spring</artifactId>
    <version>1.0.0</version>

    <name>spring</name>
    <!-- FIXME change it to the project‘s website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>
View Code

  Cat.java

技术图片
package com.edu.spring;

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

public class Cat implements InitializingBean,DisposableBean{

    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("-----------afterPropertiesSet--------------");
    }

    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("-----------destroy--------------");
    }

}
View Code

  MyConfig.java

技术图片
package com.edu.spring;

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

@Configuration
public class MyConfig {
    
    @Bean
    public Cat createCat(){
        return new Cat();
    }
}
View Code

  App.java

技术图片
package com.edu.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App 
{
    public static void main( String[] args )
    {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class);
        System.out.println(context.getBean(Cat.class));
        context.close();
    }
}
View Code

  运行结果如下:

技术图片

 2、在@Bean注解中用initMethod和destroyMethod两个属性指定实体类中的两个方法,这两个方法分别在bean初始化和销毁时候执行

  Dog.java

技术图片
package com.edu.spring;

public class Dog {

    public void init(){
        System.out.println("----------Dog init---------");
    }
    
    public void destroy(){
        System.out.println("----------Dog destroy---------");
    }
}
View Code

  MyConfig.java

技术图片
package com.edu.spring;

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

@Configuration
public class MyConfig {
    
    @Bean(initMethod="init",destroyMethod="destroy")
    public Dog createDog(){
        return new Dog();
    }
}
View Code

  App.java

技术图片
package com.edu.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App 
{
    public static void main( String[] args )
    {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class);
        System.out.println(context.getBean(Dog.class));
        context.close();
    }
}
View Code

  运行结果如下:

技术图片

 3、在实体类中方法上面分别添加@PostConstruct和@PreDestroy注解,分别指定初始化和销毁方法

  Animal.java

技术图片
package com.edu.spring;

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

public class Animal {

    @PostConstruct
    public void initial(){
        System.out.println("-------initial---------");
    }
    
    @PreDestroy
    public void close(){
        System.out.println("---------close---------");
    }
}
View Code

  MyConfig.java

技术图片
package com.edu.spring;

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

@Configuration
public class MyConfig {
    
    @Bean
    public Animal createAniaml(){
        return new Animal();
    }
}
View Code

  App.java

技术图片
package com.edu.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App 
{
    public static void main( String[] args )
    {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfig.class);
        System.out.println(context.getBean(Animal.class));
        context.close();
    }
}
View Code

  运行结果如下:

技术图片

 

 

(002)spring容器中bean初始化、销毁时执行的方法及其3种实现方式

标签:rop   sed   spring容器   ESS   err   rri   spl   creat   itme   

原文地址:https://www.cnblogs.com/javasl/p/11783018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!