0

Please forgive if i use terminology wrong, but i'm new to java-web development and Spring. Correct my assumptions, if i'm wrong.

I want to create a WebApp, that uses Vaadin for UI and Spring MVC for RESTful Web-Services. So, i think, I need 2 Servlets. One for serving Vaadin and one for serving Spring MVC.

I have some generic dao-beans, which are used by both servlets, so i thought, that i could use Spring, with @Autowired annotation to inject those @Repository and @Component - tagged beans into the MVC and the Vaadin-"(App|Servlet)". (Discovered through springs component-scan - feature)

But i can't get it working. The Spring-MVC-App - works. I've annotated all @Controllers and all things are @Autowired automaticly.

But in Vaadin i always get:

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: 

I think my basic question is, how to inject general dependencies into 2 different servlets? (In my case it's Vaadin and MVC, but i think it's replaceable).

crushervx
  • 587
  • 1
  • 7
  • 18

1 Answers1

1

A typical Spring webapp has a 2-level hierarchy of application contexts:

  • Root application context loaded by ContextLoaderListener, its default config file is applicationContext.xml. This context contains common beans that can be used by all servlets, filters, etc.

  • Servlet-specific application contexts. Their default config locations are ${serlvet-name}-servlet.xml. These contexts contain beans specific to particualr servlets.

So, in your case you need to declare common beans in applicationContext.xml and servlet-specific beans (for example, controllers for Spring MVC) in servlet-specific contexts.

Note that if you use <component-scan> you need to avoid duplication of beans in different contexts, either by using different base packages or by filtering them by annotations.

Community
  • 1
  • 1
axtavt
  • 239,438
  • 41
  • 511
  • 482