Currently my jsp 2.0 tags that need spring beans use this code:
ac = WebApplicationContextUtils.getWebApplicationContext( servletContext);
ac.getBeansOfType(MyRequestedClass.class);
The I just get the first matching bean.
This code works fine, but has the undesired drawback that I spend about half my page rendering time looking up spring beans, since this happens every time a tag is invoked. I was thinking maybe to put the bean into application scope or at least session scope. But what's really the smartest way of handling this problem ?
Another way to achieve this is use a static property to hold the dependency. Just like below:
In you applicationcontext, you have to register both so that the JSP tag can get one chance to be initiated by Spring. We we Go with the magic...
Cool huh, now aService is visible in our JSP tag :)
A simpler way would be to use @Configurable annotation on your tag class, this would make Spring automatically wire the dependencies in when the tag is initialized. Any required dependencies can be then be tagged with @AutoWired annotation and Spring will wire in the dependency even if the tag is not initialized within the Spring container.
My first thought is, are you sure the calls to spring are expensive? This stuff is pretty heavily optimized, so make sure it's actually a problem before trying to optimize it.
Assuming it is a problem, then an alternative is the
exposeContextBeansAsAttributes
andexposedContextBeanNames
properties ofInternalResourceViewResolver
. You can use one or the other (but not both) to expose some or all of your beans as JSP attributes.This raises the possibly of actually injecting Spring beans into your tag classes. For example, in your Spring context you can have:
Your JSP:
SO if
MyTag
defines an attributething
of typeMyClass
, themyBean
spring bean should get injected as a normal JSP attribute.