利用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中运行 优点:直接给出结果,执行效率高,及时报 ...
随机推荐
- re.S、 re.M
re.S是代表.可以匹配\n以及“ re.M是多行 code import re a = '''asdfsafhellopass: 234455 worldafdsf ''' b = re.fi ...
- Syncfusion SfDataGrid 导出Excel
var options = new ExcelExportingOptions { ExcelVersion = ExcelVersion.Excel2013, }; //不需要导出的字段 optio ...
- C语言 · 8皇后问题改编
8皇后问题(改编) 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8的棋盘. 输出格式 所能得到的最大数字和 样例输入 1 2 3 4 5 ...
- halcon 动态阈值分割之偏移值
- Hadoop 2.2.0安装和配置lzo
转自:http://www.iteblog.com/archives/992 Hadoop经常用于处理大量的数据,如果期间的输出数据.中间数据能压缩存储,对系统的I/O性能会有提升.综合考虑压缩.解压 ...
- SAR指标(转)
转自(https://zhidao.baidu.com/question/187156399.html) SAR指标又叫抛物线指标或停损转向操作点指标,其全称叫“Stop and Reverse,缩写 ...
- Linux 下MQ的安装和配置亲测
开篇之前奉上几条黄金链接: MQ参考文档 http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0m0/index.jsp?topic=%2Fcom.ib ...
- 同时安装anaconda2和anaconda3
安装的过程请参考 Ubuntu14.04下同时安装Anaconda2与Anaconda3 启动的时候cd到$HOME/anaconda2/envs/py3k/bin下 source activate ...
- google的Python风格规范
Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. ...
- Oracle创建表空间以及用户语句
记录一下常用的语句,便于以后使用… create tablespace TABLESPACENAMEdatafile 'E:\Data\ORACLEDATA\XXX.dbf' size 200M au ...