设计模式(三十一)----综合应用-自定义Spring框架-自定义Spring IOC-定义解析器、IOC容器相关类
3 定义解析器相关类
3.1 BeanDefinitionReader接口
BeanDefinitionReader是用来解析配置文件并在注册表中注册bean的信息。定义了两个规范:
获取注册表的功能,让外界可以通过该对象获取注册表对象。
加载配置文件,并注册bean数据。
/**
* @version v1.0
* @ClassName: BeanDefinitionReader
* @Description:
* 用来解析配置文件的,而该接口只是定义了规范
*/
public interface BeanDefinitionReader {
//获取注册表对象
BeanDefinitionRegistry getRegistry();
//加载配置文件并在注册表中进行注册
void loadBeanDefinitions(String configLocation) throws Exception;
}
3.2 XmlBeanDefinitionReader类
XmlBeanDefinitionReader类是专门用来解析xml配置文件的。该类实现BeanDefinitionReader接口并实现接口中的两个功能。
/**
* @version v1.0
* @ClassName: XmlBeanDefinitionReader
* @Description: 针对xml配置文件进行解析的类
*/
public class XmlBeanDefinitionReader implements BeanDefinitionReader {
//声明注册表对象
private BeanDefinitionRegistry registry;
public XmlBeanDefinitionReader() {
this.registry = new SimpleBeanDefinitionRegistry();
}
@Override
public BeanDefinitionRegistry getRegistry() {
return registry;
}
public void loadBeanDefinitions(String configLocation) throws Exception {
//使用dom4j进行xml配置文件的解析 须需在pom文件里面引入dom4j 1.6.1版本
SAXReader reader = new SAXReader();
//获取类路径下的配置文件
InputStream is = XmlBeanDefinitionReader.class.getClassLoader().getResourceAsStream(configLocation);
Document document = reader.read(is);
//根据Document对象获取根标签对象 (beans)
Element rootElement = document.getRootElement();
//获取根标签下所有的bean标签对象
List<Element> beanElements = rootElement.elements("bean");
//遍历集合
for (Element beanElement : beanElements) {
//获取id属性
String id = beanElement.attributeValue("id");
//获取class属性
String className = beanElement.attributeValue("class");
//将id属性和class属性封装到BeanDefinition对象中
//1,创建BeanDefinition
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setId(id);
beanDefinition.setClassName(className);
//创建MutablePropertyValues对象
MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
//获取bean标签下所有的property标签对象
List<Element> propertyElements = beanElement.elements("property");
for (Element propertyElement : propertyElements) {
String name = propertyElement.attributeValue("name");
String ref = propertyElement.attributeValue("ref");
String value = propertyElement.attributeValue("value");
PropertyValue propertyValue = new PropertyValue(name,ref,value);
mutablePropertyValues.addPropertyValue(propertyValue);
}
//将mutablePropertyValues对象封装到BeanDefinition对象中
beanDefinition.setPropertyValues(mutablePropertyValues);
//将beanDefinition对象注册到注册表中
registry.registerBeanDefinition(id,beanDefinition);
}
}
}
4 IOC容器相关类
4.1 BeanFactory接口
在该接口中定义IOC容器的统一规范即获取bean对象。
public interface BeanFactory {
//根据bean对象的名称获取bean对象
Object getBean(String name) throws Exception;
//根据bean对象的名称获取bean对象,并进行类型转换
<T> T getBean(String name, Class<? extends T> clazz) throws Exception;
}
4.2 ApplicationContext接口
该接口的所以的子实现类对bean对象的创建都是非延时的,所以在该接口中定义 refresh() 方法,该方法主要完成以下两个功能:
加载配置文件。
根据注册表中的BeanDefinition对象封装的数据进行bean对象的创建。
//定义非延时加载功能
public interface ApplicationContext extends BeanFactory {
//进行配置文件加载并进行对象创建
void refresh() throws IllegalStateException, Exception;
}
4.3 AbstractApplicationContext类
作为ApplicationContext接口的子类,所以该类也是非延时加载,所以需要在该类中定义一个Map集合,作为bean对象存储的容器。
声明BeanDefinitionReader类型的变量,用来进行xml配置文件的解析,符合单一职责原则。
BeanDefinitionReader类型的对象创建交由子类实现,因为只有子类明确到底创建BeanDefinitionReader哪儿个子实现类对象。
public abstract class AbstractApplicationContext implements ApplicationContext {
protected BeanDefinitionReader beanDefinitionReader;
//用来存储bean对象的容器 key存储的是bean的id值,value存储的是bean对象
protected Map<String, Object> singletonObjects = new HashMap<String, Object>();
//存储配置文件的路径
protected String configLocation;
public void refresh() throws IllegalStateException, Exception {
//加载BeanDefinition
beanDefinitionReader.loadBeanDefinitions(configLocation);
//初始化bean
finishBeanInitialization();
}
//bean的初始化
private void finishBeanInitialization() throws Exception {
//获取注册表对象
BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
//获取BeanDefinition对象
String[] beanNames = registry.getBeanDefinitionNames();
for (String beanName : beanNames) {
//进行bean的初始化
getBean(beanName);
}
}
}
注意:该类finishBeanInitialization()方法中调用getBean()方法使用到了模板方法模式。
4.4 ClassPathXmlApplicationContext类
该类主要是加载类路径下的配置文件,并进行bean对象的创建,主要完成以下功能:
在构造方法中,创建BeanDefinitionReader对象。
在构造方法中,调用refresh()方法,用于进行配置文件加载、创建bean对象并存储到容器中。
重写父接口中的getBean()方法,并实现依赖注入操作。
/**
* @version v1.0
* @ClassName: ClassPathXmlApplicationContext
* @Description: IOC容器具体的子实现类
* 用于加载类路径下的xml格式的配置文件
*/
public class ClassPathXmlApplicationContext extends AbstractApplicationContext {
public ClassPathXmlApplicationContext(String configLocation) {
this.configLocation = configLocation;
//构建解析器对象
beanDefinitionReader = new XmlBeanDefinitionReader();
try{
this.refresh();
} catch (Exception e) {
}
}
//根据bean对象的名称获取bean对象
public Object getBean(String name) throws Exception {
//判断对象容器中是否包含指定名称的bean对象,如果包含,直接返回即可,如果不包含,需要自行创建
Object obj = singletonObjects.get(name);
if (obj != null) {
return obj;
}
//获取BeanDefinition对象
BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
BeanDefinition beanDefinition = registry.getBeanDefinition(name);
//获取bean信息中的className
String className = beanDefinition.getClassName();
//通过反射创建对象
Class<?> clazz = Class.forName(className);
Object beanObj = clazz.newInstance();
//进行依赖注入操作
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues) {
//获取name属性值
String propertyName = propertyValue.getName();
//获取value属性
String value = propertyValue.getValue();
//获取ref属性
String ref = propertyValue.getRef();
if(ref != null && !"".equals(ref)) {
//获取依赖的bean对象
Object bean = getBean(ref);
//拼接方法名
String methodName = StringUtils.getSetterMethodByFieldName(propertyName);
//获取所有的方法对象
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
//执行该setter方法
method.invoke(beanObj,bean);
}
}
}
if(value != null && !"".equals(value)) {
//拼接方法名
String methodName = StringUtils.getSetterMethodByFieldName(propertyName);
//获取method对象
Method method = clazz.getMethod(methodName, String.class);
method.invoke(beanObj,value);
}
}
//在返回beanObj对象之前,将该对象存储到map容器中
singletonObjects.put(name,beanObj);
return beanObj;
}
public <T> T getBean(String name, Class<? extends T> clazz) throws Exception {
Object bean = getBean(name);
if(bean == null) {
return null;
}
return clazz.cast(bean);
}
}
/**
* @version v1.0
* @ClassName: StringUtils
*/
public class StringUtils {
private StringUtils() {
}
// userDao ==> setUserDao
public static String getSetterMethodByFieldName(String fieldName) {
String methodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
return methodName;
}
}
如此已经完成,只需对这个工程执行mvn install打包成一个jar,通过pom文件引入到其他工程(例如:spring使用回顾)中即可进行测试。
// 这是另外一个工程
// 引用的类均来自于前面自定义的类
public static void main(String[] args) throws Exception {
//1,创建spring的容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//2,从容器对象中获取userService对象
UserService userService = applicationContext.getBean("userService", UserService.class);
//3,调用userService方法进行业务逻辑处理
userService.add();
}
设计模式(三十一)----综合应用-自定义Spring框架-自定义Spring IOC-定义解析器、IOC容器相关类的更多相关文章
- 《经久不衰的Spring框架:Spring+SpringMVC+MyBatis 整合》
前言 主角即Spring.SpringMVC.MyBatis,即所谓的SSM框架,大家应该也都有所了解,概念性的东西就不写了,有万能的百度.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目 ...
- 什么是Spring框架? Spring框架有哪些主要的模块?
Spring框架是一个为java应用程序的开发提供了综合,广泛的基础性支持的java平台.Spring帮助开发者解决了开发中基础性的问题,使得开发人员可以专注于应用程序的开发.Spring框架本身亦是 ...
- Spring框架(1)---Spring入门
Spring入门 为了能更好的理解先讲一些有的没的的东西: 什么是Spring Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架 分层 SUN提供的EE的三层结构 ...
- 【spring框架】spring获取webapplicationcontext,applicationcontext几种方法详解--(转)
方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXmlApplicationContext(" ...
- Spring框架学习-Spring和IOC概述
一:什么是Spring框架? spring是一个分层的javase/EEfull-stack(一站式)轻量级的java开源框架.是为了解决企业开发的复杂性而创建的.框架的主要优势是分层架构,Sprin ...
- Spring框架:Spring安全
在传统的Web发展,安全码被分散在各个模块,这样方便管理,有时你可能会错过一个地方导致安全漏洞.为了解决这个问题,它的发明Spring Security.它是业务逻辑的有关安全代码的作用全部转移到一个 ...
- 什么是spring框架?spring特点与好处,使用spring框架的好处是什么?
转载:https://blog.csdn.net/hht006158/article/details/80181207. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Ja ...
- 一句话概括下spring框架及spring cloud框架主要组件
作为java的屌丝,基本上跟上spring屌丝的步伐,也就跟上了主流技术.spring 顶级项目:Spring IO platform:用于系统部署,是可集成的,构建现代化应用的版本平台,具体来说当你 ...
- Spring(二):Spring框架&Hello Spring
Spring是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为J2EE应用程序开发提供集成的框架. Spring 框架 ...
- Spring框架学习——Spring的体系结构详解
1.Spring简介 Spring是一个轻量级Java开发框架,最早有Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题.它是一个分层的JavaSE/JavaEE ...
随机推荐
- 分布式-Etcd介绍
Etcd客户端基本操作 提供了如下操作接口: put - 添加一个新的 key-value 到存储中 get - 获取一个 key 的 value delete - 删除节点 range - 获取一个 ...
- HTTP请求向服务器传参方式
请求HttpRequest 提示: 用户发送请求时携带的参数后端需要使用,而不同的发送参数的方式对应了不同的提取参数的方式 所以要学会如何提取参数,我们就需要先了解前端传参数有哪些方式 回想一下,利用 ...
- 2、Java程序设计环境
1.JDK Java开发工具箱 在Java 9之前,有32位和64位两个版本的Java开发工具包.现在Oracle公司不在开发32位版本,要使用Oracle JDK,你需要有一个64位的操作系统. 安 ...
- reflection反射
reflection反射 动态和静态语言 动态语言 动态语言就是一类在运行时可以改变其结构的语言,通俗点说就是在运行时代码可以根据某些条件改变自身结构 主要动态语言:object-C,C#,JavaS ...
- SpringBoot系列---【maven项目引入第三方jar包并打包发布】
一.问题 项目中经常会碰到这样的问题,我们做的项目依赖别人打好的jar包,这种我们可以有两种途径解决,第一种是上传到私服,再从我们的项目去引入pom坐标,这种适合有私服账号或者自己会搭建私服的,成本有 ...
- 字节过滤流 --->对象流(存入对象的)----> ObjectOutputStream: 用法
前提:1).要有一个类 并创建这个类的对象2)要让类必须继承Serializable接口3)transient修饰的属性 值不参与序列化1创建字节输出节点流FileOutputStream fos = ...
- 【搭建】【转】搭建 yum仓库
https://blog.csdn.net/wuxingge/article/details/100761637 3.2 服务端部署 1)安装软件程序(createrepo) yum install ...
- java文本转语音
下载jar包https://github.com/freemansoft/jacob-project/releases 解压,将jacob-1.18-xxx.dll相应放到项目使用的JAVA_HOME ...
- List转Map处理
List对象装一个Map<String,String> 在Java8中新增了stream流的操作,对于代码书写更为简便,而且更容易看的懂 List<Unit> unitList ...
- 7.webpack与vue-cli
一.模块化相关规范 1.1 模块化概述 传统开发模式的主要问题 命名冲突:多个JS文件之间,如果存在重名的变量,会发生变量覆盖问题 文件依赖:JS文件无法实现相互的引用 通过模块化解决上述问题 模块化 ...