spring init
DN学院讲师招募 Markdown编辑器轻松写博文 TOP 50 CTO坐镇直招 读文章说感想获好礼
通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
下面演示通过 @PostConstruct 和 @PreDestory
1:定义相关的实现类:
- package com.myapp.core.annotation.init;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- public class PersonService {
- private String message;
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- @PostConstruct
- public void init(){
- System.out.println("I'm init method using @PostConstrut...."+message);
- }
- @PreDestroy
- public void dostory(){
- System.out.println("I'm destory method using @PreDestroy....."+message);
- }
- }
2:定义相关的配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
- <context:annotation-config />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
测试类:
- package com.myapp.core.annotation.init;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml");
- PersonService personService = (PersonService)context.getBean("personService");
- personService.dostory();
- }
- }
测试结果:
I'm init method using @PostConstrut....123
I'm destory method using @PreDestroy.....123
其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
类来告诉Spring容器采用的 常用 注解配置的方式:
只需要修改配置文件为:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> -->
- <!-- <context:annotation-config /> -->
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
- <bean id="personService" class="com.myapp.core.annotation.init.PersonService">
- <property name="message" value="123"></property>
- </bean>
- </beans>
同样可以得到以上测试的输出结果。
转载于:https://www.cnblogs.com/huboking/p/4403497.html
spring init相关推荐
- Spring Init Destory
<beanid="initDestroy"class="com.sanmao.spring.ioc.InitDestroy"init-method=&qu ...
- 聊聊Spring Boot服务监控,健康检查,线程信息,JVM堆信息,指标收集,运行情况监控等!...
来自:https://juejin.im/post/5e2179def265da3e152d2561 前言 去年我们项目做了微服务1.0的架构转型,但是服务监控这块却没有跟上.这不,最近我就被分配了要 ...
- Spring Boot 服务监控,健康检查,线程信息,JVM堆信息,指标收集,运行情况监控...
作者:Richard_Yi 来源:http://39sd.cn/B2A0B 去年我们项目做了微服务1.0的架构转型,但是服务监控这块却没有跟上.这不,最近我就被分配了要将我们核心的微服务应用全部监控起 ...
- springboot原生mysql写法_【Rainbond最佳实践】Spring Boot框架配置MySQL
Rainbond开源软件介绍: Rainbond是国内首个开源的生产级无服务器PaaS. 深度整合基于Kubernetes的容器管理.多类型CI/CD应用构建与交付.多数据中心的资源管理等技术,提供云 ...
- SpringBoot快速开发利器:Spring Boot CLI
今日推荐 推荐一个 Java 接口快速开发框架干掉Random:这个类已经成为获取随机数的王者Docker + Intellij IDEA,提升 10 倍生产力!笑出腹肌的注释,都是被代码耽误的诗人! ...
- 快速创建springboot项目:使用http://start.spring.io/网页创建,使用IDEA的Spring Initializr创建,使用SpringBoot CLI创建
1 快速创建SpringBoot项目 快速创建SpringBoot项目至少可以通过两种方式: 1.使用http://start.spring.io/网页版的创建 2.使用IDEA创建 1.1.通过访问 ...
- Spring Boot CMI 使用笔记
Spring Boot 提供的优越性 1. 自动配置,不需要配置很多的bean, 会根据class 中的jar 文件来自动配置bean 2. 起步依赖,利用Sping starter强大的功能,只需简 ...
- spring boot简介_Spring Boot简介
spring boot简介 在本教程中,我们将看一下Spring Boot,看看它与Spring框架有何不同. 我们还将讨论Spring Boot提供的各种功能. 什么是Spring Boot? 在开 ...
- yeoman_具有Spring Boot和Yeoman的单页Angularjs应用程序
yeoman 我非常感谢yeoman之类的工具,这些工具提供了一种非常快速的方法来将不同的javascript库组合在一起成为一个一致的应用程序. Yeoman提供了UI层,如果您需要开发服务层和静态 ...
- 正义联盟的Spring靴
正义联盟的黑暗时代已经来临,强大的Darkseid即将征服人类. 蝙蝠侠在<神力女超人>的帮助下,努力使联盟与一个关键方面失联. 适当的正义联盟成员管理系统. 由于时间不在他们身边,他们不 ...
最新文章
- js markdown chart flow
- python中PIL模块
- 王兴最好的半年结束了
- spark-jar冲突解决方案
- Teams Bot如何解析和发送 at 用户
- iOS进阶之正则表达式
- 看完这篇Redis缓存三大问题,够你和面试官battle几回合了
- 金店管理系统的几个经验和教训
- vim 系统剪切板及 ubuntu 无法使用 vim 系统剪贴板(或 :reg 无+号寄存器)
- SDUT OJ C语言实验——Hello World!(printf练习)
- 【无标题】对于_, predicted = torch.max(outputs.data, 1)的理解
- 【Unity】Unity 几何知识、弧度、三角函数、向量运算、点乘、叉乘
- VC中用代码移动CDockPane的位置
- CPU频率与内存频率的关系
- SSM之spring事务管理
- verbose=True
- 黑马程序员:IP地址:InetAddress类的介绍(无构造函数)
- 2022-2027年中国大米蛋白粉行业市场全景评估及发展战略规划报告
- 2020年甘肃省土地利用数据(矢量)
- 关于Image.FromFile方法处理图像出现内存不足的问题