免费网站域名查询,net源码的网站建设步骤,高速公路建设论坛网站,表格比较多得网站这么做响应式简单手写SpringIOC框架 环境搭建基于XML方式项目结构项目代码运行结果 基于注解方式项目结构项目代码运行结果 简单手写SpringIOC框架核心原理基于XML方式原理项目结构项目代码运行结果 基于注解方式原理项目结构项目代码运行结果 环境搭建 
基于XML方式 
项目结构 项目代码 
p… 简单手写SpringIOC框架 环境搭建基于XML方式项目结构项目代码运行结果 基于注解方式项目结构项目代码运行结果  简单手写SpringIOC框架核心原理基于XML方式原理项目结构项目代码运行结果 基于注解方式原理项目结构项目代码运行结果    环境搭建 
基于XML方式 
项目结构 项目代码 
pom.xml 
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom/groupIdartifactIdspring/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion5.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion5.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.1.RELEASE/version/dependency/dependencies/projectUserBean.java 
package com.spring.bean;/*** author honey* date 2023-08-08 14:58:16*/
public class UserBean {
}spring.xml 
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iduserBean classcom.spring.bean.UserBean//beansSpringTest01.java 
package com.spring.test;import com.spring.bean.UserBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** author honey* date 2023-08-08 14:59:56*/
public class SpringTest01 {public static void main(String[] args) {// 读取spring.xmlClassPathXmlApplicationContext applicationContext  new ClassPathXmlApplicationContext(spring.xml);// 从IOC容器中读取对象UserBean userBean  applicationContext.getBean(userBean, UserBean.class);System.out.println(userBean);}
}运行结果 基于注解方式 
项目结构 项目代码 
ScanBean.java 
package com.spring.bean.scan;import org.springframework.stereotype.Component;/*** author honey* date 2023-08-08 16:37:26*/
Component
public class ScanBean {
}SpringConfig.java 
package com.spring.config;import com.spring.bean.UserBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;/*** author honey* date 2023-08-08 16:30:21*/
Configuration
ComponentScan(value  {com.spring.bean.scan})
public class SpringConfig {Bean(name  user)public UserBean userBean() {return new UserBean();}
}SpringTest02.java 
package com.spring.test;import com.spring.bean.UserBean;
import com.spring.bean.scan.ScanBean;
import com.spring.config.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** author honey* date 2023-08-08 16:31:25*/
public class SpringTest02 {public static void main(String[] args) {// 加载SpringConfigAnnotationConfigApplicationContext applicationContext  new AnnotationConfigApplicationContext(SpringConfig.class);// 从IOC容器中读取对象UserBean userBean  applicationContext.getBean(user, UserBean.class);System.out.println(userBean);ScanBean scanBean  applicationContext.getBean(scanBean, ScanBean.class);System.out.println(scanBean);}
}运行结果 简单手写SpringIOC框架 
核心原理 
底层使用map集合管理对象keybeanIdvalue实例对象 
private final MapString, Object beanMap  new ConcurrentHashMap();基于XML方式 
原理 
基于反射工厂模式DOM技术 
使用DOM技术解析spring.xml文件获取bean的id和class属性根据类的完整路径使用反射技术初始化对象使用工厂模式管理初始化对象 
项目结构 项目代码 
pom.xml 
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom/groupIdartifactIdext-spring-ioc/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion5.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion5.2.1.RELEASE/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.1.RELEASE/version/dependencydependencygroupIddom4j/groupIdartifactIddom4j/artifactIdversion1.6.1/version/dependency/dependencies/projectUserBean.java 
package com.spring.bean;/*** author honey* date 2023-08-08 16:56:32*/
public class UserBean {
}spring.xml 
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iduserBean classcom.spring.bean.UserBean//beansSpringIocXml.java 
package com.spring.ext;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;/*** author honey* date 2023-08-08 16:57:17*/
public class SpringIocXml {private final MapString, Object beanMap  new ConcurrentHashMap();public SpringIocXml() throws IOException, DocumentException {init();}public T T getBean(String name) {return (T) beanMap.get(name);}/*** 初始化IOC容器*/private void init() throws IOException, DocumentException {// 解析spring.xml配置ClassPathResource classPathResource  new ClassPathResource(spring.xml);File xmlFile  classPathResource.getFile();SAXReader saxReader  new SAXReader();Document doc  saxReader.read(xmlFile);// 获取根节点Element rootElement  doc.getRootElement();// 获取bean节点信息ListElement beans  rootElement.elements(bean);for (Element bean : beans) {try {String beanId  bean.attribute(id).getValue();String classPath  bean.attribute(class).getValue();// 使用反射机制初始化对象并将对象存入Map集合Class? clazz  Class.forName(classPath);Object object  clazz.newInstance();beanMap.put(beanId, object);} catch (Exception e) {e.printStackTrace();}}}}SpringTest01.java 
package com.spring.test;import com.spring.bean.UserBean;
import com.spring.ext.SpringIocXml;
import org.dom4j.DocumentException;import java.io.IOException;/*** author honey* date 2023-08-08 17:04:35*/
public class SpringTest01 {public static void main(String[] args) throws DocumentException, IOException {SpringIocXml springIocXml  new SpringIocXml();UserBean userBean  springIocXml.getBean(userBean);System.out.println(userBean);}
}运行结果 基于注解方式 
原理 
基于反射工厂模式实现 
判断配置类上是否有Configuration注解获取配置类中的所有方法判断方法上是否有Bean注解如果有则获取方法的返回值作为实例对象判断配置类上是否有ComponentScan注解如果有则扫描指定包下的所有类并判断类上是否有Component注解如果有则通过反射技术初始化对象使用工厂模式管理初始化对象/实例对象 
项目结构 项目代码 
pom.xml 
dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.7.7/version
/dependencyScanBean.java 
package com.spring.bean.scan;import org.springframework.stereotype.Component;/*** author honey* date 2023-08-09 14:28:33*/
Component
public class ScanBean {
}SpringConfig.java 
package com.spring.config;import com.spring.bean.UserBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;/*** author honey* date 2023-08-09 14:26:40*/
Configuration
ComponentScan(value  {com.spring.bean.scan})
public class SpringConfig {Beanpublic UserBean userBean() {return new UserBean();}
}SpringIocAnnotation.java 
package com.spring.ext;import cn.hutool.core.lang.ClassScanner;
import cn.hutool.core.util.StrUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;/*** author honey* date 2023-08-09 14:12:21*/
public class SpringIocAnnotation {private final Object config;private final MapString, Object beanMap  new ConcurrentHashMap();public SpringIocAnnotation(Object config) {this.config  config;init();}/*** 初始化IOC容器*/public void init() {// 判断配置类上是否有Configuration注解Configuration configuration  this.config.getClass().getDeclaredAnnotation(Configuration.class);if (configuration  null) {return;}// 处理Bean注解Class? clazz  config.getClass();Method[] declaredMethods  clazz.getDeclaredMethods();for (Method method : declaredMethods) {// 判断方法上是否有Bean注解Bean bean  method.getDeclaredAnnotation(Bean.class);if (bean ! null) {try {// 获取beanIdString[] value  bean.value();String beanId  value.length  0 ? value[0] : method.getName();// 获取方法的返回值Object object  method.invoke(config);beanMap.put(beanId, object);} catch (Exception e) {e.printStackTrace();}}}// 处理Component注解ComponentScan componentScan  clazz.getDeclaredAnnotation(ComponentScan.class);if (componentScan ! null) {for (String packageName : componentScan.value()) {try {// 扫描指定包下的所有类SetClass? classes  ClassScanner.scanPackage(packageName);for (Class? c : classes) {// 判断类上是否有Component注解Annotation component  c.getDeclaredAnnotation(Component.class);if (component ! null) {try {// 获取beanIdString beanId  StrUtil.lowerFirst(c.getSimpleName());// 通过反射技术初始化对象Object beanObject  c.newInstance();beanMap.put(beanId, beanObject);} catch (Exception e) {e.printStackTrace();}}}} catch (Exception e) {e.printStackTrace();}}}}public T T getBean(String name) {return (T) beanMap.get(name);}
}SpringTest02.java 
package com.spring.test;import com.spring.bean.UserBean;
import com.spring.bean.scan.ScanBean;
import com.spring.config.SpringConfig;
import com.spring.ext.SpringIocAnnotation;/*** author honey* date 2023-08-09 14:24:36*/
public class SpringTest02 {public static void main(String[] args) {SpringIocAnnotation springIocAnnotation  new SpringIocAnnotation(new SpringConfig());UserBean userBean  springIocAnnotation.getBean(userBean);System.out.println(userBean);ScanBean scanBean  springIocAnnotation.getBean(scanBean);System.out.println(scanBean);}
}运行结果 
 文章转载自: http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.lqrpk.cn.gov.cn.lqrpk.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.svtxeu.com.gov.cn.svtxeu.com http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.dfojgo.cn.gov.cn.dfojgo.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.ntffl.cn.gov.cn.ntffl.cn