How to inject spring beans into a jsp 2.0 SimpleTag?

后端 未结 3 998
天命终不由人
天命终不由人 2020-12-03 11:36

Currently my jsp 2.0 tags that need spring beans use this code:

ac = WebApplicationContextUtils.getWebApplicationContext( servletContext);
ac.getBeansOfType(         


        
相关标签:
3条回答
  • 2020-12-03 11:52

    Another way to achieve this is use a static property to hold the dependency. Just like below:

    public class InjectedTag extends SimpleTagSupport {
    //In order to hold the injected service, we have to declare it as static
        private static AService _service;   
    /***/   
    @Override   
    public void doTag() throws IOException {    
              getJspContext().getOut().
              write("Service injected: " + _service + "<br />");    
    }   
    public void setService(AService service) { 
            _service = service;     
    } 
    }
    

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

    <bean id="aService" class="com.foo.AService">
      <!-- configure the service. -->
    </bean>
    <bean class="com.foo.InjectedTag" >
      <property name="service"><ref local="aService"/></property>
    </bean>
    

    Cool huh, now aService is visible in our JSP tag :)

    0 讨论(0)
  • 2020-12-03 11:54

    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.

    0 讨论(0)
  • 2020-12-03 11:56

    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 and exposedContextBeanNames properties of InternalResourceViewResolver. 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:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="exposeContextBeansAsAttributes" value="true"/>
    </bean>
    
    <bean id="myBean" class="com.x.MyClass"/>
    

    Your JSP:

    <MyTag thing="${myBean}"/>
    

    SO if MyTag defines an attribute thing of type MyClass, the myBean spring bean should get injected as a normal JSP attribute.

    0 讨论(0)
提交回复
热议问题