简体   繁体   English

Spring 3和Struts2集成自动装配

[英]Spring 3 and Struts2 Integration Autowiring

This is my eventAction ActionSupport Class 这是我的eventAction ActionSupport类

public class EventAction extends ActionSupport {

    protected EventService eventService;

    protected String redirectUrl;

    public String getRedirectUrl() {
        return redirectUrl;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public void setEventService(EventService services) {
        this.eventService = services;
    }
}

And here is a fragment from my applicationContext.xml 这是我的applicationContext.xml的一个片段

<bean id ="eventService" class ="services.EventService" scope ="singleton">
        <property name = "sessionFactory" ref = "sessionFactory"/>
    </bean>

The code is working fine except for when I change the id inside the declartion. 该代码工作正常,除了我在声明中更改id时。

My question why does spring <bean id ="eventService"> id has to be matched with eventService instance variable inside EventAction support class? 我的问题为什么为什么Spring <bean id ="eventService"> id必须与EventAction支持类中的eventService实例变量匹配? isn't id is just making an identifier for the bean that is going to be created? id是不是只是为将要创建的bean创建标识符? why should the id inside the bean tag should be the same inside my EventAction, where the EventAction class is not even being mentioned in the configruation? 为什么bean标签内的id应该与我的EventAction内部相同,而在配置中甚至没有提到EventAction类?

From Spring docs beans-beanname 从Spring docs beans-beanname

Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). 每个bean具有一个或多个id(也称为标识符或名称;这些术语指的是同一件事)。 These ids must be unique within the container the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases. 这些ID在该bean所在的容器内必须唯一。一个bean几乎总是只有一个id,但是如果一个bean有多个id,则多余的ID本质上可以视为别名。 When using XML-based configuration metadata, you use the 'id' or 'name' attributes to specify the bean identifier(s). 使用基于XML的配置元数据时,可以使用'id'或'name'属性来指定Bean标识符。 The 'id' attribute allows you to specify exactly one id, and as it is a real XML element ID attribute, the XML parser is able to do some extra validation when other elements reference the id; “ id”属性允许您确切指定一个id,并且由于它是真正的XML元素ID属性,因此当其他元素引用id时,XML解析器可以进行一些额外的验证。 as such, it is the preferred way to specify a bean id. 因此,这是指定bean ID的首选方法。 However, the XML specification does limit the characters which are legal in XML IDs. 但是,XML规范确实限制了XML ID中合法的字符。 This is usually not a constraint, but if you have a need to use one of these special XML characters, or want to introduce other aliases to the bean, you may also or instead specify one or more bean ids, separated by a comma (,), semicolon (;), or whitespace in the 'name' attribute. 通常这不是一个约束,但是如果您需要使用这些特殊XML字符之一,或者想为bean引入其他别名,则也可以或者改为指定一个或多个bean id,并用逗号(, ),分号(;)或'name'属性中的空格。

I believe for Spring-Struts2 you are using the plugin where you need to either define auto-wire strategy or plugin will use default one which is name . 我相信对于Spring-Struts2,您使用的插件需要定义自动接线策略,否则插件将使用默认name

That means when plugin bridge between Struts2 and Spring it will try to inject beans based on the supplied auto-wire strategy 这意味着当Struts2和Spring之间的插件桥时,它将尝试根据提供的自动装配策略注入bean。

Refer to Struts2 Spring-plugin 请参阅Struts2弹簧插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM