1. 程式人生 > >EventBus實現解耦,使用詳解

EventBus實現解耦,使用詳解

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,結果如下: