2008-05-13

GuiceSeasar2 を連携させてみたよ

[参考文献]

    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 と内容が同じなので省略。

    Module.java

    package org.seasar.guice;

    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));

    }

    }

    Client.java

    package org.seasar.guice;

    public class Client {

    private HelloService helloService = null;

    public void setHelloService(HelloService helloService) {

    this.helloService = helloService;

    }

    public void execute() {

    helloService.sayHello();

    }

    }

    Main.java

    package org.seasar.guice;

    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!

    実行環境JDK 7 (build 1.7.0-ea-b24)

    記事への反応(ブックマークコメント)

    ログイン ユーザー登録
    ようこそ ゲスト さん