博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Context Java
阅读量:2207 次
发布时间:2019-05-04

本文共 4439 字,大约阅读时间需要 14 分钟。

Context 在英文词典中的解释:

the circumstances(环境) that form the setting for an event, statement, or idea, and in terms of which it can be fully understood and assessed.

You have misinterpreted(误解) my remark because you took it out of context.

有一点像我们中文说的“语境”

语境可以帮我们更好地表达一些意思,而不用我们多费心思去解释

在编程世界里:

C o n t e x t 会 帮 我 们 实 现 一 些 功 能 , 而 不 需 要 我 们 再 费 心 思 去 解 决 \color{red}{Context 会帮我们实现一些功能,而不需要我们再费心思去解决} Context


例如:

Spring 框架中的ApplicationContext

它其实就是个接口

官方Javadoc是这样描述的(Spring Framework 5.2.9.RELEASE API)

public interface ApplicationContextextends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich interdependencies between those objects.

划重点— represents the Spring IoC container,即负责控制反转,依赖注入的容器()

Inversion of Control (IoC) . IoC is also known as dependency injection (DI).

我们提供POJO和配置metadata,容器会帮我们去创建我们需要的Bean对象

POJO(Plain Ordinary Java Object)简单的Java对象

配置metadata 即Bean的配置信息,我们可以利用注解,xml或Java代码提供Bean的配置信息

An ApplicationContext provides:

1.Bean factory methods for accessing application components. Inherited from ListableBeanFactory.

2.The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
3.The ability to publish events to registered listeners. Inherited from the ApplicationEventPublisher interface.
3.The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
4.Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.

这里大家最熟悉的应该就是这个Bean factory 了

context=new ClassPathXmlApplicationContext("applicationContext.xml");	UserDao t=context.getBean("userDaoImpl",UserDao.class);

在context下,我们想要获取某个Java Bean ,调一下getBean()方法就好了

context已经帮我们实现了其背后的一堆工作

大家肯定会想,我要一个对象,我new出来不一样吗?

确实,你可以自己实现这样的一些功能

就像我前面说的

语境可以帮我们更好地表达一些意思,而不用我们多费心思去解释

你当然可以自己花点功夫去实现一些功能

(import 对应的package然后new 实例化出一个对象)

但是通过context.getBean()方法的话,简单太多了

你只要提供POJO和配置metadata就行了


context 其实是应用了一种设计模式:Facade Pattern(外观模式)

外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

大家可以去学习这个设计模式,可以加深对context的理解


这里补充说明一下前面的的代码

context=new ClassPathXmlApplicationContext("applicationContext.xml");	UserDao t=context.getBean("userDaoImpl",UserDao.class);

ClassPathXmlApplicationContext 其实就是ApplicationContext接口的一个实现

语境是需要构建的

你描述描述事情发生时的情形,语境就构建起来了

context也是需要构建的,更准确地说是配置(configuration)

而ClassPathXmlApplicationContext 则是通过XML文件去配置context

applicationContext.xml:

在上面这个例子中,XML配置了需要scan的包、配置了一个Java Bean

------------补充-------------

(XML文件上面的那些xmlns指的是xml namespace,声明了后面.xsd文件的namespace

xmlns:context="http://www.springframework.org/schema/context"

即声明了下面用的<context:component-scan> 标签是在Spring namespace下的context

那这些这xsd(XML Schema Definition)又有什么用呢?

先看看什么是XML Schema

XML schemais a language which is used for expressing constraint about XML documents.

用来解释xml文件

xsd会给出一些标准来解释xml文件

就拿上面的applicationContext.xml 举一个很简单的例子:

声明中有一个

http://www.springframework.org/schema/beans/spring-beans.xsd

可以在浏览器中查看这个文件

spring-beans.xsd(下面只给出了部分代码)

解释一下

这个标准则表示:

我们可以在xml文件里使用这个

然后在<xsd:element name=“bean”> </xsd:element>标签之间

有一个这个

显而易见

其规定了一系列的Attributes

例如name, class, scope等一些我们可以在bean标签上设置的属性

除了xsd 另一个比较常见的就是DTD (Document Type Definition)了

也是一个XML Schema,作解释用

----------补充结束----------

而scan这个概念,就得说到CDI了(Contexts and Dependency Injection)

依赖和注入

scan 是用于基于注解annotaion 的依赖和注入

具体的我这里也不进行展开啦~


总结:

Context 就是默默帮我们处理复杂事物的接口

转载地址:http://wliyb.baihongyu.com/

你可能感兴趣的文章
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>
国内外helm源记录
查看>>
牛客网题目1:最大数
查看>>
散落人间知识点记录one
查看>>
Leetcode C++ 随手刷 547.朋友圈
查看>>
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>