spring-使用注释摆脱* -portlet.xml.可能?怎么样?

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.但是现在我很高兴:-).

最佳答案
您是否尝试过使用@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 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的实现可以在文档中找到:

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

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

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...