簡體   English   中英

Spring 3和Struts2集成自動裝配

[英]Spring 3 and Struts2 Integration Autowiring

這是我的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;
    }
}

這是我的applicationContext.xml的一個片段

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

該代碼工作正常,除了我在聲明中更改id時。

我的問題為什么為什么Spring <bean id ="eventService"> id必須與EventAction支持類中的eventService實例變量匹配? id是不是只是為將要創建的bean創建標識符? 為什么bean標簽內的id應該與我的EventAction內部相同,而在配置中甚至沒有提到EventAction類?

從Spring docs beans-beanname

每個bean具有一個或多個id(也稱為標識符或名稱;這些術語指的是同一件事)。 這些ID在該bean所在的容器內必須唯一。一個bean幾乎總是只有一個id,但是如果一個bean有多個id,則多余的ID本質上可以視為別名。 使用基於XML的配置元數據時,可以使用'id'或'name'屬性來指定Bean標識符。 “ id”屬性允許您確切指定一個id,並且由於它是真正的XML元素ID屬性,因此當其他元素引用id時,XML解析器可以進行一些額外的驗證。 因此,這是指定bean ID的首選方法。 但是,XML規范確實限制了XML ID中合法的字符。 通常這不是一個約束,但是如果您需要使用這些特殊XML字符之一,或者想為bean引入其他別名,則也可以或者改為指定一個或多個bean id,並用逗號(, ),分號(;)或'name'屬性中的空格。

我相信對於Spring-Struts2,您使用的插件需要定義自動接線策略,否則插件將使用默認name

這意味着當Struts2和Spring之間的插件橋時,它將嘗試根據提供的自動裝配策略注入bean。

請參閱Struts2彈簧插件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM