package org.springframework.webflow.execution; public interface Action { public Event execute(RequestContext context) throws Exception; }
在这个接口中,定义了一个没有具体实现的方法,方法名叫做execute(),返回类型是Event。如前面第一条所述,接口中的方法都是没有实现的。这些方法的具体实现是在实现(implements)这个接口的类中给出的。
再来看一个实现Action接口的抽象类AbstractAction,代码如下。
package org.springframework.webflow.action; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.ClassUtils; import org.springframework.webflow.core.collection.AttributeMap; import org.springframework.webflow.execution.Action; import org.springframework.webflow.execution.Event; import org.springframework.webflow.execution.RequestContext; public abstract class AbstractAction implements Action, InitializingBean { protected final Log logger = LogFactory.getLog(getClass()); public EventFactorySupport getEventFactorySupport() { return new EventFactorySupport(); } public void afterPropertiesSet() throws Exception { try { initAction(); } catch (Exception ex) { throw new BeanInitializationException("Initialization of this Action failed: " + ex.getMessage(), ex); } } protected void initAction() throws Exception { } protected Event success() { return getEventFactorySupport().success(this); } protected Event success(Object result) { return getEventFactorySupport().success(this, result); } protected Event error() { return getEventFactorySupport().error(this); } protected Event error(Exception e) { return getEventFactorySupport().error(this, e); } protected Event yes() { return getEventFactorySupport().yes(this); } protected Event no() { return getEventFactorySupport().no(this); } protected Event result(boolean booleanResult) { return getEventFactorySupport().event(this, booleanResult); } protected Event result(String eventId) { return getEventFactorySupport().event(this, eventId); } protected Event result(String eventId, AttributeMap resultAttributes) { return getEventFactorySupport().event(this, eventId, resultAttributes); } protected Event result(String eventId, String resultAttributeName, Object resultAttributeValue) { return getEventFactorySupport().event(this, eventId, resultAttributeName, resultAttributeValue); } public final Event execute(RequestContext context) throws Exception { Event result = doPreExecute(context); if (result == null) { result = doExecute(context); doPostExecute(context); } else { if (logger.isInfoEnabled()) { logger.info("Action execution disallowed; pre-execution result is '" + result.getId() + "'"); } } return result; } protected String getActionNameForLogging() { return ClassUtils.getShortName(getClass()); } protected Event doPreExecute(RequestContext context) throws Exception { return null; } //抽象方法 protected abstract Event doExecute(RequestContext context) throws Exception; protected void doPostExecute(RequestContext context) throws Exception { } }
在抽象类AbstractAction中,既有具体实现的方法,又有没有具体实现的抽象方法
//抽象方法 protected abstract Event doExecute(RequestContext context) throws Exception;