Spring中Bean的生命周期之-----JSR250的@PostConstruct和@PreDestroy

方法:使用JSR250;

           在初始化方法上面添加:@PostConstruct注解;在销毁方法上面添加: @PreDestroy注解;
           @PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
           @PreDestroy:在容器销毁bean之前通知我们进行清理工作

注:单实例时容器关闭的时候会销毁对象;多实例时容器不会管理这个bean,即容器不会调用销毁方法;


一、创建Bean类

用@Component标注,可以被扫描完成自动装配

在初始化方法上面添加:@PostConstruct注解;在销毁方法上面添加: @PreDestroy注解;

@PostConstruct和@PreDestroy都是Javax包中的注解。

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

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class Dog {
	
	public Dog(){
		System.out.println("dog constructor...");
	}
	
	//对象创建并赋值之后调用
	@PostConstruct
	public void init(){
		System.out.println("Dog....@PostConstruct...");
	}
	
	//容器移除对象之前
	@PreDestroy
	public void detory(){
		System.out.println("Dog....@PreDestroy...");
	}
	
}

二、配置类

 标注@ComponentScan("com.atguigu.bean"),表示扫描该包下所有有@Controller,@Service,@Repository,@Component注解的类

@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
	
	@Bean(initMethod="init",destroyMethod="detory")
	public Car car(){
		return new Car();
	}

}

三、测试

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

import com.atguigu.config.MainConfigOfLifeCycle;

public class IOCTest_LifeCycle {
	
	@Test
	public void test01(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		
		//applicationContext.getBean("car");
		//关闭容器
		applicationContext.close();
	}

}

结果:

说明:添加了注解后,容器自动调用初始化和销毁的方法。 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值