XML方式适用于没有大量使用spring来管理bean的模块,我们可以通过手动在模块中建立对应的xml文件来初始化对应模块的上下文获取该bean,实现方式如下:
public interface IBaseCom {
/**
* 打开回调方法
*/
public int open(String param);
/**
* 卸载回调方法
*/
public int close( );
/**
* 接收消息
*/
public int receiveMessage(String message);
}
通过定义接口来规范我们每个模块中需要通信的类,方便我们在jarslink中扫描对应的Action,我们来看对应模块中的实现方法
public class TestCom implements IBaseCom{
public int open(String param){
return 1;
}
public int close(){
return 2;
}
public int receiveMessage(String message){
if(message.equals("1")){
//a逻辑
}else if(.....){
}
}
}
我们现在模块中定义了一个通信类,通信类中一般需要三个方法,启动初始化方法、卸载方法(用于卸载资源)、消息传递方法。其中消息传递方法可以由每个模块实现单独的逻辑。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--模块加载引擎-->
<bean id="testCom" name="testCom" class="com.bigdog.TestCom" />
</beans>
上面是jarslink扫包时必要的xml,需要通过该xml实例化出该模块的上下文,从而从上下文中获取到对应的通信类,该文件的放置位置如下
需要将其放置在resource下的对应文件路径内,因为jarslink的XML加载方式默认加载的xml路径在下面的代码中已经规定好了
public class ModuleLoaderImpl implements ModuleLoader, ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(ModuleLoaderImpl.class);
/**
* Spring bean文件所在目录,不同的路径确保能取到资源
*/
private static String[] SPRING_XML_PATTERN = {"classpath*:META-INF/spring/*.xml", "classpath*:*META-INF/spring/*.xml"};