赞
踩
关于上下文(Context),我们在开发的过程中经常会遇到,比如开发 Struts2 的 ActionContext、 Spring 中的 ApplicationContext,上下文是贯穿整个系统或阶段生 命周期的对象,其中包含了系统全局的一些信息,比如登录之后的用户信息、账号信息,以 及在程序每一个阶段运行时的数据。
具体的代码业务逻辑图:
代码1:
Context:共享资源
package com.bjsxt.chapter19.demo01; /** * @author wmr * @date 2021/2/25 */ public class Context { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
QueryAction:查询接口
package com.bjsxt.chapter19.demo01;
/**
* @author wmr
* @date 2021/2/25
*/
public interface QueryAction {
void execute(Context context);
}
从数据库查id
package com.bjsxt.chapter19.demo01; import java.util.concurrent.TimeUnit; /** * @author whl * @date 2021/2/25 */ public class QueryFromDBAction implements QueryAction { @Override public void execute(Context context) { try { TimeUnit.SECONDS.sleep(1); String id="123456 - "+Thread.currentThread().getId(); context.setId(id); }catch (Exception e){ e.printStackTrace(); } } }
从第三方查名称
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。