第一种方式,用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. Cordova热更新cordova-hot-code-push

    原文转载自:https://www.cnblogs.com/huangenai/p/7137475.html cordova-hot-code-push ,Cordova热代码推送插件提供了在应用程序 ...

  2. [原创]Modbus协议学习笔记

    一.参考资料 1.老罗传奇的2篇博文,写的不错,通俗易懂.链接地址为:http://www.cnblogs.com/luomingui/tag/Modbus/ 2.阿莫论坛精华资料:http://ww ...

  3. 【Dubbo 源码解析】04_Dubbo 服务注册&暴露

    Dubbo 服务注册&暴露 Dubbo 服务暴露过程是通过 com.alibaba.dubbo.config.spring.ServiceBean 来实现的.Spring 容器 refresh ...

  4. golang 多个worker正常关闭的示例

    代码如下,如有问题请联系 baibaibai_000@163.com package work_test import ( "math/rand" "runtime&qu ...

  5. 10.27 rest_framework(1)

    2018-10-27 16:48:04 前几天在整理django博客! 新学的rest_framework框架是源于Django的 使用的时候得 pip install djangorestframe ...

  6. R 540

    好久没写题解了嘻嘻嘻,昨天补edu自闭了一天还没补完fg这div3令人愉悦. A: #include <bits/stdc++.h> #define mk(a,b) make_pair(a ...

  7. Postman 接口测试

    使用场景: 开发接口的时候需要快速调接口的时候 测试的时候需要非常方便的调用接口.通过不同的参数去测试接口的输出 这些接口调用是需要保存下来反复运行的 在运行过程中,如果有断言,检查点就更好了第三方 ...

  8. Oracle 创建表 Create Table...

    一.创建表 主键约束primary key 约束条件,唯一且非空,一个表中只能有一个主键:有多个字段联合作为主键时,合在一起唯一标识记录,叫做联合主键. 外键约束 foreign key 受另外一张表 ...

  9. 移动端click事件

    var tap = "ontouchstart" in document.documentElement ? "touchend" : "click& ...

  10. Python学习之旅(八)

    Python基础知识(7):数据基本类型之元组.字典 一.元组 用括号把元素括起来中间用逗号隔开.用逗号分开一些值便可创建元组 1,2,3 结果: (1, 2, 3) 空元组可以用没有包含任何内容的两 ...