利用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中运行 优点:直接给出结果,执行效率高,及时报 ...
随机推荐
- 60cms Cookies欺骗漏洞审计
源码地址:https://files.cnblogs.com/files/ssooking/60cms.zip 运行60cms目录下的Netbox.exe即可开启Asp Web服务,默认端口80 环境 ...
- 2.4G还是5G?带你选择最正确的路由器
智能设备井喷的时代,无线路由器成为家庭中最重要的电器设备.稳定性.连接速度.信号强弱都是无线路由使用体验的重要组成部分.究竟如何选购与配置路由器才能得到最好的用户体验呢? 当你在选购无线路由器的时候是 ...
- 【iCore4 双核心板_ARM】例程二十五:LWIP_DNS实验——域名解析
实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...
- [加密]openssl之数字证书签名,CA认证原理及详细操作
转自:http://blog.sina.com.cn/s/blog_cfee55a70102wn3h.html 1 公钥密码体系(Public-key Cryptography) 公钥密码体系,又称非 ...
- Windows 10 Version 1803 (Updated March 2018) MSDN 镜像下载
微软已按先前预期于美国时间 2018 年 4 月 30 日正式向所有用户发布了 Windows 10 Version 1803 (Updated March 2018) 的 ISO 镜像下载,按微软官 ...
- 使用for of循环遍历获取的nodeList,配置babel编译,webpack打包之后在iphone5下报错
报错信息: Symbol.iterator is not a function [duplicate] 代码示例: function insertCta() { let ctaArr = Array. ...
- shell脚本学习笔记(符号)
shell脚本的学习: 1.Shell的作用是解释运行用户的命令,用户输入一条命令,Shell就解释运行一条,这样的方式称为交互式(Interactive),Shell还有 一种运行命令的方式称为批处 ...
- 安装Oracle Database 11g 找不到文件“WFMLRSVCApp.ear” .
在64位Windows 7 系统下安装Oracle Database 11g 的过程中,出现提示:“未找到文件D:\app\Administrator\product\11.2.0\dbhome_1\ ...
- 0710 mux协议的作用(ppp拨号时如何和gprs进行at指令交互)
ppp拨号使gprs上网的同时如何和gprs模块进行at指令的交互,这是一个问题. 在linux中,ppp拨号上网是内核中支持的,只需要在内核配置中选上. ppp拨号的方式使gprs进行上网与at指令 ...
- java基础---->String中replace和replaceAll方法
这里面我们分析一下replace与replaceAll方法的差异以及原理. replace各个方法的定义 一.replaceFirst方法 public String replaceFirst(Str ...