spring - 组件扫描在 Tomcat webapp 的 JAR 中找不到 @Component

标签 spring web-applications applicationcontext opencms

我刚刚在 Spring bugsystem ( https://jira.springsource.org/browse/SPR-8551 ) 中提交了一个错误,但我仍然不确定我是否遗漏了什么

我通过 <context:component-scan/> 找到了一个问题对这个声明。
给定以下两个类,它们位于 Web 应用程序的 WEB-INF/lib 中的同一个 JAR 中(JAR 文件具有目录结构):

测试/TheBean.java:

package test;
@Component
public class TheBean{
}

测试/BeanSearcher.java:
package test;
public class BeanSearcher{

  public void init(){ 
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
    ctx.scan("test"); 
    ctx.refresh(); 
    TheBean b=  ctx.getBean(TheBean.class); 
    // What is the value of b? 
  }
}

如果我运行 new BeanSearcher().init()在 jUnit 测试用例或其他类型的独立应用程序中,b 被分配了一个 TheBean 实例,但是如果我在 JSP 中运行它,ctx.getBean()正在返回空值。

那么,我是做错了什么还是没有考虑到某些事情,这只是一个错误......?

编辑 8/8/2011:当我试图简化问题时,它似乎工作得很好,但是,当我尝试让它工作时,在 OpenCms 的初始化中,它还是失败了。现在我试图寻找工作版本和不工作版本之间的差异。 (类加载器,相关类在不同 JAR 中的泛化或直接在 WEB-INF/classes 中,通过反射调用等)

最佳答案

正如我在评论中所写,解决方案由此处的答案给出:
Spring Annotation-based controllers not working if it is inside jar file

When you export the jar file using the export utility in eclipse there is a option called Add directory entries.

关于spring - 组件扫描在 Tomcat webapp 的 JAR 中找不到 @Component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773912/

相关文章:

HTML 表单 Action 标签。调用父相对 URL

java - Maven WebApp META-INF context.xml

javascript - jQTouch在AJAX页面加载时执行代码

spring - 与纯@ContextConfiguration相比,使用@ContextHierarchy有什么优势?

java - Spring jpa native 查询在有 0 个结果时抛出 MySQLSyntaxErrorException

spring - 如何将 Controller 方法内部生成的变量传递给 ExceptionHandler

spring - CSRF token 在登录期间过期

ajax - 一个适合ajax应用的广告系统

java - Spring在运行时添加占位符值

spring - 是否可以在类之间缓存 Spring 的应用程序上下文?