Struts 2 和 Spring 如何处理 struts 创建的对象 [英] Struts 2 and Spring how struts-created objects handled

查看:24
本文介绍了Struts 2 和 Spring 如何处理 struts 创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 Struts 2 和 spring 框架 4 (https://struts.apache.org/docs/spring-plugin.html).我有一些关于 Struts bean 创建的问题.

We are using Struts 2 with spring frame work 4 (https://struts.apache.org/docs/spring-plugin.html). I have some question about Struts bean creation.

当我们在 Spring 中使用 Struts 时,我们可以轻松地在 Actions@Inject@Value@Resourcecode>、ValidatorsInterceptors没有 将它们中的任何一个注释为 @Component(或 @Named).这似乎 struts 创建的对象是 spring 管理的 bean.

When we use Struts with Spring we can easily use spring @Inject, @Value, @Resource in Actions, Validators and Interceptors, without annotating any of them as @Component (or @Named). This seems that struts-created objects are spring managed beans.

这不是真的,因为当您查看 applicationContext.getBeanDefinitionNames() 时,您找不到任何操作、验证器或拦截器.

This is not true, because when you look at applicationContext.getBeanDefinitionNames() you can not find any action, validator or interceptor.

如果 Struts 创建的对象不是 spring 管理 bean,为什么 spring 注释(@Inject@Value、...)工作得很好?

So if Struts-created objects are not spring manage beans, why the spring annotations ( @Inject, @Value, ...) works quite well?

我们是否可以在技术上创建一个新对象(在 spring 启动之后)并将其传递给 Spring 并让 Spring 设置它?怎么样?!

Is it technically possible that we create a new object (after spring start up) and pass it to Spring and let Spring setup it? How?!

推荐答案

Struts 使用 ObjectFactory 构建任何对象,如动作、拦截器、验证器等:

Struts uses an ObjectFactory to build any object like actions, interceptors, validators, etc.:

ObjectFactory 负责构建核心框架对象.用户可以注册他们的ObjectFactory 的自己的实现来控制这些对象的实例化.

ObjectFactory is responsible for building the core framework objects. Users may register their own implementation of the ObjectFactory to control instantiation of these Objects.

Struts-Spring插件注册自己的对象工厂StrutsSpringObjectFactory:

与 Spring 集成的 Struts 对象工厂.

Struts object factory that integrates with Spring.

在那里,它覆盖了构建对象的方法,例如 buildBean().

There, it overrides methods building Objects, such as buildBean().

现在如果你看一下实现,它使用方法 org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean()

Now if you look at the implementation, it is using method org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean()

这通常会创建 clazz 参数的新实例.

which generally creates a new instance of the clazz argument.

这个类的中心方法:创建一个bean实例,填充 bean 实例,应用后处理器等.

Central method of this class: creates a bean instance, populates the bean instance, applies post-processors, etc.

然后,将新创建的实例传递给 org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties()

Then, the newly created instance is passed for autowiring with org.springframework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties()

之后,这个实例被传递给 Guice 进行注入.

After that, this instance is passed to Guice for injection.

现在它已完全烘烤并准备返回.

Now it's fully baked and ready to return.

请注意,如果 bean 由 Spring 管理,它会使用应用程序上下文中的 getBean 方法,否则如果 bean 不是由 Spring 管理的,则新实例总是由 struts-spring 插件创建.

Note that, if the bean is managed by Spring, it uses getBean method from application context, otherwise the new instance is created always by struts-spring plugin if the bean is not managed by Spring.

这篇关于Struts 2 和 Spring 如何处理 struts 创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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