以下是以一個Action為範例,
代碼:
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Component("login-action")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class LoginAction extends ActionSupport {
@Action(className = "login-action")
public String execute() {
return SUCCESS;
}
}
重點:
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Component("login-action")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class LoginAction extends ActionSupport {
@Action(className = "login-action")
public String execute() {
return SUCCESS;
}
}
- @Component("login-action")讓Spring管理LoginAction instance。
- @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)避開thread safe問題。
- @Action(className = "login-action")用來以Spring bean name覆蓋預設的full class name。
如此就可以在Convention Plugin的開發模式中運用Spring Plugin的特性。