john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

Spring源码分析之ClassPathXmlApplicationContext

发表于 2020-03-20

我们在分析Spring源码时,通常会自己创建一个独立的应用上下文,比如本文说的ClassPathXmlApplicationContext。我们在实际开发过程中一般框架内部会初始化一个应用程序上下文,比如springmvc中的XmlWebApplicationContext还有springboot中的AnnotationConfigServletWebServerApplicationContext。

ClassPathXmlApplicationContext 简介

官方文档是这么说的:

Standalone XML application context, taking the context definition files from the class path,
interpreting plain paths as class path resource names that include the package path
(e.g. “mypackage/myresource.txt”). Useful for test harnesses(测试框架) as well as
for application contexts embedded within JARs.

也就是说它可以创建独立应用程序上下文,获取上下文的定义文件等等。它的主要功能如下:

  1. 定义了很多构造函数,只是参数不同,不同的参数具体会影响配置资源的定义和spring应用上下文的刷新(refresh函数)。

  2. 配置资源路径的不同,获取方式的不同。如果设置configLocations参数,那么会通过该类的父类也就是抽象类AbstractRefreshableConfigApplicationContext的resolvePath函数来解析。如果使用带有class参数的构造函数,那么会使用ClassPathResource来获取资源。

AbstractEnvironment

resolvePath函数内部其实是调用AbstractEnvironment的resolveRequiredPlaceholders方法,方法内部会调用propertyResolver它的父类AbstractPropertyResolver的resolveRequiredPlaceholders方法。

1
2
3
4
5
6
7
8

private final ConfigurablePropertyResolver propertyResolver =
new PropertySourcesPropertyResolver(this.propertySources);

@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
return this.propertyResolver.resolveRequiredPlaceholders(text);
}

参考资料:

Spring 中 ClassPathXmlApplicationContext 类的简单使用
彻底搞懂Class.getResource和ClassLoader.getResource的区别和底层原理
SpringMVC加载流程

spring源码分析之@Import注解

发表于 2019-07-06

@Import定义

Indicates one or more @Configuration classes to import.

表示一个或多个类会被导入,参考@Configuration.

Provides functionality equivalent to the element in Spring XML.

提供在xml中标签相同的功能.

Allows for importing @Configuration classes, ImportSelector and ImportBeanDefinitionRegistrar implementations,as well as regular component classes (as of 4.2; analogous to AnnotationConfigApplicationContext#register).

允许导入@Configuration类,ImportSelector和ImportBeanDefinitionRegistrar的实现类,等同于正常的组件类(4.2开始;类似于AnnotationConfigApplicationContext.register).

不过我从spring文档中看到AnnotationConfigRegistry接口是从4.1版本开始的

@Bean definitions declared in imported @Configuration classes should be accessed by using org.springframework.beans.factory.annotation.Autowired @Autowired injection. Either the bean itself can be autowired, or the configuration class instance declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly navigation between @Configuration class methods.

在导入类里定义的Bean应该可以被Autowired注入访问。bean自身或者定义bean的配置类都能被autowired。后者能在类方法之间被显式的导航. 这个我们在之后使用示例中会展示。

May be declared at the class level or as a meta-annotation.

在class层级定义或者当做一个meta-annotation。

If XML or other non-@Configuration bean definition resources need to be imported, use the @ImportResource annotation instead.

如果xml或者其他没有@Configuration的bean资源需要被导入,那就使用@ImportResource注解来替代。

参考资料:

  1. Spring Import 三种用法与源码解读
  2. Spring @Import注解 —— 导入资源
  3. @Autowired用法详解

spring框架知识点收获整理

发表于 2019-06-26 | 更新于 2019-07-06

static 静态代码块

org.springframework.util.ClassUtils 抽象类里包含了static静态代码初始化块。

1
2
3
4
5
6
7
8
9
10
static {
primitiveWrapperTypeMap.put(Boolean.class, boolean.class);
primitiveWrapperTypeMap.put(Byte.class, byte.class);
primitiveWrapperTypeMap.put(Character.class, char.class);
primitiveWrapperTypeMap.put(Double.class, double.class);
primitiveWrapperTypeMap.put(Float.class, float.class);
primitiveWrapperTypeMap.put(Integer.class, int.class);
primitiveWrapperTypeMap.put(Long.class, long.class);
primitiveWrapperTypeMap.put(Short.class, short.class);
}

Java编程思想p96页5.7.3节提到了java的显式静态初始化。

[Ljava/lang/String; 的含义

“([Ljava/lang/String;)V” 它是一种对函数返回值和参数的编码。这种编码叫做JNI字段描述符(JavaNative Interface FieldDescriptors)。一个数组int[],就需要表示为这样”[I”。如果多个数组double[][][]就需要表示为这样 “[[[D”。也就是说每一个方括号开始,就表示一个数组维数。多个方框后面,就是数组 的类型。
如果以一个L开头的描述符,就是类描述符,它后紧跟着类的字符串,然后分号“;”结束。

比如”Ljava/lang/String;”就是表示类型String;

“[I”就是表示int[];

“[Ljava/lang/Object;”就是表示Object[]。

JNI方法描述符,主要就是在括号里放置参数,在括号后面放置返回类型,如下:

(参数描述符)返回类型

当一个函数不需要返回参数类型时,就使用”V”来表示。

比如”()Ljava/lang/String;”就是表示String f();

“(ILjava/lang/Class;)J”就是表示long f(int i, Class c);

“([B)V”就是表示void String(byte[] bytes);

Java类型 符号
Boolean Z,
Byte B,
Char C,
Short S,
Int I,
Long J,
Float F,
Double D,
Void V;
objects对象以”L”开头,以”;”结尾,中间是用”/“ 隔开的包及类名。比如:Ljava/lang/String;如果是嵌套类,则用$来表示嵌套。例如 “(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z”

另外数组类型的简写,则用”[“加上如表A所示的对应类型的简写形式进行表示就可以了,

比如:[I 表示 int [];[L/java/lang/objects;表示Objects[],另外。引用类型(除基本类型的数组外)的标示最后都有个”;”

例如:

“()V” 就表示void Func();

“(II)V” 表示 void Func(int, int);

“(Ljava/lang/String;Ljava/lang/String;)I”.表示 int Func(String,String)

Array.newInstance

动态创建数组

  1. java.lang.reflect.Array.newInstance与list.toArray

ClassLoader.loadClass

  1. 类加载器URLClassLoader.loadClass(className)后, 无法卸载Jar
  2. 深入浅出ClassLoader
  3. Java类加载原理与ClassLoader使用总结

spring aop学习资料整理

发表于 2019-06-26
  1. 彻底征服 Spring AOP 之 理论篇
  2. Spring(4)——面向切面编程(AOP模块)
  3. Spring系列之AOP实现的两种方式
  4. Unable to locate Spring NamespaceHandler for XML schema产生的原因及解决方法
  5. Dubbo 源码分析
  6. Spring AOP 源码分析 - 拦截器链的执行过程
  7. Spring IOC和AOP 原理彻底搞懂

spring5.1.1源码剖析之BeanFactory接口

发表于 2019-06-26

关于BeanFactory接口

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory。

org.springframework.beans 和 org.springframework.context 两个包是Spring框架IoC
容器的基础。BeanFactory接口为任何类型提供了高级配置能力。ApplicationContext是BeanFactory的子接口,它提供了以下功能:

  1. Easier integration with Spring’s AOP features
    (与Spring AOP特性的简单集成)

  2. Message resource handling (for use in internationalization) (处理Message resource,用于国际化)

  3. Event publication (事件发布)

  4. Application-layer specific contexts such as the WebApplicationContext for use in web applications. (应用层指定上下文,比如Web应用的WebApplicationContext)

为啥这么说呢?我们来看AppplicationContext接口:

1
2
3
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

}
  1. ApplicationEventPublisher跟Event publication相关
  2. MessageSource跟Message resource handling相关

In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory and is used exclusively in this chapter in descriptions of Spring’s IoC container.

大致意思如下:

简单的说,BeanFactory提供配置框架和基础的函数,子接口ApplicationContext加入了针对商业特定的方法。
ApplicationContext是BeanFactory完整的超集,在这个Spring IoC容器章节描述中会专门使用。

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

在Spring世界里,构成应用程序主干和被IoC容器管理的对象称之为beans。一个bean要么实例化,组装,要么被容器管理。除此以外,一个bean就是你应用里许多对象中的一个。
Beans和他们的依赖是在容器使用的配置元数据里反射的。

参考资料:

  1. Introduction to the Spring IoC Container and Beans
1…789…47

John

232 日志
43 标签
GitHub Twitter
欢迎关注我的公众号:沉迷Spring
© 2023 johnwonder
由 Hexo 强力驱动 v3.2.0
|
主题 – NexT.Pisces v7.1.1
|
0%