文档here说:


该框架将在初始化DispatcherPortlet时寻找
在以下位置的WEB-INF目录中名为[portlet-name] -portlet.xml的文件
您的Web应用程序并创建在那里定义的bean(覆盖
在全局中使用相同名称定义的任何bean的定义
范围)。


如果可以的话,我会使用注释进行配置,因为保持配置和实际代码同步很容易。因此,我项目中的[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。但是现在我很高兴:-)。

最佳答案

您是否尝试过使用@Configuration注释类进行配置?从3.0版开始,它是spring框架中的功能

请参阅此处的参考文档:

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初始化参数

contextClass

实现WebApplicationContext的类,该类将用于实例化
此Portlet使用的上下文。如果未指定此参数,则
将使用XmlPortletApplicationContext。


WebApplicationContext的实现可以在文档中找到:

http://static.springsource.org/spring/docs/current/api/org/springframework/web/context/WebApplicationContext.html

似乎没有特定于Portlet的WebApplicationContext实现类,至少我没有找到任何实现类。一个人总是可以做一个:)

09-30 17:12
查看更多