Spring 2.5托管servlet:如何? [英] Spring 2.5 managed servlets: howto?
问题描述
如果有任何错误,请纠正我。
据我了解,所有Spring功能,即通过装豆的DI都可以工作。 b $ b
Spring Context,即getBean()方法。
否则,即使我的方法标记为@Transactional,也无法正常工作,我将
使用新的运算符创建拥有类,将不提供任何交易管理。
我将Tomcat 6用作servlet容器。
所以,我的问题是:如何使Spring框架管理Servlet方法。
这里的问题是我使用了一个框架,它的servlet扩展了
基本java Servlets的功能,因此他们有更多方法。尽管如此,web.xml仍然像往常一样存在于应用程序中。
问题是我不控制servlet的创建流程,只能覆盖一些方法
每个servlet的
基本上将流程记录在某个xml文件中,但是我使用图形gui控制此过程
。
因此,基本上,我只向每个Servlet的一些方法中添加一些代码。
如何使这些方法由Spring框架管理?我需要做的基本事情是
使这些方法具有事务性(@Transactional)。
向博佐评论:
@Bozho让我们看看。
在这些servlet的方法中,我使用框架功能,例如,在当前会话中获取并保存的特殊变量。
所需的是,遍历这些基于框架的集合,同时将一些值保存在数据库中。
您建议的是引入一个新的非常复杂的对象,以便可以将其传递到服务层。 (服务层对框架,它的类和当前会话中保存的对象一无所知!
首先,我们将基于框架的集合包装到这样的对象,因此将所有内容复制到其中。
然后同样,Service层方法应该将更改保存在数据库中,或者更糟的情况是,返回一个新的复杂对象,以便Servlet框架方法可以根据Service层方法执行的结果来更新框架变量。但是您认为这是个好主意吗?
您也可以直接定义servlet在Spring应用程序上下文中,您将需要在 web.xml
中注册的代理 servlet,并委派到在<$ c $中配置为bean的servlet实例。 c> applicationContext.xml 。代理servlet配置了目标servlet bean的名称,它通过 WebApplicationContextUtils.getRequiredWebApplicationContext()。getBean(...)
并将所有处理委托给目标servlet。在这种情况下,您的servlet实例完全由Spring管理。
Correct me if anything is wrong.
As I understand, all Spring functionality, namely DI works when beans are got thru
Spring Context, ie getBean() method.
Otherwise, none can work, even if my method is marked @Transactional and I will
create the owning class with a new operator, no transaction management will be provided.
I use Tomcat 6 as a servlet container.
So, my question is: how to make Servlet methods managed by Spring framework.
The issue here is that I use a framework, and its servlets extend the functionality of
basic java Servlets, so they have more methods. Still, web.xml is present in an app as usual.
The thing is that I do not control the servlets creation flow, I can only override a few methods
of each servlet, the flow is basically written down in some xml file, but I control this process
using a graphical gui.
So, basically, I only add some code to a few methods of each Servlet.
How to make those methods managed by Spring framework? The basic thing I need to do is
making these methods transactional (@Transactional).
comment to Bozho:
@Bozho Let's see.
In these servlets' methods I work with framework capabilities, let's say special variables that are got and saved in the current session.
And what is needed, is looping through those framework-based collections while saving some values in a database.
What you suggest is introducing a new very complex object, so that it could be passed to a service layer. (Service layer will not know anything about framework, its classes and objects kept in current Session!
First, we "wrap" framework based collections to such a object, so copy everything into it.
Then, again, the Service layer method should either save changes in a database or, worse case, return a new complex object so that Servlet framework method could update framework variables depending on the result of Service layer method execution.
It is a workaround but do you think it is a good idea?
You can also define your servlets directly in the Spring application context. You'll need a "proxy" servlet registered in web.xml
and delegating to the servlet instance which is configured as bean in the applicationContext.xml
. Proxy servlet is configured with the name of the target servlet bean, it discovers this bean via WebApplicationContextUtils.getRequiredWebApplicationContext().getBean(...)
and delegates all the processing to the target servlet. In this case an instance of your servlet is completely managed by Spring.
这篇关于Spring 2.5托管servlet:如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!