第一种方式,用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种方式的更多相关文章

  1. mybatis(二)执行CRUD操作的两种方式配置和注解

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...

  2. centos7下自定义服务启动和自动执行脚本

    systemctl list-units --type=service #查看所有已启动的服务 systemctl enable httpd.service #加入开机自启动服务 systemctl ...

  3. (一)在Spring Boot应用启动之后立刻执行一段逻辑

    在Spring Boot应用启动之后立刻执行一段逻辑 1.CommandLineRunner 2.ApplicationRunner 3.传递参数 码农小胖哥:如何在Spring Boot应用启动之后 ...

  4. SAS启动时自动执行代码

    有时候我们希望SAS启动时自动执行已经编写好的程序,可以按照以下方法实现: 首先正常打开SAS,编写我们想要让SAS启动时自动执行的代码,例如获取桌面文件夹路径,以便在其他程序中引用这个路径. pro ...

  5. 操作系统+编程语言的分类+执行python程序的两种方式+变量

    1.什么是操作系统? 操作系统就是一个协调\管理\控制计算机硬件资源与软件资源的一个控制程序. 2.为何要操作系统? a.把复杂的硬件操作封装成简单的功能\接口用来给用户或者程序来使用(文件) b.把 ...

  6. idea执行maven命令的三种方式

    前言: java开发的IDE工具idea默认会提供maven生命周期的图形化执行,但是如果我们需要定制化的执行命令的时候,就需要使用手动执行maven命令的方式,今天就和大家讲一下idea手动执行ma ...

  7. 执行python解释器的两种方式

    执行python解释器的两种方式 1.交互式 python是高级语言,是解释型语言,逐行翻译,写一句翻译一句 print ('hello world') 2.命令行式 python和python解释器 ...

  8. 执行Go程序的三种方式及Go语言关键字

    执行 Go 程序的三种方式及 Go 语言关键字 执行 Go 程序的三种方式 一.使用 go run 命令 二.使用 go build 命令 Step1. 对 go 源码源文件执行 go build 命 ...

  9. day05-1 执行Python程序的两种方式

    目录 执行Python程序的两种方式 第一种:交互式 第二种:命令行式 三个步骤 两种方式的区别 执行Python程序的两种方式 第一种:交互式 在cmd中运行 优点:直接给出结果,执行效率高,及时报 ...

随机推荐

  1. 2.4G还是5G?带你选择最正确的路由器

    智能设备井喷的时代,无线路由器成为家庭中最重要的电器设备.稳定性.连接速度.信号强弱都是无线路由使用体验的重要组成部分.究竟如何选购与配置路由器才能得到最好的用户体验呢? 当你在选购无线路由器的时候是 ...

  2. 【资料下载区】【iCore1S相关代码、资料下载地址】更新日期2017/10/09

    [iCore1S相关文档][更新中...] iCore1S原理图(PDF)下载iCore1S引脚注释(PDF)下载 [iCore1S相关例程代码][ARM][更新中...] DEMO1.0测试程序发布 ...

  3. Zookeeper —— 初识

    什么是 Zookeeper Zookeeper 是一个开放源代码的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现: Zookeeper 是典型的分布式数据一致性的解决方案,分布 ...

  4. IE8 下更改input[file] file文件选择框样式

    1/使用绝对定位,将文件选择框固定,并且隐藏该选择框(文件选择框可调整宽高),设置该文件选择框 z-index 调高 比如 999. 2/使用任意标签,调整为与上面选择框相同宽高,目的为使用该标签样式 ...

  5. C++笔试面试题整理

    朋友给出的一些常见的C++面试题,特整理如下,后期遇到新的再更新. 面试题 列举并解释C++中的四种运算符转化,说明它们的不同点: static_cast: 在功能上基本上与C风格的类型转换一样强大, ...

  6. JS中toString()、toLocaleString()、valueOf()的区别

    前言 Array.Boolean.Date.Number等对象都具有 toString().toLocaleString().valueOf()三个方法,那这三个方法有什么区别? 一.JS Array ...

  7. java多线程的3种写法

    1.Thread /** * */ package testJava2.thread; /** * @author xxx.sjtu * @function * @date 2017年5月17日 * ...

  8. xcode8 iOS函数返回值使用警告

    没有使用返回值时, 警告 swift: @warn_unused_result func doSomething() -> Bool { return true } OC: - (BOOL)do ...

  9. phpcms栏目标签调用

    $CATEGORY[$catid][catid]                栏目id   $CATEGORY[$catid][module]                栏目所在的模块   $C ...

  10. weblogic反序列化漏洞CVE-2018-2628-批量检测脚本

    #coding=utf-8 import socket import time import re,os,sys,codecs type = 'utf-8' reload(sys) sys.setde ...