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

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

问题描述

我们正在使用具有弹簧框架工作的Struts 2( https://struts.apache .ORG /文档/弹簧plugin.html )。我有一些关于Struts bean创建的问题。



当我们在Spring中使用Struts时,我们可以轻松地使用spring @Inject @Value @Resource 操作验证器拦截器 没有 将其中的任何一个注释为 @Component (或 @Named )。这个 似乎 ,这个struts创建的对象是弹簧管理的bean。



这不是真的,因为当你看在 applicationContext.getBeanDefinitionNames()你找不到任何动作,验证器或拦截器。



所以如果Struts创建对象不是spring管理bean,为什么spring注释( @Inject @Value ,...)好?



在技术上可以创建一个新对象(弹簧启动后),并将其传递给Spring并让Spring设置它?如何?

解决方案

Struts使用 ObjectFactory 来构建任何对象操作,拦截器,验证器等:


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


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


Struts对象工厂,与Spring集成。


它覆盖了构建对象的方法,如 buildBean ()



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



通常会创建一个新的clazz参数实例。


此类的中央方法:创建一个bean实例,
填充bean实例,应用后处理器等。


< blockquote>

然后,新创建的实例被传递给自动装配,使用 org.springfra mework.beans.factory.config.AutowireCapableBeanFactory#autowireBeanProperties()



之后,将此实例传递给Guice进行注入。 >

现在已经完全烘焙并准备返回。






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


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.

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.

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

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

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 uses an ObjectFactory to build any object like actions, interceptors, validators, etc.:

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 plugin registers its own object factory StrutsSpringObjectFactory:

Struts object factory that integrates with Spring.

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

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

which generally creates a new instance of the clazz argument.

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

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

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

Now it's fully baked and ready to return.


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天全站免登陆