DispatcherPortlet を使用せずに、Spring でポートレットを開発する方法はありますか? UI に他のテクノロジー、主に Vaadin を使用したいと考えています。Spring は DI などに使用されます。ポートレット側に ContextLoaderListener クラスに似たものはありますか?
3 に答える
Springのドキュメントを見る:次のように作成できますApplicationContext
:
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
クラスパスで xml ファイルを参照する。
その後、 を呼び出して Bean を取得できますcontext.getBean("beanName")
。Spring MVC は必要ありません。
ノエルが与えたものよりも詳細な答えを期待していました. 多分これにはいくつかのベストプラクティスがありますか?これが私の現在の解決策です:
xml ファイルの場所を init パラメータとしてポートレットに指定します。私のinitメソッドは次のようになります
@Override
public void init(PortletConfig config) throws PortletException {
super.init(config);
String configLocations = config.getInitParameter("contextConfigLocation");
ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext();
springContext.setConfigLocation(configLocations);
springContext.refresh();
config.getPortletContext().setAttribute(APPLICATION_CONTEXT_ATTRIBUTE, springContext);
}
これで、必要なときにいつでも PortletContext を介して applicationContext にアクセスできるようになりました。
(ApplicationContext) portalContext.getAttribute(APPLICATION_CONTEXT_ATTRIBUTE);
APPLICATION_CONTEXT_ATTRIBUTE は、私が作成した単なる文字列定数です。私はまだより良い解決策を求めています。
PortletApplicationContextUtils を使用して、Web アプリケーション コンテキストを取得できます。
ApplicationContext ctx = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext());
次に、web.xml に構成を追加するだけです。