Wicket Spring統合の例

Wicket + Spring統合の例

このチュートリアルでは、integrate Wicket with Spring frameworkを実行する方法を示します。

この記事のライブラリ:

  1. Wicket v1.4.17

  2. wicket-spring v1.4.17

  3. Spring v3.0.5.RELEASE

1. プロジェクト構造

このチュートリアルの最終的なプロジェクトディレクトリ構造は、特別なものではなく、単なる標準のMavenプロジェクトです。

wicket spring example

2. プロジェクトの依存関係

WicketとSpringの依存関係を取得します。両方を統合するには、「wicket-spring.jar」が必要です。



    

        
        
            org.apache.wicket
            wicket
            1.4.17
        

        
        
            org.apache.wicket
            wicket-spring
            1.4.17
        

        
        
            org.springframework
            spring-core
            3.0.5.RELEASE
        

        
            org.springframework
            spring-context
            3.0.5.RELEASE
        

        
            org.springframework
            spring-web
            3.0.5.RELEASE
        

        
        
            org.slf4j
            slf4j-log4j12
            1.5.6
        

    

3. 春豆

Spring Beanを作成し、@Serviceで注釈を付けます。

package com.example.user;

public interface HelloService {

    String getHelloWorldMsg();

}
package com.example.user;

import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {

    public String getHelloWorldMsg() {
        return "Spring : hello world";
    }

}

4. Springコンテナに注入する

標準のSpringapplicationContext.xmlファイルを作成し、自動コンポーネントスキャン機能を有効にします。

ファイル:applicationContext.xml



    

5. WicketとSpringを統合する

Wicketアプリケーションのinit()メソッドをこの「addComponentInstantiationListener(new SpringComponentInjector(this));」でオーバーライドします。

ファイル:Wicketアプリケーションクラス

package com.example;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import com.example.user.SimplePage;

public class WicketApplication extends WebApplication {

    @Override
    public Class getHomePage() {

        return SimplePage.class; // return default page
    }

    @Override
    protected void init() {

        super.init();
        addComponentInstantiationListener(new SpringComponentInjector(this));

    }

}

これで、@SpringBeanを介してSpringBeanをWicketコンポーネントに注入できます。

package com.example.user;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class SimplePage extends WebPage {

    @SpringBean
    private HelloService helloService;

    public SimplePage(final PageParameters parameters) {

        add(new Label("msg", helloService.getHelloWorldMsg()));

    }

}


    

Wicket + Spring integration example

6. web.xml

最後のステップは、WebプロジェクトにWicketとSpringを知らせることです。 両方をweb.xmlで宣言します。

ファイル:web.xml


    Wicket Web Application

    
        wicket.wicketTest
        org.apache.wicket.protocol.http.WicketFilter
        
            applicationClassName
            com.example.WicketApplication
        
    

    
        wicket.wicketTest
        /*
    

    
        
                     org.springframework.web.context.ContextLoaderListener
                
    

7. Demo

開始してアクセス–http://localhost:8080/WicketExamples/

シンプルなWicketページ。メッセージはSpringから返されます。

wicket spring demo

ダウンロード–Wicket-Spring-Integration-Example.zip(8KB)