Spring常用配置——SpringEvent

Spring的事件(Application Event)为Bean之间的消息通信提供了支持。当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这是我们就需要让另外一个Bean监听当前Bean所发送的事件

Spring的事件需要遵循如下流程:

(1)自定义事件,继承ApplicationEvent

(2)定义事件监听器,实现ApplicationListener

(3)使用容器发布事件。

示例代码:

DemoEvent.java

package com.minivison.caixing.learn.event;

import org.springframework.context.ApplicationEvent;

/**
 *  
* * @author caixing
* @version 1.0
* @taskId
* @CreateDate 2018年04月17日
*/ public class DemoEvent extends ApplicationEvent { private static final long serialVersionUID = -2419537024459602985L; 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; } }

DemoListener.java

package com.minivison.caixing.learn.event;

import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 *  
* * @author caixing
* @version 1.0
* @taskId
* @CreateDate 2018年04月17日
*/ @Component public class DemoListener implements ApplicationListener { @Override public void onApplicationEvent(DemoEvent demoEvent) { String msg = demoEvent.getMsg(); try { System.out.println("我接收到消息:" + msg); /* for(;;){ } */ } catch (Exception e) { e.printStackTrace(); } } }

DemoService.java

package com.minivison.caixing.learn.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 *  
* * @author caixing
* @version 1.0
* @taskId
* @CreateDate 2018年04月17日
*/ @Service public class DemoService { @Autowired private ApplicationContext context; public void msg(String msg){ context.publishEvent(new DemoEvent(this,msg)); System.out.println(msg); } }

EventConfig.java

package com.minivison.caixing.learn.event;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 *  
* * @author caixing
* @version 1.0
* @taskId
* @CreateDate 2018年04月17日
*/ @Configuration @ComponentScan("com.minivison.caixing.learn.event") public class EventConfig { }

Main.java

package com.minivison.caixing.learn;

import com.minivison.caixing.learn.beaninit.BeanConfig;
import com.minivison.caixing.learn.beaninit.BeanWayService;
import com.minivison.caixing.learn.beaninit.JSR250WayService;
import com.minivison.caixing.learn.common.ElConfig;
import com.minivison.caixing.learn.event.DemoService;
import com.minivison.caixing.learn.event.EventConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.Base64Utils;

import javax.imageio.stream.FileImageInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *  
* * @author caixing
* @version 1.0
* @taskId
* @CreateDate 2018年04月12日
*/ public class Main { public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class); DemoService service = context.getBean(DemoService.class); service.msg("hello event"); context.close(); //String str = Base64Utils.encodeToString(image2byte("E:\\123.jpg")); //System.out.println(str); } public static byte[] image2byte(String path) { byte[] data = null; FileImageInputStream input = null; try { input = new FileImageInputStream(new File(path)); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int numBytesRead = 0; while ((numBytesRead = input.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } data = output.toByteArray(); output.close(); input.close(); } catch (FileNotFoundException ex1) { ex1.printStackTrace(); } catch (IOException ex1) { ex1.printStackTrace(); } return data; } }

你可能感兴趣的:(java,spring)