1、在Eclipse中建立项目,并导入hivemind-1.1.jar,javassist-3.0.jar,jakarta-oro-2.0.6.jar三个类包。
Jakarta-ORO正则表达式库,它是最全面的正则表达式API之一。
Javassist是一个开源的分析、编辑和创建Java字节码的类库。是由东京技术学院的数学和计算机科学系的 Shigeru Chiba 所创建的。它已加入了开放源代码JBoss 应用服务器项目,通过使用Javassist对字节码操作为JBoss实现动态AOP框架。
2、建立一个接口和它的实现类,因为在HiveMind中是强制针对接口编程的。
接口Action.java
package
org.apache.hivemind.example;
/** */ /**
* @author starsiday
*/
public interface Action {
public String execute(String msg);
}
/** */ /**
* @author starsiday
*/
public interface Action {
public String execute(String msg);
}
实现类InitAction.java
package
org.apache.hivemind.example;
/** */ /**
* @author starsiday
*/
public class InitAction implements Action {
public String execute(String msg) {
return msg.toLowerCase();
}
}
/** */ /**
* @author starsiday
*/
public class InitAction implements Action {
public String execute(String msg) {
return msg.toLowerCase();
}
}
3、在项目中新建一个目录META-INF,并新建hivemodule.xml。
<?
xml version="1.0"
?>
< module id ="examples" version ="1.0.0"
package ="org.apache.hivemind.example" >
< service-point id ="theAction" interface ="Action" >
< create-instance class ="InitAction" />
< interceptor service-id ="hivemind.LoggingInterceptor" />
</ service-point >
</ module >
< module id ="examples" version ="1.0.0"
package ="org.apache.hivemind.example" >
< service-point id ="theAction" interface ="Action" >
< create-instance class ="InitAction" />
< interceptor service-id ="hivemind.LoggingInterceptor" />
</ service-point >
</ module >
在这里,目录META-INF中的hivemodule.xml是作为默认的配置文件,package属性指定了类所在的包名。create-instance指的是接口的实现类。
当需要更换接口的实现类时,只需添加实现类和更改配置文件。
4、测试类ActionTest.java
package
org.apache.hivemind.example;
import java.util.Locale;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;
import junit.framework.TestCase;
/** */ /**
* @author starsiday
*/
public class ActionTest extends TestCase {
Registry registry;
public void setUp() {
RegistryBuilder builder = new RegistryBuilder();
builder.addDefaultModuleDescriptorProvider();
registry = builder.constructRegistry(Locale.getDefault());
}
public void testExecute() {
Action action = (Action) registry.getService(Action. class );
System.out.println(action.execute( " Hello World " ));
}
}
在SetUp()方法中的builder.addDefaultModuleDescriptorProvider()是为了加载资源,包括HiveMind自身的资源和用户定义的资源,因为这是使用默认的配置,所以只使用builder.addDefaultModuleDescriptorProvider()就可以运行。
import java.util.Locale;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;
import junit.framework.TestCase;
/** */ /**
* @author starsiday
*/
public class ActionTest extends TestCase {
Registry registry;
public void setUp() {
RegistryBuilder builder = new RegistryBuilder();
builder.addDefaultModuleDescriptorProvider();
registry = builder.constructRegistry(Locale.getDefault());
}
public void testExecute() {
Action action = (Action) registry.getService(Action. class );
System.out.println(action.execute( " Hello World " ));
}
}
5、运行
package
org.apache.hivemind.example;
import java.util.Locale;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;
/** */ /**
* @author starsiday
*/
public class Main {
public static void main(String[] args) {
RegistryBuilder builder = new RegistryBuilder();
builder.addDefaultModuleDescriptorProvider();
Registry registry = builder.constructRegistry(Locale.getDefault());
Action action = (Action) registry.getService(Action.class);
System.out.println(action.execute("Hello World"));
}
}
import java.util.Locale;
import org.apache.hivemind.Registry;
import org.apache.hivemind.impl.RegistryBuilder;
/** */ /**
* @author starsiday
*/
public class Main {
public static void main(String[] args) {
RegistryBuilder builder = new RegistryBuilder();
builder.addDefaultModuleDescriptorProvider();
Registry registry = builder.constructRegistry(Locale.getDefault());
Action action = (Action) registry.getService(Action.class);
System.out.println(action.execute("Hello World"));
}
}