1

I wrote a Tomcat valve and configured it in server.xml. So far so good. However, I want one of the valve's data members to be a Spring managed bean. So, how can I make the valve also be Spring managed so that I can have Spring's IoC inject that dependency into the valve?

skaffman
  • 398,947
  • 96
  • 818
  • 769
user585037
  • 21
  • 2

1 Answers1

1

Valves aren't associated with an application, while spring contexts are. So you can't have a spring-managed bean in a Valve.

You can, of course, instantiate the spring context in the Valve constructor, and use context.autowireBean(this) there, but this will be a separate spring context, not one from any of the contexts available.

Technically, you have access to the contexts from the Valve, but afaik it is from the request, so you can obtain the ApplicationContext for each servlet context, and from there - get a reference to a bean, but that sounds odd.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks for the quick answer. Currently, we implement a common functionality for several applications deployed into a Tomcat as ServletFilters. We want to consolidate that into a valve. The valve itself doesn't relate directly to other beans in the deployed application. It does, however, dependent on some libs that are spring wired. So I just wanted to have spring wire all these beans and inject them in the valve. I think your suggestion will work. Thanks. – user585037 Jan 21 '11 at 21:03