spring boot -- 监听器

一些系统配置,存储在数据库中,需要在项目启动时加载进缓存。此时可以实现ApplicationListener类来实现监听。

第一步:

package com.lic.base.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.lic.app.system.service.impl.SysConfigService;
import com.lic.base.tools.CacheHelp;

public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent>{

	protected Logger log = LoggerFactory.getLogger(StartApplicationListener.class);
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		log.info("加载系统配置...");
		
		SysConfigService sysConfigService = event.getApplicationContext().getBean(SysConfigService.class);
		
		CacheHelp.SYS_CONFIG_MAP = sysConfigService.selectKeyValueByGroupCode(null);
		
		log.info("系统配置加载完成...");
	}

}
这个类 不会自动注册,需要我们手动注册。

第二步:

package com.lic.boot;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.lic.base.servlet.StartApplicationListener;

@SpringBootApplication
@ImportResource("classpath:/spring/applicationContext.xml")
//@Configuration
@EnableAutoConfiguration
//@ComponentScan("main.java.com.huike")
// @EnableEurekaServer
public class StartApplication {

	private static final Logger log = LoggerFactory.getLogger(StartApplication.class);

	public static void main(String[] args) {
		log.info("服务开始启动了");
		SpringApplication app = new SpringApplication(StartApplication.class);
		app.addListeners(new StartApplicationListener());
		app.run(args);
	}
}

运行可以看到控制台打印:

04-27 15:52:23.917 [main] INFO  com.huike.boot.StartApplication - 服务开始启动了
04-27 15:52:24.489 [background-preinit] INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.2.1.Final

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.3.RELEASE)

04-27 15:52:24.870 [main] INFO  com.huike.boot.StartApplication - No active profile set, falling back to default profiles: default
04-27 15:52:25.445 [main] WARN  org.mybatis.spring.mapper.ClassPathMapperScanner - No MyBatis mapper was found in '[com.huike.boot]' package. Please check your configuration.
04-27 15:52:26.021 [main] INFO  org.apache.catalina.core.StandardService - Starting service Tomcat
04-27 15:52:26.022 [main] INFO  org.apache.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.6
04-27 15:52:26.103 [localhost-startStop-1] INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
04-27 15:52:27.716 [main] INFO  com.huike.base.servlet.StartApplicationListener - 加载系统配置...
04-27 15:52:28.013 [main] DEBUG com.huike.app.system.service.ISysConfigService.selectKeyValueByGroupCode - ==>  Preparing: select ckey, cvalue from sys_config where 1 = 1 
04-27 15:52:28.030 [main] DEBUG com.huike.app.system.service.ISysConfigService.selectKeyValueByGroupCode - ==> Parameters: 
04-27 15:52:28.045 [main] DEBUG com.huike.app.system.service.ISysConfigService.selectKeyValueByGroupCode - <==      Total: 21
04-27 15:52:28.049 [main] INFO  com.huike.base.servlet.StartApplicationListener - 系统配置加载完成...
04-27 15:52:28.054 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8090"]
04-27 15:52:28.062 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler [http-nio-8090]
04-27 15:52:28.070 [main] INFO  org.apache.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read
04-27 15:52:28.088 [main] INFO  com.huike.boot.StartApplication - Started StartApplication in 4.088 seconds (JVM running for 4.621)

打成jar包是可以使用的,但是打成war包就不能使用了。解决办法是在监听器上加一个@Component注解。

package com.huike.base.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.huike.app.system.service.impl.SysConfigService;
import com.huike.base.tools.CacheHelp;

@Component
public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent>{

	protected Logger log = LoggerFactory.getLogger(StartApplicationListener.class);
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		log.info("加载系统配置...");
		
		SysConfigService sysConfigService = event.getApplicationContext().getBean(SysConfigService.class);
		
		CacheHelp.SYS_CONFIG_MAP = sysConfigService.selectKeyValueByGroupCode(null);
		
		log.info("系统配置加载完成...");
	}

}
这样也会在项目启动时执行。



  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: rocketmq-spring-boot-starter是一个基于Spring Boot框架的RocketMQ客户端启动器,可以方便地在Spring Boot应用中集成RocketMQ消息队列服务。它提供了一些自动配置和便捷的注解,使得开发者可以快速地使用RocketMQ进行消息的发送和消费。同时,它还支持RocketMQ的高可用、负载均衡、顺序消息等特性。 ### 回答2: RocketMQ是一个分布式的消息中间件系统,是由阿里巴巴公司开发和维护的。RocketMQ在分布式系统中能够提供高可用、高性能、可靠的消息服务。由于RocketMQ经历了多年的实践和迭代,因此它拥有了广泛的用户群体和丰富的功能特性。而RocketMQ-Spring-Boot-Starter就是为了更加方便RocketMQ在Spring Boot应用中的使用而开发的。下面就来介绍一下RocketMQ-Spring-Boot-Starter的使用和特点。 RocketMQ-Spring-Boot-Starter能够非常方便地集成RocketMQ消息系统到Spring Boot应用中。它提供了很多的配置选项,能够以最少的配置来完成RocketMQ的使用。在使用过程中,只需引入相应的依赖,然后在配置文件中添加配置即可。在消息生产者方面,只需要编写一个简单的类就可以实现发送消息的操作。在消息消费者方面,只需要编写一个监听器类就可以实现消息的接收和处理。而且RocketMQ-Spring-Boot-Starter提供了一些常用的RocketMQ操作,如日志记录、重试机制、事务消息等等,可以方便地进行日常开发和维护。 RocketMQ-Spring-Boot-Starter还有一些其他的特点。比如,它支持集成RocketMQ的多个版本,可以兼容不同版本的应用。同时,RocketMQ-Spring-Boot-Starter还支持多种序列化方式,如JSON、Protobuf等,可以满足不同的应用场景要求。而且,RocketMQ-Spring-Boot-Starter还提供了自定义的序列化插件和编码器,可以进一步满足自定义需求。 总之,RocketMQ-Spring-Boot-Starter是一个强大而实用的RocketMQ消息中间件集成框架。它可以有效地提高RocketMQ消息中间件的开发效率和应用性能,为开发人员提供更好的开发和维护体验。 ### 回答3: rocketmq-spring-boot-starter是一个开源项目,基于Spring Boot框架及Apache RocketMQ消息队列实现的一款快速构建分布式应用的工具,使得RocketMQ在Spring Boot框架下快速应用成为了一件简单的事情。该项目可以避免使用RocketMQ时需要手动编写大量重复的配置代码,还可以便捷地实现RocketMQ的消息发送和消费。 在使用rocketmq-spring-boot-starter的时候,用户只需要简单配置相关配置项,就可以实现轻松的RocketMQ消息队列的使用。通过整合Spring Boot消息组件让开发者使用RocketMQ变得更加方便和容易。rocketmq-spring-boot-starter将RocketMQ客户端的配置和实例化放在自己的工程内,并将这些配置文件和实例自动注入到Spring Boot的应用环境中,从而让使用者可以直接使用aop来使用RocketMQ的消息发送和消费功能。 此外,该项目还具有简单易用、高可靠、无侵入等特点,方便了开发者的代码管理和维护。用户可以少写一些繁琐重复的代码,只需在配置文件中配置相关项,就可以快速完成RocketMQ的使用,并利用Spring Boot的依赖注入,让代码结构更加简洁清晰,可维护性更高。 总之,rocketmq-spring-boot-starter的出现让使用RocketMQ消息队列的开发者可以更加轻松高效地进行消息处理,而且这个项目也在GitHub上得到了广泛的使用和支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zerlinda_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值