Spring注解之生命周期

1.什么是生命周期

生命周期是通常是指一个bean在ioc 容器中的一系过程。

  1. bean被创建(某个类的构造方法被初始化)
  2. bean的初始化这个bean初始化方法(init()\ afterPropertiesSet() \ @PostConstruct标注的方法)被调用。
  3. bean的销毁这个bean销毁方法(destroy() \ @PreDestroy标注的方法)被调用

2.生命周期中常用的注解

2.1 生命周期的实现方法 定义 init()以及destroy()

bean的定义代码

public class Blue {

    public Blue(){
        System.out.println("Blue constructor ......");
    }

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

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

生命周期的调用方法

public class MyConfing {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Blue blue(){
        return new Blue();
    }
}

2.2 bean通过实现 initlizingBean,DisposableBean接口重写相关方法

public class Green implements InitializingBean,DisposableBean{
    public Green green(){
        System.out.println("Green的构造方法执行。。。。。");
        return new Green();
    }

    //销毁方法
    @Override
    public void destroy() throws Exception {
        System.out.println("Green 的销毁方法执行。。。。");
    }
    //初始化方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Green的初始化方法执行。。。。。");
    }
}

2.3使用jsr规范的注解指定生命周期的方法

public class Gray {

    public Gray gray(){
        System.out.println("Gray 的构造方法执行。。。");
        return new Gray();
    }

    //初始化方法
    @PostConstruct
    public void init(){
        System.out.println("Gray的初始化方法执行。。。");
    }

    //销毁方法
    @PreDestroy
    public void destroy(){
        System.out.println("Gray的销毁方法执行");
    }


}

3.触发调用生命周期的方法

使用@Bean的注解方法来触发

public class MyConfing {
    //指定初始化方法
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Blue blue(){
        return new Blue();
    }
}

实现接口与使用注解方式不用手动触发,由容器来调用。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中有多种方式来定义和管理bean的生命周期。其中包括使用XML配置、注解和使用JSR250中的@PostConstruct和@PreDestroy注解。 1. XML配置方式:在XML配置文件中,可以使用<bean>标签来定义bean,并通过指定init-method和destroy-method属性来指定bean的初始化和销毁方法。例如: ```xml <bean id="user" class="com.demo.pojo.User" init-method="init" destroy-method="destroy"> </bean> ``` 在这个例子中,init-method属性指定了bean的初始化方法为"init",destroy-method属性指定了bean的销毁方法为"destroy"。 2. 注解方式:使用注解可以更简洁地定义bean的生命周期。可以使用注解@Bean来标注一个方法,该方法返回一个bean实例,并可以使用@PostConstruct和@PreDestroy注解来指定初始化和销毁方法。例如: ```java @Configuration public class AppConfig { @Bean(initMethod = "init", destroyMethod = "destroy") public User user() { return new User(); } } ``` 在这个例子中,@Bean注解标注的方法user()返回一个User实例,并通过initMethod和destroyMethod属性指定了初始化和销毁方法。 3. 使用JSR250中的@PostConstruct和@PreDestroy注解:可以在bean类中使用@PostConstruct和@PreDestroy注解来标注初始化和销毁方法。例如: ```java public class User { @PostConstruct public void init() { // 初始化方法的逻辑 } @PreDestroy public void destroy() { // 销毁方法的逻辑 } } ``` 在这个例子中,@PostConstruct注解标注的方法init()会在bean初始化之后调用,@PreDestroy注解标注的方法destroy()会在bean销毁之前调用。 总结起来,Spring提供了多种方式来定义和管理bean的生命周期,包括XML配置、注解和使用JSR250中的@PostConstruct和@PreDestroy注解。这些方式可以根据具体的需求和项目的特点来选择和使用。[1][2][3]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值