用于Java的SCA客户机和实现模型
http://www.webjx.com/ 2007-11-09 00:58:14 来源:网页教学网 Sreedevi Penugonda
基本客户机模型
客户机可以从 SCA 组件和非 SCA 组件获得对服务的访问。
从 SCA 组件实现访问服务
获得对服务的访问的不同方法如下:
使用引用注入 通过定义服务的类型接口的 Java 类数据成员指定和通过 @Reference Annotation 加标注,可使用引用注入获得对服务的访问。@Reference Annotation 的特性有:
- name——引用的名称。
- required——是否需要服务的注入。
清单 11 显示了使用 @Reference Annotation 的 Java 实现。
清单 11. 使用 @Reference Annotation 的 Java 实现
| 以下为引用的内容: package services.profile; import org.osoa.sca.annotations.Service; import org.osoa.sca.annotations.Reference; @Service(LoginService.class) public class LoginServiceImpl implements LoginService{ @Reference(name="profileService", required=true) private ProfileService profileService; public int login(String employeeID, String password) { .... } } |
以下是上述组件实现对应的组件类型。
清单 12. 组件类型
| 以下为引用的内容: <?xml version="1.0" encoding="ASCII"?> <componentType xmlns="http://www.osoa.org/xmlns/sca/0.9"> <service name="ProfileService "> <interface.java interface="services.profile.LoginService"/> </service> <reference name="profileService"> <interface.java interface="services.profile.ProfileService"/> </reference> </componentType> |
使用模块上下文
使用模块上下文访问服务的必要事项有:
- 字段必须定义为能接受注入的模块上下文。
- 必须对注入的模块上下文调用方法。
通过将字段定义为 ModuleContext 类型,或使用 @Context Annotation,可指定使用模块上下文进行服务访问。清单 13 显示了 ModuleContext 接口及其 locateService() 方法。
清单 13. ModuleContext 接口
| 以下为引用的内容: package org.osoa.sca; public interface ModuleContext { ... Object locateService(String serviceName); } |
清单 14 显示了使用 @Context Annotation 的模块上下文定义示例:
清单 14. 使用 @Context Annotation 的模块上下文定义
| 以下为引用的内容: package innovasolutions.web.ui; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.osoa.sca.CurrentModuleContext; import org.osoa.sca.ModuleContext; import services.profile.LoginService; public class LoginServlet extends HttpServlet{ private ServletContext mContext; public void init(ServletConfig pCfg) throws ServletException{ mContext = pCfg.getServletContext(); } public void doPost(HttpServletRequest pReq, HttpServletResponse pResp) throws ServletException{ LoginService loginMgr = (LoginService)CurrentModuleContext.getContext(). locateService("LoginServiceComponent"); if (loginMgr == null){ throw new ServletException("LoginManager not found"); } . . . . } |




文章评论
共有 0 位网友发表了评论 查看完整内容