开发者

Using component-scan in a web project

I have an Eclipse "Dynamic Web Project" and a Tomcat server configured in Eclipse to use that project. I have a file extension mapped to a servlet, and in the servlet config I have a component-scan element setup like this:

<context:component-scan base-package="com.mycompany.web" />

When my web server starts up, I see this error message in the log:

May 6, 2011 9:50:23 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet cap
java.la开发者_运维百科ng.IllegalArgumentException: Resource path [C:\...\src\main\webapp\WEB-INF\classes\com\mycompany\web] does not denote a directory

That directory does not exist (except in the packaged .war file). However, the com.mycompany.web package does exist.

Is there another way to do a component-scan? It needs to either look in another folder, or somehow find it in my project or something...


It doesn't appear to be possible to do a component-scan in this way. It does a directory listing, which I guess isn't possible when the class files aren't physically located where Spring is expecting. I solved this just by not using a component-scan, and listing the controllers in the spring config like this:

<context:annotation-config />
<bean class="com.mycompany.web.MyController"/>

Annotations in the controller still work, so listed the controllers manually is the only extra step.


it's possible to do what you want, but you need a little extra configuration.

You can annotate your classes with @Controller or @Repository or other Spring stereotype annotations and Spring will pick them up automatically.

Or you can give context:component-scan a bit more info. Example:

<context:component-scan base-package="com.mycompany.web">
    <!-- scan all classes in the com.mycompany.web package (but
         not in subpackages) -->
    <context:include-filter type="aspectj" expression="com.mycompany.web.*"/>


    <!-- or if you want to include subpackages this config scans all
         classes in com.mycompany.web.special and in its subpackages
         (that's what the ..* means) -->
    <context:include-filter type="aspectj" expression="com.mycompany.web.special..*"/>
</context:component-scan>

Finally, if you don't like using aspectj, context:include-filter's type can be a regular expression, custom annotation, etc. See this link for more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜