English 中文(简体)
取决于扫描和对比过滤
原标题:context depended scan-component filter

My SpringMVC based webapp uses typically 2 contexts: the webapplication context for the MVC dispatcher servlet and the parent/root application context.

<!-- the context for the dispatcher servlet -->
<servlet>
    <servlet-name>webApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
....
<!-- the context for the root/parent application context -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:root-context.xml</param-value>
</context-param>

Within these contexts, I use component scanning for loading all beans. My packages are named according their usecase (e.g. com.abc.registration, com.abc.login etc.) rather then based on the technological tier (e.g. com.abc.dao, com.abc.services etc.)

Now my question: in order to avoid duplicate scanning of some classes, is it a good practice, to filter the candidate component classes for both contexts, e.g. include only the MVC Controller for web context scan and include all other components (services, dao/repositorie) in the root application context ?

<!-- servlet-context.xml -->
<context:component-scan base-package="com.abc.myapp" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- root-context.xml -->
<context:component-scan base-package="de.efinia.webapp">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

或者,避免该构成部分出现这种重复,既不重要也不必要?

最佳回答

我赞同你在两个领域的解决办法:

  1. 你根据使用案例而不是分层划分课程。 如果你有一套所有控制器的“>>包,那么你就没有问题。 但我仍然认为这种做法更好。

  2. 是的,你应当过滤教室。 显然,这并不是记忆足迹增加的问题,因为这微不足道(但启动时间可能大大增加)。

但是,有重复的豆类(既包括控制器,也包括服务豆)可能会带来微妙的 b和不一致之处。 一些连接库已经启动两次,一些开端 h头有两个原因造成意外行为。 如果您使用<代码>singleton,则保持这一范围。 也许你们会立即遇到一些问题,但听任合同。

BTW指出,有<条码>和>;mvc:通知驱动/>。 tag。

问题回答

这确实是一种良好做法。 母体应用环境不应有控制器。

我可以提出更多的理由来证明这种做法是正当的,但这样做肯定更清洁。





相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签