简体   繁体   中英

Spring 3 and Struts2 Integration Autowiring

This is my eventAction ActionSupport Class

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

<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.

My question why does spring <bean id ="eventService"> id has to be matched with eventService instance variable inside EventAction support class? isn't id is just making an identifier for the bean that is going to be created? 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?

From Spring docs beans-beanname

Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). 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. When using XML-based configuration metadata, you use the 'id' or 'name' attributes to specify the bean identifier(s). 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; as such, it is the preferred way to specify a bean id. However, the XML specification does limit the characters which are legal in XML IDs. 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.

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 .

That means when plugin bridge between Struts2 and Spring it will try to inject beans based on the supplied auto-wire strategy

Refer to Struts2 Spring-plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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