当前位置: 首页 > article >正文

Spring(三)Spring事件+计划任务+条件注解+SpringAware

Application Event 事件

当一个Bean处理完一个任务之后,希望另一个Bean知道并做出相应的处理,这时需要让另外一个Bean监听当前Bean所发送的事件

  • 自定义事件,集成ApplicationEvent
  • 自定义事件监听器,实现ApplicationListener
  • 使用容器发布事件

①、自定义事件

public class DemoEvent extends ApplicationEvent{
	
	private static final long serialVersionUID = 1L;

	private String msg;

	public DemoEvent(Object source,String msg){
		super(source);
		this.msg = msg;
	}

	public String getMsg(){
		return msg;
	}
	public void setMsg(String msg){
		this.msg = msg;
	}
}

②、事件监听器

实现AppliationListener接口,并指定监听的事件类型DemoEvent
使用onApplicationEvent方法对消息进行接收处理

@Component
public class DemoListener implements ApplicationListener<DemoEvent>{

	public void onApplicationEvent(DemoEvent event){
		String msg = event.getMsg();
		System.out.println("bean-demoListener接收到了bean-demoPublisher发布的消息:"+msg);
	}
}

③、事件发布类

@Component
public class DemoPublisher{

	@Autowired
	ApplicationContext applicationContext;//注入该引用,用来发布事件

	public void publish(String msg){//发布
		applicationContext.publishEvent(new DemoEvent(this,msg));
	}
}

④、配置类

@Configuration
@ComponentScan("com.xxx.event");
public class EventConfig{

}

⑤、运行

public class Main{
	
	public static void main(String[] args){
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
		
		DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
		demoPublisher.publish("hello application event");
		context.close();
	}
}

计划任务

①、计划任务执行类

@Service
public class ScheduleTaskService{
	
	private static final SimpleDataFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	
	@Scheduled(fixedRate = 5000) //每隔固定的时间执行
	public void reportCurrentTime(){
		System.out.println("每隔五秒执行一次"+dateFormat(new Date()))
	}

	@Scheduled(cron = "0 28 11 ? * *")//按照指定的每天11点28分执行
	public void fixTimeExecution(){
		System.out.println("在指定时间"+dateFormat.format(new Date())+"执行");
	}
}

②、配置类

@Configuration
@ComponentScan("com.xxx.taskscheduler")
@EnableScheduling //开启对计划任务的支持
public class TaskSchedulerConfig{

}

③、运行

public class Main{
	
	public static void main(String[] args){
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
	}
}

条件注解@Conditional

根据满足某一个特定条件创建一个特定的Bean

①、判定条件定义

/**
	判定Window的条件
*/
public class WindowsCondition implements Condition{
	
	public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata){
		return context.getEnvironment().getProperty("os.name").contains("windows");
	}
	
}
/**
	判定Linux的条件
*/
public class LinuxCondition implements Condition{
	
	public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata){
		return context.getEnvironment().getProperty("os.name").contains("Linux");
	}
	
}

②、不同系统下Bean的类

public interface ListService{
	public String showListCmd();
}

Windows下需要创建的Bean

public class WindowListService implements ListService{
	
	@Override
	public String showListCmd(){
		return "dir";
	}
}

Linux下需要创建的Bean

public class LinuxListService implements ListService{
	
	@Override
	public String showListCmd(){
		return "ls";
	}
}

③、配置类

@Configuration
public class ConditionConfig{
	
	@Bean
	@Conditional(WindowsCondition.class)
	public ListService windowsListService(){
		return new WindowsListService();
	}

	@Bean
	@Conditional(LinuxCondition.class)
	public ListService linuxListService(){
		return new LinuxListService();
	}
}

④、运行

public class Main{
	
	public static void main(String[] args){
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class);
		ListService listService = context.getBean(ListService.class);
		
		System.out.println(context.getEnvironment().getProperty("os.name")+"系统下的命令为:"+listService.showListCmd())
	}
}

SpringAware

Spring提供的Aware接口:为了让Bean获得Spring容器服务
在这里插入图片描述

①、在com.xxx.aware包下新建一个test.txt,内容任意

②、实现对应的接口,获取Bean名称和资源加载服务

@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{
	
	private String beanName;
	private ResourceLoader loader;
	
	@Override
	public void setResourceLoader(ResourceLoader resourceLoader){
		this.loader = resourceLoader;
	}

	@Override
	public void setBeanName(String name){
		this.beanName = name;
	}
	
	public void outputResult(){
		System.out.println("Bean的名称为:" + beanName);
		Resource resource = loader.getResource("classpath:com/xxx/aware/test.txt");
		try{
			System.out.println("ResourceLoader加载的文件内容为"+IOUtils.buString(resource.getInputStream()));
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

③、配置类

@Configuration
@ComponentScan("com.xxx.aware")
public class AwareConfig{

}

④、运行

public class Main{
	public static void main(String[] args){
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
		
		AwareServcie awareService = context.getBean(AwareService.class);
		awareService.outputResult();//加载的文件内容
		context.close();
	}
}

http://www.kler.cn/news/314762.html

相关文章:

  • 开源网安多城联动、多形式开展网安周公益活动,传播网络安全知识
  • 中断-MCU
  • HTML粉色烟花秀
  • python新手的五个练习题
  • MySQl索引事务(B树)
  • 基于 K8S kubernetes 的常见日志收集方案
  • 大模型如何学习数据
  • NLP 文本分类核心问题
  • LangChain教程 - 构建一个检索增强生成 (RAG) 应用程序
  • 面试金典题8
  • go webapi上传文件
  • 【Linux】Docker:离线主机部署
  • 【Temporal】日志打印控制
  • 【AI视频】AI虚拟主播制作网站推荐
  • 深度学习02-pytorch-06-张量的形状操作
  • 基于深度学习的智能电网优化
  • Java异常架构与异常关键字
  • Spring后端直接用枚举类接收参数,自定义通用枚举类反序列化器
  • IT行业:未来发展的无限可能
  • 【医学半监督】置信度指导遮蔽学习的半监督医学图像分割
  • 51单片机-系列-数码管中断和定时器
  • Lsposed Java HOOK原理及检测
  • 我的AI工具箱Tauri版-VideoIntroductionClipCut视频介绍混剪
  • Nacos与Eureka的区别:深入解析微服务中的服务注册与发现
  • npm切换为淘宝镜像源
  • GPU加速生物信息分析的尝试
  • 数据结构之存储位置
  • AIGC专栏15——CogVideoX-Fun详解 支持图文生视频 拓展CogVideoX到256~1024任意分辨率生成
  • Web_php_include 攻防世界
  • C++20 std::format