here文档说:
The framework will,on initialization of a DispatcherPortlet,look for
a file named [portlet-name]-portlet.xml in the WEB-INF directory of
your web application and create the beans defined there (overriding
the definitions of any beans defined with the same name in the global
scope).
如果可以的话,我会使用注释进行配置,因为保持配置和实际代码同步很容易.因此,我项目中的[portlet-name] -portlet.xml看起来像这样(这个文件已经存在了……每个portlet一个):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean class="some.path.to.a.Class" />
</beans>
大量XML仅用于一小部分信息:应该使用some.path.to.a.Class来处理对[portlet-name]的请求.在some.path.to.a.Class上放置@ForPortlet(“ [portlet-name]”)注释或类似的注释会容易得多,而完全不用理会这个XML文件了.这样有可能吗? This bug report可能提示“否” /“尚未”?
解
感谢OlliS的超级有用提示(非常感谢!),我找到了一种方法.我潜心研究OlliS给了我Spring的资源,并花了很长时间来弄清楚事情是如何协同工作的,我写了以下课程:
public class MyPortletContext extends
AbstractRefreshablePortletApplicationContext
{
private static final String PORTLET_PACKAGE = "package.where.my.portlets.are.";
private static final String REMOVE_FROM_NAMESPACE_FOR_PORTLETNAME = "-portlet";
@Override
protected void loadBeanDefinitions(DefaultListablebeanfactory beanfactory)
throws BeansException,IOException
{
// The following line does the same thing as specifying
// <context:annotation-config /> in the xml file:
AnnotationConfigUtils.registerAnnotationConfigProcessors(beanfactory);
// Figure out the portlet name:
final String portletName = StringUtils.removeEnd(getNamespace(),REMOVE_FROM_NAMESPACE_FOR_PORTLETNAME);
// Derive the controller from the portlet name:
final String beanClassName = PORTLET_PACKAGE + portletName;
// Tell spring about the bean:
final GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClassName(beanClassName);
final String beanName = BeanDefinitionReaderUtils.generateBeanName(
beanDefinition,beanfactory);
final BeanDefinitionHolder bdHolder = new BeanDefinitionHolder(
beanDefinition,beanName,new String[] { beanClassName });
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder,beanfactory);
}
}
然后,我使用init-param在portlet.xml文件中将该类注册为contextClass,就像OlliS在他的答案中指出的那样.就是这样.不再需要* -portlet.xml文件.只需一个类即可配置我的所有portlet.
当然,仍然可以改进此类,使其更加灵活,从某个地方而不是从常量中读取portlet软件包.也许是一个初始化参数.或者可以扫描注释,也许是提到的@ForPortlet注释,这将可能注册多个bean.但是现在我很高兴:-).
请参阅此处的参考文档:
http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-java
例如,可以嵌入context:component-scan:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("com.acme");
基于Java的配置也可以与XML结合;在文档中提到.
希望能帮助到你.
编辑:
因此,您需要用Java类替换特定于Portlet的上下文.我不确定DispatcherPortlet是否支持Java配置.
您可以尝试添加在普通Web应用程序中使用的类似内容:
<portlet>
<portlet-name>portlet</portlet-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<!-- Configure DispatcherPortlet to use AnnotationConfigWebApplicationContext
instead of the default XmlPortletApplicationContext ? -->
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.PortletConfig</param-value>
</init-param>
<!-- ... -->
</portlet>
有关AnnotationConfigWebApplicationContext的一些文档:
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.html
从Portlet参考文档:
DispatcherPortlet initialization parameters
contextClass
Class that implements WebApplicationContext,which will be used to instantiate the
context used by this portlet. If this parameter isn’t specified,the
XmlPortletApplicationContext will be used.
WebApplicationContext的实现可以在文档中找到:
似乎没有特定于Portlet的WebApplicationContext实现类,至少我没有找到任何实现类.一个人总是可以做一个:)