利用spring实现服务启动就自动执行某些操作的2种方式
第一种方式,用bean的init-method属性
<bean class="com.emax.paycenter.log.LogBridge" init-method="init"></bean>
第二种方式,实现InitializingBean接口
@Component
public class TJUnionAgentPayNotifyTCPServer implements InitializingBean { private static Logger logger = LogManager.getLogger(); @Autowired
private ThreadPoolTaskExecutor tjunionNotifyTaskExecutor; @Override
public void afterPropertiesSet() throws Exception {
logger.info("异步线程开启TCP侦听...");
// new Thread() {
// public void run() {
// openTJUnionAgentPayNotifyTCPServer();
// }
// }.start();
}
}
不过,这种在class名上声明@Component或@Service注解,当启动服务后,发现afterPropertiesSet方法被重复执行两次。寻不得果。
只好不用注解,改用声明bean的方式,spring默认每个Bean的作用域都是单例。
<bean class="com.emaxcard.tcpserver.TJUnionAgentPayNotifyTCPServer">
<property name="tjunionNotifyTaskExecutor" ref="tjunionNotifyTaskExecutor"></property>
</bean>
这种情况下,要注意,给bean的私有属性赋值时,这个属性要有公共的set方法,以让spring可以找到。
2018-11-30 10:28:03,301 ERROR [main] (ContextLoader.java:351) - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.emaxcard.tcpserver.TJUnionAgentPayNotifyTCPServer#0' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'tjunionNotifyTaskExecutor' of bean class [com.emaxcard.tcpserver.TJUnionAgentPayNotifyTCPServer]: Bean property 'tjunionNotifyTaskExecutor' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
public class TJUnionAgentPayNotifyTCPServer implements InitializingBean {
private static Logger logger = LogManager.getLogger();
private ThreadPoolTaskExecutor tjunionNotifyTaskExecutor;
public void setTjunionNotifyTaskExecutor(ThreadPoolTaskExecutor tjunionNotifyTaskExecutor) {
this.tjunionNotifyTaskExecutor = tjunionNotifyTaskExecutor;
}
@Override
public void afterPropertiesSet() throws Exception {
logger.info("异步线程开启TCP侦听...");
// new Thread() {
// public void run() {
// openTJUnionAgentPayNotifyTCPServer();
// }
// }.start();
}
}
利用spring实现服务启动就自动执行某些操作的2种方式的更多相关文章
- mybatis(二)执行CRUD操作的两种方式配置和注解
一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...
- centos7下自定义服务启动和自动执行脚本
systemctl list-units --type=service #查看所有已启动的服务 systemctl enable httpd.service #加入开机自启动服务 systemctl ...
- (一)在Spring Boot应用启动之后立刻执行一段逻辑
在Spring Boot应用启动之后立刻执行一段逻辑 1.CommandLineRunner 2.ApplicationRunner 3.传递参数 码农小胖哥:如何在Spring Boot应用启动之后 ...
- SAS启动时自动执行代码
有时候我们希望SAS启动时自动执行已经编写好的程序,可以按照以下方法实现: 首先正常打开SAS,编写我们想要让SAS启动时自动执行的代码,例如获取桌面文件夹路径,以便在其他程序中引用这个路径. pro ...
- 操作系统+编程语言的分类+执行python程序的两种方式+变量
1.什么是操作系统? 操作系统就是一个协调\管理\控制计算机硬件资源与软件资源的一个控制程序. 2.为何要操作系统? a.把复杂的硬件操作封装成简单的功能\接口用来给用户或者程序来使用(文件) b.把 ...
- idea执行maven命令的三种方式
前言: java开发的IDE工具idea默认会提供maven生命周期的图形化执行,但是如果我们需要定制化的执行命令的时候,就需要使用手动执行maven命令的方式,今天就和大家讲一下idea手动执行ma ...
- 执行python解释器的两种方式
执行python解释器的两种方式 1.交互式 python是高级语言,是解释型语言,逐行翻译,写一句翻译一句 print ('hello world') 2.命令行式 python和python解释器 ...
- 执行Go程序的三种方式及Go语言关键字
执行 Go 程序的三种方式及 Go 语言关键字 执行 Go 程序的三种方式 一.使用 go run 命令 二.使用 go build 命令 Step1. 对 go 源码源文件执行 go build 命 ...
- day05-1 执行Python程序的两种方式
目录 执行Python程序的两种方式 第一种:交互式 第二种:命令行式 三个步骤 两种方式的区别 执行Python程序的两种方式 第一种:交互式 在cmd中运行 优点:直接给出结果,执行效率高,及时报 ...
随机推荐
- 【C#】读取Excel中嵌套的Json对象,Json带斜杠的问题(其三)
除了上一篇中提到的对字符串的字符替换操作,去掉Json中的转义符反斜杠\之外,还发现了更加简单的办法. 就是使用Newtownsoft.Json序列化Json时,将嵌套的Json对象(字符串)转为JO ...
- Java中Comparable和Comparator区别
很好的一篇博客:http://blog.csdn.net/jq_ak47/article/details/61203817 http://www.cnblogs.com/cmxwt/p/6215253 ...
- JavaScript学习历程03
一闪一闪亮晶晶 <script type="text/javascript"> var nn = Number(prompt('请输入一个1-9的正整数!')); va ...
- minicom for Mac 配置
安装:brew install minicom 配置: 串口是:dev/tty.usbserial minicons -s 配置一下: 流控要关掉,不然输入不了
- laravel5.8笔记十:Redis操作
> 位置:\vendor\laravel\framework\src\Illuminate\Redis\Connections\PhpRedisConnection.php > 参考:ht ...
- TIJ -- 任务间使用管道进行输入/输出
1. 通过输入/输出在线程间进行通信通常很有用.提供线程功能的类库以“管道”的形式对线程间的输入/输出提供了支持.它们在Java输入/输出类库中的对应物就是PipedWriter类(允许任务向管道写) ...
- Java编程常见缺陷汇总(一)
[案例1] public boolean equalNode(JudgeNode a, JudgeNode b) { return a.getId() == b.getId(); } [点评] 应在 ...
- 【腾讯云的1001种玩法】几种在腾讯云建立WordPress的方法(Linux)(二)
版权声明:本文由张宁原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/126547001488207964 来源:腾云阁 ht ...
- MSVCP110.DLL没有被指定在WINDOWS上运行
要重新安装C++ 运行库 为msvcp110.dll是VC++2012的文件 数字代表版本msvcp120是VC++2013的 110是2012的 100是2010的 90是2008的 71是2005 ...
- JBPM工作流(五)——执行流程实例
概念: ProcessInstance,流程实例:代表流程定义的一次执行.如:张三昨天按请假流程请了一次假.一个流程实例包括了所有运行阶段,其中最典型的属性就是跟踪当前节点的指针,如下图. Execu ...