Context Java

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 ApplicationContext
extends 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 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:component-scan base-package="com.dgut.annotation"></context:component-scan>

<bean id="helloImpl" class="com.dgut.spring.Helloimpl" ></bean>

</beans>

在上面这个例子中,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(下面只给出了部分代码)

<xsd:element name="bean">
<xsd:annotation>
<xsd:documentation source="java:org.springframework.beans.factory.config.BeanDefinition">
<![CDATA[ Defines a single (usually named) bean. A bean definition may contain nested tags for constructor arguments, property values, lookup methods, and replaced methods. Mixing constructor injection and setter injection on the same bean is explicitly supported. ]]>
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="identifiedType">
<xsd:group ref="beanElements"/>
<xsd:attributeGroup ref="beanAttributes"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

解释一下

<xsd:element name="bean">

这个标准则表示:

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

<bean></bean>

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

有一个这个

<xsd:attributeGroup ref="beanAttributes"/>

显而易见

其规定了一系列的Attributes

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

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

也是一个XML Schema,作解释用

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

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

依赖和注入

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

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


总结:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值