InitializingBean应用场景一
作者:互联网
假设设计一个事件模块,为了降低与其它模块之间的耦合性我们可以这样设计
1、事件模块的接口package com.hdx.common.hdxspringinit.event;
/**
* 事件模块接口
*/
public interface EventInterface {
/**
* 事件类型
* @return
*/
public String eventType();
/**
* 执行事件
* @return
*/
public void executeEvent();
}
2、事件工厂接口
package com.hdx.common.hdxspringinit.event;
/**
* 事件工厂注册类
*/
public interface Eventfactory {
/**
* 注册事件
*/
public void registerEvent(EventInterface eventInterface);
/**
* 根据事件类型获取事件执行器
*/
public EventInterface getEvent(String eventType);
}
3、事件管理器接口
package com.hdx.common.hdxspringinit.event;
/**
* 事件调度管理器
*/
public interface EventManager {
/**
* 执行事件
* @param eventType
*/
public void excuteEvent(String eventType);
}
4、事件具体实现
package com.hdx.common.hdxspringinit.evnettest;
import com.hdx.common.hdxspringinit.event.EventInterface;
import org.springframework.stereotype.Component;
@Component
public class DeleteEvent implements EventInterface {
@Override
public String eventType() {
return "delete";
}
@Override
public void executeEvent() {
System.out.printf("执行删除事件业务:com.hdx.common.hdxspringinit.evnettest.DeleteEvent.executeEvent");
}
}
package com.hdx.common.hdxspringinit.evnettest;
import com.hdx.common.hdxspringinit.event.EventInterface;
import org.springframework.stereotype.Component;
@Component
public class UpdateEvent implements EventInterface {
@Override
public String eventType() {
return "update";
}
@Override
public void executeEvent() {
System.out.printf("执行删除事件业务:com.hdx.common.hdxspringinit.evnettest.UpdateEvent.executeEvent");
}
}
5、事件工厂类默认实现
package com.hdx.common.hdxspringinit.event.impl;
import com.hdx.common.hdxspringinit.event.EventInterface;
import com.hdx.common.hdxspringinit.event.Eventfactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class EventfactoryImpl implements Eventfactory, InitializingBean {
private final Map<String, EventInterface> eventInterfaceMap = new HashMap<>();
@Autowired
private ApplicationContext applicationContext;
/**
* 注册事件
*/
@Override
public void registerEvent(EventInterface eventInterface) {
String eventType = eventInterface.eventType();
EventInterface eventInterface1 = eventInterfaceMap.get(eventType);
if (eventInterface1 == null) {
eventInterfaceMap.put(eventType, eventInterface);
}
}
@Override
public EventInterface getEvent(String eventType) {
return eventInterfaceMap.get(eventType);
}
@Override
public void afterPropertiesSet() throws Exception {
Map<String, EventInterface> events = applicationContext.getBeansOfType(EventInterface.class);
for (EventInterface eventInterface : events.values()) {
registerEvent(eventInterface);
}
}
}
6、事件管理器默认实现
package com.hdx.common.hdxspringinit.event.impl;
import com.hdx.common.hdxspringinit.event.EventInterface;
import com.hdx.common.hdxspringinit.event.EventManager;
import com.hdx.common.hdxspringinit.event.Eventfactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 默认事件管理器实现
*/
public class DefaultEventManagerImpl implements EventManager {
@Autowired
private Eventfactory eventfactory;
@Override
public void excuteEvent(String eventType) {
EventInterface eventInterface = eventfactory.getEvent(eventType);
if(eventInterface != null) {
eventInterface.executeEvent();
}
}
}
扩展知识点
ConditionalOnProperty 属性
name 在属性文件(.properties)中配置的key
havingValue 在属性文件(.properties)中配置的value
matchIfMissing 默认是否有效(如果属性文件中没有配置)
import org.springframework.context.annotation.Configuration;
@ConditionalOnProperty(name = "spring.datasource.type",
havingValue = "org.apache.tomcat.jdbc.pool.DataSource",
matchIfMissing = true)
标签:InitializingBean,场景,hdx,common,应用,hdxspringinit,import,com,public 来源: https://www.cnblogs.com/hu0529/p/16298604.html