Java 中的上下文究竟是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3918083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What exactly is a Context in Java?
提问by Christopher Perry
I Googled this and read the Java documentation, but I'm a bit confused. Can somebody please explain what a Context
is in plain English?
我在谷歌上搜索并阅读了 Java 文档,但我有点困惑。有人Context
可以用简单的英语解释什么是 a吗?
采纳答案by BalusC
In programming terms, it's the larger surrounding part which can have anyinfluence on the behaviour of the current unit of work. E.g. the running environment used, the environment variables, instance variables, local variables, state of other classes, state of the current environment, etcetera.
在编程术语中,它是更大的周围部分,可以对当前工作单元的行为产生任何影响。例如使用的运行环境、环境变量、实例变量、局部变量、其他类的状态、当前环境的状态等。
In some API's you see this name back in an interface/class, e.g. Servlet's ServletContext
, JSF's FacesContext
, Spring's ApplicationContext
, Android's Context
, JNDI's InitialContext
, etc. They all often follow the Facade Patternwhich abstracts the environmental details the enduser doesn't need to know about away in a single interface/class.
在某些 API 中,您会在接口/类中看到这个名称,例如 Servlet 的ServletContext
、JSF 的FacesContext
、Spring 的ApplicationContext
、Android 的Context
、JNDI 的InitialContext
等。它们都经常遵循Facade 模式,该模式抽象了最终用户不需要知道的环境细节。单个接口/类。
回答by chrismh
since you capitalized the word, I assume you are referring to the interface javax.naming.Context
. A few classes implement this interface, and at its simplest description, it (generically) is a set of name/object pairs.
由于您将这个词大写,我假设您指的是 interface javax.naming.Context
。一些类实现了这个接口,在最简单的描述中,它(一般)是一组名称/对象对。
回答by Will Hartung
A Context represents your environment. It represents the state surrounding where you are in your system.
上下文代表您的环境。它代表您在系统中所处位置的状态。
For example, in web programming in Java, you have a Request, and a Response. These are passed to the service method of a Servlet.
例如,在 Java 的 Web 编程中,您有一个请求和一个响应。这些被传递给 Servlet 的服务方法。
A property of the Servlet is the ServletConfig, and within that is a ServletContext.
Servlet 的一个属性是 ServletConfig,其中有一个 ServletContext。
The ServletContext is used to tell the servlet about the Container that the Servlet is within.
ServletContext 用于将 Servlet 所在的容器告知 servlet。
So, the ServletContext represents the servlets environment within its container.
因此, ServletContext 表示其容器内的 servlets 环境。
Similarly, in Java EE, you have EBJContexts that elements (like session beans) can access to work with their containers.
类似地,在 Java EE 中,您拥有元素(如会话 bean)可以访问以使用其容器的 EBJContext。
Those are two examples of contexts used in Java today.
这是当今 Java 中使用的上下文的两个示例。
Edit --
编辑 -
You mention Android.
你提到了安卓。
Look here: http://developer.android.com/reference/android/content/Context.html
看这里:http: //developer.android.com/reference/android/content/Context.html
You can see how this Context gives you all sorts of information about where the Android app is deployed and what's available to it.
您可以看到此上下文如何为您提供有关 Android 应用程序的部署位置和可用内容的各种信息。
回答by Dmitry Kolesnikovich
Simply saying, Java context means Java native
methods all together.
简单地说,Java 上下文意味着native
所有的Java方法。
In next Java code two lines of code needs context: // (1)
and // (2)
在接下来的 Java 代码中,两行代码需要上下文:// (1)
和// (2)
import java.io.*;
public class Runner{
public static void main(String[] args) throws IOException { // (1)
File file = new File("D:/text.txt");
String text = "";
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null){ // (2)
text += line;
}
System.out.println(text);
}
}
(1) needs context because is invoked by Java native methodprivate native void java.lang.Thread.start0();
(1) 需要上下文,因为它是由 Java 本地方法调用的private native void java.lang.Thread.start0();
(2) reader.readLine()
needs context because invokes Java native methodpublic static native void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
(2)reader.readLine()
需要上下文,因为调用 Java 本地方法public static native void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
PS.
附注。
That is what BalusC is sayed about pattern Facade more strictly.
这就是 BalusC 对模式 Facade 的更严格的说法。