如何将spring beans注入jsp 2.0 SimpleTag? [英] How to inject spring beans into a jsp 2.0 SimpleTag?

查看:75
本文介绍了如何将spring beans注入jsp 2.0 SimpleTag?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我需要spring bean的jsp 2.0标签使用此代码:

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

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

我只是得到第一个匹配的bean。

The I just get the first matching bean.

这段代码工作得很好,但是有一个不希望出现的缺点,我花了大约一半的页面渲染时间来查找spring 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. I was thinking maybe to put the bean into application scope or at least session scope. 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.

假设 是一个问题,那么替代方案是 exposeContextBeansAsAttributes exposedContextBeanNames InternalResourceViewResolver 的属性。您可以使用其中一个(但不是两个)将一些或所有bean作为JSP属性公开。

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.

这会引发实际将Spring bean注入到您的bean中标签类。例如,在Spring上下文中,您可以:

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"/>

您的JSP:

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

如果 MyTag 定义属性<$类型 MyClass 的c $ c> thing , myBean spring bean应该被注入一个普通的JSP属性。

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

这篇关于如何将spring beans注入jsp 2.0 SimpleTag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆