版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hcmony/article/details/83348214
1,创建一个springboot项目。结构如下:
2,引入jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
3,启动类代码:
package com.hcmony;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MqApplication {
public static void main(String[] args) {
SpringApplication.run(MqApplication.class, args);
}
}
4,EventObject 实体类代码:
package com.hcmony.eventbus;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.io.Serializable;
/**
* <h3>实体类</h3>
* <p></p>
*
* @author hcmony
* @since V1.0.0, 2018/10/24 14:47
*/
public class EventObject implements Serializable{
private static final long serialVersionUID = 7031664195960133302L;
public EventObject(long id, String name) {
this.id = id;
this.name = name;
}
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
5,EventPark抽象类
package com.hcmony.eventbus;
import com.google.common.eventbus.EventBus;
/**
* <h3>Shenjue.java基本描述</h3>
* <p></p>
*
* @author hcmony
* @since V1.0.0, 2018/10/24 15:34
*/
public interface EventPark {
void post(EventObject eventObject);
void register(Listener listener);
void unregister(Listener listener);
EventBus getEventBus();
Listener getListener();
}
6,具体实现类AppcontxtListener:
package com.hcmony.eventbus;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* <h3>Shenjue.java基本描述</h3>
* <p></p>
*
* @author hcmony
* @since V1.0.0, 2018/10/24 15:05
*/
@Component
public class AppcontxtListener implements EventPark {
private final EventBus eventBus = new EventBus();
private final Listener listener = new OneListener();
@Override
public EventBus getEventBus() {
return eventBus;
}
@Override
public Listener getListener() {
return listener;
}
public AppcontxtListener() {
register(getListener());
}
@Override
public void post(EventObject eventObject) {
getEventBus().post(eventObject);
}
@Override
public void register(Listener listener) {
getEventBus().register(listener);
}
@Override
public void unregister(Listener listener) {
getEventBus().unregister(listener);
}
}
7,订阅类Listener接口
package com.hcmony.eventbus;
/**
* <h3>Shenjue.java基本描述</h3>
* <p></p>
*
* @author hcmony
* @since V1.0.0, 2018/10/24 14:47
*/
public interface Listener<T extends Object>{
void consume(T event);
}
8,OneListener 订阅类实现Listener接口
package com.hcmony.eventbus;
import com.google.common.eventbus.Subscribe;
import org.springframework.stereotype.Component;
/**
* <h3>Shenjue.java基本描述</h3>
* <p></p>
*
* @author hcmony
* @since V1.0.0, 2018/10/24 14:51
*/
@Component
public class OneListener implements Listener<EventObject> {
@Subscribe
@Override
public void consume(EventObject event) {
System.out.println(event.toString());
}
}
9,TestController 测试类:
package com.hcmony.eventbus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <h3>Shenjue.java基本描述</h3>
* <p></p>
*
* @author hcmony
* @since V1.0.0, 2018/10/24 15:10
*/
@RestController
public class TestController {
@Autowired
private EventPark eventPark;
@RequestMapping("/test")
public void test(){
System.out.println("test");
EventObject object = new EventObject(1,"test");
eventPark.post(object);
}
}
10,结果如下: