简体   繁体   English

如何将spring beans注入jsp 2.0 SimpleTag?

[英]How to inject spring beans into a jsp 2.0 SimpleTag?

Currently my jsp 2.0 tags that need spring beans use this code: 目前我需要spring bean的jsp 2.0标签使用此代码:

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

The I just get the first matching bean. 我刚刚得到第一个匹配的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. 这段代码运行正常,但是有一个不希望出现的缺点,我花了大约一半的页面渲染时间来查找spring bean,因为每次调用一个标签时都会发生这种情况。 I was thinking maybe to put the bean into application scope or at least session scope. 我想也许可以将bean放入应用程序范围或至少是会话范围。 But what's really the smartest way of handling this problem ? 但是,处理这个问题的最聪明方法是什么?

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 . 假设这一个问题,那么替代方案是InternalResourceViewResolverexposeContextBeansAsAttributesexposedContextBeanNames属性。 You can use one or the other (but not both) to expose some or all of your beans as JSP attributes. 您可以使用其中一个(但不是两个)将一些或所有bean作为JSP属性公开。

This raises the possibly of actually injecting Spring beans into your tag classes. 这引发了实际将Spring bean注入标记类的可能性。 For example, in your Spring context you can have: 例如,在Spring上下文中,您可以:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="exposeContextBeansAsAttributes" value="true"/>
</bean>

<bean id="myBean" class="com.x.MyClass"/>

Your JSP: 你的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. 因此,如果MyTag定义属性thing类型MyClass ,在myBean的Spring bean应该得到注入作为一个正常的JSP属性。

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. 一种更简单的方法是在标记类上使用@Configurable注释,这将使Spring在标记初始化时自动连接依赖项。 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. 然后可以使用@AutoWired批注标记任何所需的依赖项,即使标记未在Spring容器中初始化,Spring也会依赖于依赖关系。

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. 在您的applicationcontext中,您必须注册两者,以便JSP标记可以有一次机会由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 :) 很酷,现在我们的JSP标签中可以看到一个服务:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM