Inject Spring Beans into JSP Tags

May 7, 2006 – 22:46 | aop, java, spring

Spring 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:

JAVA:
  1. // In order for itself to be picked up by
  2. // Spring for injection, a class needs to wear
  3. // this @Configurable "badge"...
  4. @Configurable
  5. public class InjectedTag extends SimpleTagSupport {
  6.     private AService _service;
  7.  
  8.     /***/
  9.     @Override
  10.     public void doTag() throws IOException {
  11.         getJspContext().getOut().write("Service injected: " + _service + "<br />");
  12.     }
  13.  
  14.     public void setService(AService service) {
  15.         _service = service;
  16.     }
  17. }

Now, in the Spring application context file:

CODE:
  1. <aop :spring-configured/>
  2.  
  3. <bean id="aService" class="com.foo.AService">
  4.   <!-- configure the service. -->
  5. </bean>
  6.  
  7. <bean class="com.foo.InjectedTag" lazy-init="true">
  8.   <property name="service"><ref local="aService"/></property>
  9. </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>

Trackback from your site, or follow the comments in RSS.
  1. 6 Responses to “Inject Spring Beans into JSP Tags”

  2. 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

  3. p3t0r, which problems are you getting? I use compile-time weaving (i.e. iajc). -- Jing

    By Jing Xue on Jan 13, 2007

  4. 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

  5. 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

  6. 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

  7. 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

Post a Comment