How to inject spring beans into a jsp 2.0 SimpleTag?

后端 未结 3 1006
天命终不由人
天命终不由人 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: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:

    
      
    
    
    
    

    Your JSP:

    
    

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

提交回复
热议问题