Java 如何将 spring bean 注入 jsp 2.0 SimpleTag?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1296052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inject spring beans into a jsp 2.0 SimpleTag?
提问by krosenvold
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. 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 ?
这段代码工作正常,但有一个不受欢迎的缺点,我花了大约一半的页面渲染时间来查找 spring bean,因为每次调用标记时都会发生这种情况。我在想也许将 bean 放入应用程序范围或至少是会话范围。但是处理这个问题的最聪明的方法是什么?
采纳答案by skaffman
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.
我的第一个想法是,你确定调用 spring 很贵吗?这些东西经过了大量优化,因此在尝试优化它之前,请确保它确实是一个问题。
Assuming it isa 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.
假设这是一个问题,那么另一种选择是 的exposeContextBeansAsAttributes
和exposedContextBeanNames
属性InternalResourceViewResolver
。您可以使用其中一个(但不能同时使用)将部分或全部 bean 作为 JSP 属性公开。
This raises the possibly of actually injecting Spring beans into your tag classes. For example, in your Spring context you can have:
这增加了将 Spring bean 实际注入标签类的可能性。例如,在您的 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
type属性MyClass
,myBean
spring bean 应该作为一个普通的 JSP 属性被注入。
回答by Biju Kunjummen
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.
一种更简单的方法是在标签类上使用 @Configurable 注释,这将使 Spring 在标签初始化时自动连接依赖项。然后可以使用@AutoWired 注释标记任何必需的依赖项,即使标记未在 Spring 容器中初始化,Spring 也会连接到依赖项中。
回答by user698188
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...
在您的应用程序上下文中,您必须同时注册两者,以便 JSP 标记有机会被 Spring 启动。我们带着魔法走……
<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 标签中可以看到 aService :)