最近有空试用了一下Guice,感觉还是比较小巧的,在上面做了个类似Tapestry的Configuration,利用它可以实现类似扩展点的功能。 (可以参考http://tapestry.apache.org/tapestry5/tapestry- ioc/configuration.html)
另外关于网上代码和XML的配置的问题,个人认为不是问题。基于代码的配置就象一个核,如果你喜欢基于XML的配置,只要自己写一个模块,读取XML并使用其中的信息再用基于代码的配置去配置即可,只是基于使用@Inject Annotation 的地方稍微难处理点,但也只需要动态生成类也可以解决这个问题。
使用的时候参考以下这个测试用例:
public class GuiceTest extends TestCase {
public static class ContributorUsingContributorClass extends
ContributableModule {
@Override
protected void configure() {
contribute(new AbstractContributor() {
@Inject
@ConfigFor(AnotherInterface.class)
OrderedConfiguration<string></string> orderedConfiguration;
@Inject
@ConfigFor(AnotherInterface.class)
Configuration<string></string> unorderedConfiguration;
@Inject
@ConfigFor(Service.class)
MappedConfiguration configuration;
@Inject
public void contribute() {
configuration.add("abcd", new Runnable() {
public void run() {
}
});
unorderedConfiguration.add("abc");
orderedConfiguration.add("my", "abc");
}
});
}
}
public void testClient() {
Injector injector = Guice.createInjector(new MyModule());
Client client = injector.getInstance(Client.class);
MockService mock = (MockService) injector.getInstance(Service.class);
client.go();
assertTrue(mock.isGone());
}
public void testConfigurationThroughContributorClass() {
Injector injector = Guice.createInjector(new MyModule(),
new ContributorUsingContributorClass());
MockService mock = (MockService) injector.getInstance(Service.class);
Map map = mock.getConfigMap();
assertNotNull(map);
assertEquals(1, map.size());
}
public void testUnorderedConfiguration() {
Injector injector = Guice.createInjector(new MyModule(),
new ContributorUsingContributorClass());
MockService mock = (MockService) injector.getInstance(Service.class);
Collection<string></string> config = mock.config;
assertNotNull(config);
assertEquals(1, config.size());
assertEquals("[abc]", config.toString());
}
public void testOrderedConfiguration() {
Injector injector = Guice.createInjector(new MyModule(),
new ContributorUsingContributorClass());
MockService mock = (MockService) injector.getInstance(Service.class);
Collection<string></string> config = mock.orderedConfig;
assertNotNull(config);
assertEquals(1, config.size());
assertEquals("[abc]", config.toString());
}
另外关于网上代码和XML的配置的问题,个人认为不是问题。基于代码的配置就象一个核,如果你喜欢基于XML的配置,只要自己写一个模块,读取XML并使用其中的信息再用基于代码的配置去配置即可,只是基于使用@Inject Annotation 的地方稍微难处理点,但也只需要动态生成类也可以解决这个问题。
使用的时候参考以下这个测试用例:
public class GuiceTest extends TestCase {
public static class ContributorUsingContributorClass extends
ContributableModule {
@Override
protected void configure() {
contribute(new AbstractContributor() {
@Inject
@ConfigFor(AnotherInterface.class)
OrderedConfiguration<string></string> orderedConfiguration;
@Inject
@ConfigFor(AnotherInterface.class)
Configuration<string></string> unorderedConfiguration;
@Inject
@ConfigFor(Service.class)
MappedConfiguration configuration;
@Inject
public void contribute() {
configuration.add("abcd", new Runnable() {
public void run() {
}
});
unorderedConfiguration.add("abc");
orderedConfiguration.add("my", "abc");
}
});
}
}
public void testClient() {
Injector injector = Guice.createInjector(new MyModule());
Client client = injector.getInstance(Client.class);
MockService mock = (MockService) injector.getInstance(Service.class);
client.go();
assertTrue(mock.isGone());
}
public void testConfigurationThroughContributorClass() {
Injector injector = Guice.createInjector(new MyModule(),
new ContributorUsingContributorClass());
MockService mock = (MockService) injector.getInstance(Service.class);
Map map = mock.getConfigMap();
assertNotNull(map);
assertEquals(1, map.size());
}
public void testUnorderedConfiguration() {
Injector injector = Guice.createInjector(new MyModule(),
new ContributorUsingContributorClass());
MockService mock = (MockService) injector.getInstance(Service.class);
Collection<string></string> config = mock.config;
assertNotNull(config);
assertEquals(1, config.size());
assertEquals("[abc]", config.toString());
}
public void testOrderedConfiguration() {
Injector injector = Guice.createInjector(new MyModule(),
new ContributorUsingContributorClass());
MockService mock = (MockService) injector.getInstance(Service.class);
Collection<string></string> config = mock.orderedConfig;
assertNotNull(config);
assertEquals(1, config.size());
assertEquals("[abc]", config.toString());
}