はてなキーワード: Guiceとは
[参考文献]
S2 は、.dicon ファイルで定義をだいぶ簡略化できる。パフォーマンスはどうなんだろう。誰かテストしてくれいw
app.dicon
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" "http://www.seasar.org/dtd/components24.dtd">
<components namespace="client">
<include path="hello.dicon" />
<component class="org.seasar.guice.Client" />
</components>
hello.dicon
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components initializeOnCreate="false">
<component class="org.seasar.guice.HelloServiceImpl" />
</components>
HelloService.java、HelloServiceImpl.java は、上記 ITPro と内容が同じなので省略。
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.SingletonS2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
public class Module extends AbstractModule {
S2Container container = null;
public Module(S2Container container){
this.container = container;
}
@Override
protected void configure() {
bind(S2ContainerFactory.class).annotatedWith(Names.named(container.getPath()));
bind(Client.class).toInstance(SingletonS2Container.getComponent(Client.class));
}
}
private HelloService helloService = null;
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
public void execute() {
helloService.sayHello();
}
}
Main.java
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Main {
private static final String PATH = ".\\app.dicon";
public static void main(String[] args) {
SingletonS2ContainerFactory.setConfigPath(PATH);
SingletonS2ContainerFactory.init();
S2Container container = SingletonS2ContainerFactory.getContainer();
Module module = new Module(container);
Injector injector = Guice.createInjector(module);
Client client = injector.getInstance(Client.class);
client.execute();
}
}
実行結果
java -cp ?? org.seasar.guice.Main
2008/05/13 21:19:22 org.seasar.framework.log.Logger info
情報: Running on [ENV]product, [DEPLOY MODE]Normal Mode
Hello, world!