美文网首页
5. Spring4.x Bean的初始化和销毁

5. Spring4.x Bean的初始化和销毁

作者: 第八号灬当铺 | 来源:发表于2017-08-29 12:41 被阅读0次
  1. 新建一个普通的类
package com.xiaohan.ch2.prepost;

/**
 * Java配置方式
 */
public class BeanWayService {
    public void init() {
        System.err.println("@Bean-init-method");
    }

    public void destroy() {
        System.err.println("@Bean-destory-method");
    }

    public BeanWayService() {
        System.err.println("BeanWayService构造函数");
    }
}
  1. 新建一个使用注解指定了初始化方法 和 销毁前方法
package com.xiaohan.ch2.prepost;

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

/**
 * 注解方式
 */
public class AnnotationWayService {

    // 2    实例化后
    @PostConstruct
    public void init() {
        System.err.println("Annotation-init-method");
    }

    // 3    销毁前
    @PreDestroy
    public void destroy() {
        System.err.println("Annotation-destory-method");
    }

    // 1    构造
    public AnnotationWayService() {
        System.err.println("AnnotationWayService构造函数");
    }
}
  1. 配置类
package com.xiaohan.ch2.prepost;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@ComponentScan("com.xiaohan.ch2.prepost")
public class PrePostConfig {

    @Bean
    public AnnotationWayService annotationWayService() {
        return new AnnotationWayService();
    }

    // 指定初始化方法 和 销毁前方法
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public BeanWayService beanWayService() {
        return new BeanWayService();
    }
}
  1. Main测试
package com.xiaohan.ch2.prepost;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

// Bean的初始化和销毁
public class Main {
    public static void main(String[] args) {
        // 这里不使用ApplicationContext接口接收了 因为没有close方法
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrePostConfig.class);
        ac.close();
    }
}

输出 可以看到 先实例化的 后销毁

AnnotationWayService构造函数
Annotation-init-method
BeanWayService构造函数
@Bean-init-method
@Bean-destory-method
Annotation-destory-method

相关文章

  • Spring生命周期

    1. @Bean初始化和销毁 1.1 bean的生命周期:   bean创建-----初始化-----销毁的过程 ...

  • 5. Spring4.x Bean的初始化和销毁

    新建一个普通的类 新建一个使用注解指定了初始化方法 和 销毁前方法 配置类 Main测试 输出 可以看到 先实例...

  • Spring 生命周期

    Bean生命周期 Bean创建 --> 初始化 --> 销毁过程 容器管理Bean生命周期 自定义初始化和销毁方法...

  • Bean的生命周期:

    配置Bean的初始化和销毁的方法:配置初始化和销毁的方法: init-method=”setup” destroy...

  • spring注解驱动开发(2)——组件注册

    指定初始化和销毁函数的方法有 @Bean注解指定初始化和销毁方法 组件实现InitializingBean,Dis...

  • spring基础(1)

    1、Bean的初始化和销毁 (1)、java配置方式:使用@Bean的initMethod和destroyMe...

  • Spring注解06 @Bean生命周期

    bean的生命周期:bean创建---初始化----销毁的过程容器管理bean的生命周期: 我们可以自定义初始化和...

  • Spring bean 生命周期

    bean的生命周期:bean创建---初始化----销毁的过程容器管理bean的生命周期;我们可以自定义初始化和销...

  • Bean生命周期

    bean的生命周期:创建----->初始化----->销毁自定义初始化和销毁方法新建Car类,添加@Compone...

  • 2.spring bean生命周期

    目录 1.bean的生命周期 创建->初始化->销毁 1.@Bean initMethod和destroyMeth...

网友评论

      本文标题:5. Spring4.x Bean的初始化和销毁

      本文链接:https://www.haomeiwen.com/subject/yfsodxtx.html