首页 > 其他分享 >如何在Spring初始化Bean或销毁Bean前执行某些操作

如何在Spring初始化Bean或销毁Bean前执行某些操作

时间:2023-06-02 21:04:42浏览次数:29  
标签:初始化 Spring void System Bean Car println message public


阅读文本大概需要3分钟。

0x01:通过在Bean中定义init-method 和 destory-method方法

1. public class Car {
2. 
3. 
4.  public Car() {
5.  System.out.println("Car's Constructor..");
6.  }
7. 
8. 
9.  public void init(){
10.  System.out.println("Car's Init...");
11.  }
12. 
13. 
14.  public void destory(){
15.  System.out.println("Car's Destroy...");
16.  }
17. 
18. 
19. }
20.  @Bean(initMethod = "init",destroyMethod = "destory")
21.  public Car car(){
22.  return new Car();
23.  }

0x02: 通过@PostConstruct和@PreDestroy方法实现初始化和销毁bean之前进行的操作


1. import javax.annotation.PostConstruct;
2. import javax.annotation.PreDestroy;
3. 
4. @Service
5. public class CustomerService
6. {
7.  String message;
8. 
9. 
10.  public String getMessage() {
11.  return message;
12.  }
13. 
14. 
15.  public void setMessage(String message) {
16.  this.message = message;
17.  }
18. 
19. 
20.  @PostConstruct
21.  public void initIt() throws Exception {
22.  System.out.println("Init method after properties are set : " + message);
23.  }
24. 
25. 
26.  @PreDestroy
27.  public void cleanUp() throws Exception {
28.  System.out.println("Spring Container is destroy! Customer clean up");
29.  }
30. 
31. }

这两个注解是JDK自带的,因此与Spring的耦合性较低(必须要Spring扫描到这个java类才能执行使用该注解的方法)

0x03: 通过bean实现InitializingBean和DisposableBean接口


1. @Service
2. public class CustomeService implements InitializingBean, DisposableBean
3. {
4.  String message;
5. 
6. 
7.  public String getMessage() {
8.  return message;
9.  }
10. 
11. 
12.  public void setMessage(String message) {
13.  this.message = message;
14.  }
15. 
16. 
17.  public void afterPropertiesSet() throws Exception {
18.  System.out.println("Init method after properties are set : " + message);
19.  }
20. 
21. 
22.  public void destroy() throws Exception {
23.  System.out.println("Spring Container is destroy! Customer clean up");
24.  }
25. 
26. }


关注我

每天进步一点点

如何在Spring初始化Bean或销毁Bean前执行某些操作_aop


标签:初始化,Spring,void,System,Bean,Car,println,message,public
From: https://blog.51cto.com/u_13538361/6404927

相关文章

  • 关于开发- springBoot 的中间件
    数据库中间件:主要用于存储和管理应用程序的数据。消息队列中间件:主要用于异步处理任务、削峰填谷、分布式解耦等场景。缓存中间件:主要用于提供快速的数据访问和响应能力,降低系统负载。搜索引擎中间件:主要用于实现全文搜索、分析数据、大规模数据聚合等场景。消息......
  • SpringCloud大文件分片上传/多线程上传
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,ie8,ie9,Chrome,Firefox,360安全浏览器,并且刷新浏览器后仍然能够续传,重启浏览器(关闭......
  • 升级spring boot异常:spring循环依赖
    问题从springboot2.2.9升级到2.6.2版本后,项目启动后访问报错Thedependenciesofsomeofthebeansintheapplicationcontextformacycle.serviceCollectionIdCacheService┌─────┐|serviceProductInfoProviderImpl↑↓|serviceOfflineProviderImpl↑......
  • springboot - 项目启动初始化数据
    1、redis配置依赖<!--redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>redisconfi......
  • SpringBoot大文件分片上传/多线程上传
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。pub......
  • Spring配置数据源
    1.Spring配置数据源1.1数据源(连接池)的作用数据源(连接池)是提高程序性能如出现的事先实例化数据源,初始化部分连接资源使用连接资源时从数据源中获取使用完毕后将连接资源归还给数据源常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等开发步骤①导入数据源的坐标和数据......
  • Spring核心接口之InitializingBean
    一、InitializingBean接口说明InitializingBean接口为bean提供了属性初始化后的处理方法,它只包括afterPropertiesSet方法,凡是继承该接口的类,在bean的属性初始化后都会执行该方法。packageorg.springframework.beans.factory;/***Interfacetob......
  • Spring核心接口之Ordered
    一、Ordered接口介绍Spring中提供了一个Ordered接口。从单词意思就知道Ordered接口的作用就是用来排序的。Spring框架是一个大量使用策略设计模式的框架,这意味着有很多相同接口的实现类,那么必定会有优先级的问题。于是Spring就提供了Ordered这个接口,来处......
  • 如何使用Spring管理Filter和Servlet
    在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext......
  • spring为什么注入接口而不是实现类?
    首先,一般使用接口是很常用并且有益的变成技术。其次,在spring中,你可以在运行过程中注入各种实现。一个很经典的情况就是在测试阶段,注入模拟的实现类。===1.网上说jdk动态代理基于实现接口。直接注入实现类会使aop失效。没有cglib可能真的就失效了。2.解耦。假如有一天实现类的名......