Java 在没有SpringMVC的portlet中使用Spring
有没有一种不用DispatcherPortlet而使用spring开发Portlet的方法?我想为UI使用其他技术,主要是Vaadin。弹簧用于DI和其他东西。Portlet端是否有类似于ContextLoaderListener类的内容?查看:您可以创建一个Java 在没有SpringMVC的portlet中使用Spring,java,spring,portlet,vaadin,Java,Spring,Portlet,Vaadin,有没有一种不用DispatcherPortlet而使用spring开发Portlet的方法?我想为UI使用其他技术,主要是Vaadin。弹簧用于DI和其他东西。Portlet端是否有类似于ContextLoaderListener类的内容?查看:您可以创建一个ApplicationContext,如下所示: ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml
ApplicationContext
,如下所示:
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
在类路径上引用xml文件
然后可以通过调用
context.getBean(“beanName”)
来获取bean。不需要Spring MVC。我希望得到比Noel给出的更详细的答案。也许有一些最佳实践可以做到这一点?以下是我当前的解决方案:
将xml文件位置作为init参数提供给我的portlet。
我的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);
应用程序上下文属性只是我编的字符串常量。我仍然希望找到更好的解决方案。您可以使用PortletApplicationContextUtils检索web应用程序上下文: ApplicationContext ctx=PortletApplicationContextUtils.getWebApplicationContext(getPortletContext())
然后,您只需要在web.xml上添加一些配置 我认为除了这个基于MVC的portlet之外,spring中没有其他portlet实现。当然,我们可以作为noel Writer直接生成bean,但如何将其视为portlet?如何使用PortletApplicationContext初始化应用程序上下文?