spring中使用@PostConstruct和@PreConstruct注解
1.@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
2.@PreDestroy说明
被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。(详见下面的程序实践)
package com.yygx.flowinterface.service.impl; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import net.zoneland.uniflow.client.impl.SyncProcessServiceClient;
import net.zoneland.uniflow.client.impl.SyncTaskServiceClient; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository; /**
*
* 流程引擎客户端工具类
* @author wuhda
*
*/
@Repository
public class FlowClientUtil { private final static Logger LOGGER = LoggerFactory.getLogger(FlowClientUtil.class); /**
* 流程类接口客户端
*/
private SyncProcessServiceClient processClient; /**
* 任务类接口客户端
*/
private SyncTaskServiceClient taskClient; @PostConstruct
public void init() {
// 接入系统的账号密码,在流程管理平台(UniflowAdminConsole)上配置
String username = "ehr";
String password = "password"; // Uniflow的服务地址
String endpoint = "http://172.16.92.50:10081/UniflowControlCenter/rest"; // 构建流程类接口客户端
processClient = new SyncProcessServiceClient(username, password);
processClient.setServerAddress( endpoint + "/RestProcessService" ); // 构建任务类接口客户端
taskClient = new SyncTaskServiceClient(username, password);
taskClient.setServerAddress( endpoint + "/RestTaskService" );
} @PreDestroy
public void close() {
System.out.println("close client ...");
// 应用结束是最好关闭客户端(内部有HttpClient的连接池)
processClient.close();
taskClient.close();
} public SyncProcessServiceClient getProcessClient() {
return processClient;
} public void setProcessClient(SyncProcessServiceClient processClient) {
this.processClient = processClient;
} public SyncTaskServiceClient getTaskClient() {
return taskClient;
} public void setTaskClient(SyncTaskServiceClient taskClient) {
this.taskClient = taskClient;
} }
spring中使用@PostConstruct和@PreConstruct注解的更多相关文章
- Spring中的@Valid 和 @Validated注解你用对了吗
1.概述 本文我们将重点介绍Spring中 @Valid和@Validated注解的区别 . 验证用户输入是否正确是我们应用程序中的常见功能.Spring提供了@Valid和@Validated两个注 ...
- Java开发之@PostConstruct和@PreConstruct注解
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...
- Spring 中的事务操作、注解、以及 XML 配置
事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...
- Spring中Bean管理的常用注解
在Spring中,主要用于管理bean的注解分为四大类:1.用于创建对象.2.用于给对象的属性注入值.3.用于改变作用的范围.4.用于定义生命周期.这几个在开发中经常接触到,也可以说每天都会遇见.其中 ...
- 在spring中常被忽视的注解 @Primary
在spring 中使用注解,常使用@Autowired, 默认是根据类型Type来自动注入的. 但有些特殊情况,对同一个接口,可能会有几种不同的实现类,而默认只会采取其中一种的情况下 @Primary ...
- @PostConstruct和@PreConstruct注解
@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的void()方法.而且这个方法不能有抛出异常声明. @PostConstruct //方式1 public v ...
- 品Spring:对@PostConstruct和@PreDestroy注解的处理方法
在bean的实例化过程中,也会用到一系列的相关注解. 如@PostConstruct和@PreDestroy用来标记初始化和销毁方法. 平常更多的是侧重于应用,很少会有人去了解它背后发生的事情. 今天 ...
- Spring中通过java的@Valid注解和@ControllerAdvice实现全局异常处理。
通过java原生的@Valid注解和spring的@ControllerAdvice和@ExceptionHandler实现全局异常处理的方法: controller中加入@Valid注解: @Req ...
- Spring 中基于 AOP 的 @AspectJ注解实例
@AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格.通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的 ...
随机推荐
- DataTable与List的相互转换
List转DataTable: public static DataTable ToDataTable<T>(IEnumerable<T> collection) { var ...
- _pet
可以控制各职业召唤物的属性.用于增强BB `comment` 备注 `classIndex` 职业序号 `DmgAddPct` 宠物伤害倍率 `SpAddPct` 法术伤害 `HpAddPct`血量倍 ...
- Mac redis安装
Download, extract and compile Redis with: #进入下载目录 $ cd ... $ wget http://download.redis.io/releases/ ...
- (16)线程---定时器Timer
# ### 定时器:指定时间执行任务 from threading import Timer def func(): print("目前正在执行任务") t = Timer(5,f ...
- 20190226_xlVba提取查新标题和关键词
Sub MainProc() Dim Sht As Worksheet Dim Wb As Workbook Set Wb = Application.ThisWorkbook Set Sht = W ...
- 『Numpy』内存分析_利用共享内存创建数组
引.内存探究常用函数 id(),查询对象标识,通常返回的是对象的地址 sys.getsizeof(),返回的是 这个对象所占用的空间大小,对于数组来说,除了数组中每个值占用空间外,数组对象还会存储数组 ...
- mybatis-generator自动生成代码工具
1.在项目的配置文件中放入配置文件mybatis-generator-config.xml 根据情况修改下配置 <?xml version="1.0" encoding= ...
- 在 .NET 项目中集成 SwaggerUI(2018.9.30)
不多说,直接上教程! 1. 打开NuGet管理器搜索并安装 Swashbuckle和Swagger.Net两项 2. 修改生成设置 3. 修改SwaggerConfig文件 (1)去除注释 c.Inc ...
- decode encode
https://blog.csdn.net/crylearner/article/details/38521685,python常用的十进制.16进制.字符串.字节串之间的转换
- spring注解实现防盗链拦截
首先配置 applicationContext.xml, 添加 <!-- 启用 @AspectJ --> <aop:aspectj-autoproxy /> 新建Java工具类 ...