美文网首页
2019-08-22 实现ApplicationListene

2019-08-22 实现ApplicationListene

作者: 归去来ming | 来源:发表于2019-08-22 00:37 被阅读0次

最近在阅读项目代码时,有个疑问:某个service类没有定时器,也没有调用方,它是怎么跑起来的?后来请教了同事之后发现,关键在于这个类实现了ApplicationListener<ContextRefreshedEvent>接口,再加上@Service注解。这个类是在Spring启动时跟着运行的。这不是web服务,而平时常见的都是web服务,或者mq,dubbo,都有请求来源,所以犯了思维定式的错误。

ApplicationListener<ContextRefreshEvent>

百度了一下,很多文章都是用@Component注解,其实@Service与@Component都能起作用,因为@Controller, @Service, @Repository都包含了@Component :

查看spring官网:

@Component注解

也就是说,如果你不知道要在项目的业务层采用@Service还是@Component注解。那么,@Service是一个更好的选择。

相关文章

网友评论

      本文标题:2019-08-22 实现ApplicationListene

      本文链接:https://www.haomeiwen.com/subject/vfxgsctx.html