Inject Spring Beans into JSP Tags
May 7, 2006 – 22:46 | aop, java, springSpring 2.0 supports injecting beans into an arbitrary POJO, which isn't necessarily instantiated by a Spring bean factory. This powerful feature is mostly used in injecting into domain objects whose life cycle is usually managed by ORM tools such as Hibernate. But we don't have to stop here, e.g., JSP tags can also be injected the same way. Here's how:
First let's have a sample tag implementation:
-
// In order for itself to be picked up by
-
// Spring for injection, a class needs to wear
-
// this @Configurable "badge"...
-
@Configurable
-
public class InjectedTag extends SimpleTagSupport {
-
private AService _service;
-
-
/***/
-
@Override
-
getJspContext().getOut().write("Service injected: " + _service + "<br />");
-
}
-
-
public void setService(AService service) {
-
_service = service;
-
}
-
}
Now, in the Spring application context file:
-
<aop :spring-configured/>
-
-
<bean id="aService" class="com.foo.AService">
-
<!-- configure the service. -->
-
</bean>
-
-
<bean class="com.foo.InjectedTag" lazy-init="true">
-
<property name="service"><ref local="aService"/></property>
-
</bean>
That's all there is to it. The tag declaration in the TLD isn't really relevant, because it's no different from what you would do with any other tags, but for the sake of completeness...-
<tag>
<name>injectedTag</name>
<tag-class>com.foo.InjectedTag</tag-class>
<body-content>empty</body-content>
</tag>

6 Responses to “Inject Spring Beans into JSP Tags”
Nice example!
But when I was trying to get you example to work I had some problems, some information seems to be lacking....?
Are you using this in combination with Load or Runtime weaving, how did you make the setup work?
By p3t0r on Jan 12, 2007
p3t0r, which problems are you getting? I use compile-time weaving (i.e. iajc). -- Jing
By Jing Xue on Jan 13, 2007
A question hopes to resolve.
I need to invoke a bean's method in MyTag's doTag method. I can use ApplicatonContextUtil to get an applicationContext and then use getBean to get such bean. However, such bean configuration must be in the applicationContext.xml config file.
The question is, if the bean defined in another xml file, how do I can get it from the applicationContext variable?
Thanks.
By netsesame on Jan 17, 2007
netsesame, how are you loading that other xml file in which the bean is defined? Maybe try importing that file into applicationContext.xml?
By Jing Xue on Jan 17, 2007
Hi,
I've followed your instructions with no luck. I am using Spring 2.5.
Could you please tell me where I am going wrong?
I have changed the applicationContext.xml from to and have added the namespace xmlns:context="http://www.springframework.org/schema/context"
I have also added all the relevant AspectJ libraries
Now, I get an error of :
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.config.internalBeanConfigurerAspect': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public static org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect.aspectOf()] threw exception; nested exception is org.aspectj.lang.NoAspectBoundException: Exception while initializing org_springframework_beans_factory_aspectj_AnnotationBeanConfigurerAspect: java.lang.InstantiationError: org.springframework.beans.factory.wiring.BeanConfigurerSupport
Thanks in advance
By Gareth on Jun 25, 2008
Gareth, sorry I haven't got around to play with this with 2.5. 2.5 is supposed to be a drop-in upgrade though, so I'm pretty sure the technique should still work. Did you ask on the spring forums?
By Jing Xue on Jul 2, 2008