2012-03-14 61 views
0

请原谅,如果我使用术语错误,但我是新来的java-web开发和春天。 纠正我的假设,如果我错了。如何将Spring DI与2个或更多的servlet一起使用?

我想创建一个WebApp,它使用Vaadin进行UI和Spring MVC的RESTful Web服务。 所以,我想我需要2个Servlets。 一个服务于Vaadin,另一个服务于Spring MVC。

我有一些通用的dao bean,这两个servlet都使用它,所以我认为我可以使用Spring和@Autowired注释将这些@Repository和@Component标记的bean注入MVC​​和Vaadin - “程序(App | Servlet的)”。 (通过弹簧组件扫描功能发现)

但我无法让它工作。 Spring-MVC-应用程序 - 工作。我注释了所有的@Controllers,所有的东西都是自动的@Autowired。

但在Vaadin我总是得到:

SCHWERWIEGEND: Servlet.service() for servlet [hello] in context with path [/pliste] threw exception [javax.servlet.ServletException: failed to acquire new instance of class net.d21.pliste.HelloWorld] with root cause 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [net.d21.pliste.HelloWorld] is defined: expected single bean but found 0: 

我想,我的基本问题是,如何注入依赖一般为2周不同的servlet? (在我的情况下,它是Vaadin和MVC,但我认为它是可替换的)。

回答

1

一个典型的Spring web项目具有应用程序上下文2层次结构:

  • 根应用程序上下文由ContextLoaderListener加载,其默认的配置文件是applicationContext.xml。此上下文包含可供所有servlet,过滤器等使用的通用bean。

  • 特定于Servlet的应用程序上下文。他们的默认配置位置是${serlvet-name}-servlet.xml。这些上下文包含特定于particualr servlet的bean。

所以,你的情况,你需要在applicationContext.xml声明菜豆和特定的servlet-豆(例如,用于Spring MVC控制器)在特定的servlet的背景。

请注意,如果您使用<component-scan>,则需要通过使用不同的基本包或filtering them by annotations避免在不同上下文中重复bean。

+0

您的评论,espacialy您的链接帮助我得到它的工作。谢谢! – crushervx 2012-03-15 12:57:33

相关问题