Hello Wicket and Spring-OSGi
30
Nov
Nov
0
源起
想要嘗試 wicket 開發網頁的方式,並套到 Spring-OSGi 的環境中。
hello wicket
在 web.xml 中可以看出整合的關係,上半部是 Spring 的部份喚起 ContextLoaderListener 並 採用 OsgiWebApplicationContext 當 ContextClass。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.osgi.context.support.OsgiWebApplicationContext </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
下半部就是宣告 wicket 的使用,並用 SpringWebApplicationFactory 來注入 bean。
<servlet>
<servlet-name>HelloWorldApplication</servlet-name>
<servlet-class>
wicket.protocol.http.WicketServlet
</servlet-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>
wicket.spring.SpringWebApplicationFactory
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldApplication</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
applicationContext.xml 中 BundleViewer 是一個 BundleContextAware 介面的實做,利用 BundleContext 可以得到 Bundles 的資料。 HelloWorldApplication 則是 wicket.protocol.http.WebApplication 的實做。 實際上 BundleViewer 這個 bean 會被注到某些 Page 去,但是在這個 xml 檔案中 無法看到這些關聯,注入行為由 HelloWorldApplication 負責。
<bean id="bundleViewer" class="haha.osgi.webconsole.wicket.BundleViewer"/> <bean id="wicketApplication" class="haha.osgi.webconsole.wicket.HelloWorldApplication" />
HelloWorldApplication 需要加個 init 來注入需要的 bean。
public class HelloWorldApplication extends WebApplication {
@Override
protected void init() {
// THIS LINE IS IMPORTANT - IT INSTALLS THE COMPONENT INJECTOR THAT WILL
// INJECT NEWLY CREATED COMPONENTS WITH THEIR SPRING DEPENDENCIES
addComponentInstantiationListener(new SpringComponentInjector(this));
}
@Override
public Class getHomePage() {
return HelloWorldPage.class;
}
HelloWorldPage 只要加一行,不能初始化,也不需要 setter。
@SpringBean private BundleViewer bundleViewer;
觀察
- wicket 跟 Spring 的整合有許多方式,這是利用其中 wicket-spring-annot 提供的支援 來做的。
- wicket 還需要多練習。