当前位置: 首页 > wzjs >正文

招聘网站开发计划书百度知道官网手机版

招聘网站开发计划书,百度知道官网手机版,贵阳营销型网站建设,企业手机网站案例Spring系列五:手动实现Spring底层机制 🍝类加载器和classpath详解 💗实现任务阶段1🍚编写自己Spring容器, 扫描包得到bean的class对象 💗实现任务阶段2🍚扫描将bean信息封装到BeanDefinition对象, 并放入到…

Spring系列五:手动实现Spring底层机制

    • 🍝类加载器和classpath详解
  • 💗实现任务阶段1
    • 🍚编写自己Spring容器, 扫描包得到bean的class对象
  • 💗实现任务阶段2
    • 🍚扫描将bean信息封装到BeanDefinition对象, 并放入到Map

上文中, 我们学习到了 Spring系列四:AOP切面编程

接下来我们学习, 手动实现Spring底层机制
在这里插入图片描述

语法学习

  1. 类名首字母小写
    import org.apache.commons.lang.StringUtils;
    StringUtils.uncapitalize(className);
  2. 判断类对象(clazz)是否实现某个接口
    接口.class.isAssignableFrom(clazz)

🍝类加载器和classpath详解

● java的类加载器 3 种

  1. Bootstrap类加载器---------------------对应路径jre\lib
  2. Ext类加载器------------------------------对应路径jre\lib\ext
  3. App类加载器-----------------------------对应路径classpath

●classpath 类路径, 就是 java.exe 执行时, 指定的路径, 比如

在这里插入图片描述

复制, 粘贴到txt文档中,,如下


"D:\Program Files\Java\jdk1.8.0_361\bin\java.exe " -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:55339,suspend=y,server=n -javaagent:C:\Users\97896\AppData\Local\JetBrains\IntelliJIdea2022.3\captureAgent\debugger-agent.jar -Dfile.encoding=GBK -classpath “D:\Program Files\Java\jdk1.8.0_361\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_361\jre\lib\rt.jar;D:\idea_project\zzw_spring\zzw-spring\target\classes;D:\maven\repository\org\springframework\spring-context\5.3.8\spring-context-5.3.8.jar;D:\maven\repository\org\springframework\spring-aop\5.3.8\spring-aop-5.3.8.jar;D:\maven\repository\org\springframework\spring-beans\5.3.8\spring-beans-5.3.8.jar;D:\maven\repository\org\springframework\spring-core\5.3.8\spring-core-5.3.8.jar;D:\maven\repository\org\springframework\spring-jcl\5.3.8\spring-jcl-5.3.8.jar;D:\maven\repository\org\springframework\spring-expression\5.3.8\spring-expression-5.3.8.jar;D:\maven\repository\org\springframework\spring-aspects\5.3.8\spring-aspects-5.3.8.jar;D:\maven\repository\org\aspectj\aspectjweaver\1.9.6\aspectjweaver-1.9.6.jar;D:\Program Files\IntelliJ IDEA 2022.3.2\lib\idea_rt.jar” com.zzw.spring.AppMain

💗实现任务阶段1

🍚编写自己Spring容器, 扫描包得到bean的class对象

编写自己Spring容器, 扫描包得到bean的class对象
在这里插入图片描述


第一阶段的代码参考👉Spring系列三:基于注解配置bean

1.在zzw-spring项目创建子模块(java maven module) zzw-myspring项目
在这里插入图片描述
在这里插入图片描述

或者

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.在com.zzw.spring.annotation包下新建注解ComponentScan

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentScan {//通过value可以指定要扫描的包String value() default "";
}

2.在com.zzw.spring.annotation包下新建注解 Component

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {//通过value可以给注入的bean/对象指定名字String value() default "";
}

3.在com.zzw.spring.ioc包下新建 ZzwSpringConfig

//这是一个配置类, 作用类似于我们原生spring的 beans.xml 容器配置文件
@ComponentScan(value = "com.zzw.spring.component")
public class ZzwSpringConfig {
}

4.在com.zzw.spring.component包下新建 MonsterService

//MonsterService 是一个Service
//1.如果指定了value, 那么在注入spring容器时, 以我们指定的为准
//2.如果没有指定value, 则使用类名首字母小写的方式命名
@Component(value = "monsterService")// 把MonsterService注入我们自己写的spring容器中
public class MonsterService {}

5.在com.zzw.spring.component包下新建MonsterDao

@Component(value = "monsterDao")
public class MonsterDao {
}

6.在com.zzw.spring.ioc包下新建 ZzwSpringApplicationContext

//模拟spring-ioc容器
//ZzwSpringApplicationContext 类的作用类似于Spring原生ioc容器
public class ZzwSpringApplicationContext {private Class configClass;//构造器public ZzwSpringApplicationContext(Class configClass) {//获取要扫描的包//1.先得到ZzwSpringConfig配置类的 @ComponentScan(value = "com.zzw.spring.component")ComponentScan componentScan =(ComponentScan) this.configClass.getDeclaredAnnotation(ComponentScan.class);//2.通过componentScan的value => 即要扫描的包String path = componentScan.value();System.out.println("要扫描的包=" + path);//com.zzw.spring.component//得到要扫描的包下的所有资源(.class 类)//1.得到类的加载器->APP 类加载器ClassLoader classLoader = ZzwSpringApplicationContext.class.getClassLoader();//2.通过类的加载器获取到要扫描包的资源url =>类似一个路径path = path.replace(".", "/");//一定要把 .替换成 / com/zzw/spring/componentURL resource = classLoader.getResource(path);System.out.println("resource=" + resource);//resource=file:/D:/idea_project/zzw_spring/zzw-myspring/target/classes/com/zzw/spring/component//3.将要加载的资源(.class) 路径下的文件进行遍历File file = new File(resource.getFile());//在io中, 目录也是文件if (file.isDirectory()) {File[] files = file.listFiles();for (File f : files) {//System.out.println(f.getAbsolutePath());//D:\idea_project\zzw_spring\zzw-myspring\target\classes\com\zzw\spring\component\MonsterDao.classString fileAbsolutePath = f.getAbsolutePath();//这里我们只处理.class文件if (fileAbsolutePath.endsWith(".class")) {//1.获取类名String className =fileAbsolutePath.substring(fileAbsolutePath.lastIndexOf("\\") + 1, fileAbsolutePath.lastIndexOf(".class"));//2.获取类的完整的路径(全类名)// path.replace("/", ".") => com.zzw.spring.componentString classFullName = path.replace("/", ".") + "." + className;//比如 com.zzw.spring.component.UserDao//3.判断该类是不是需要注入到容器, 就看该类是不是有注解 @Component @Controller...try {Class<?> clazz = classLoader.loadClass(classFullName);//这里就是演示了一下机制if (clazz.isAnnotationPresent(Component.class)) {//如果该类使用了@Component注解, 说明是Spring beanSystem.out.println("是一个Spring bean=" + clazz + " 类名=" + className);} else {//如果该类没有使用@Component注解, 说明不是Spring beanSystem.out.println("不是一个Spring bean=" + clazz + " 类名=" + className);}} catch (Exception e) {throw new RuntimeException(e);}}}System.out.println("=====================================================================================");}}//编写方法返回容器对象public Object getBean(String name) {return null;}
}

7.在 com.zzw.spring 包下新建 AppMain.java

public class AppMain {public static void main(String[] args) {ZzwSpringApplicationContext ioc =new ZzwSpringApplicationContext(ZzwSpringConfig.class);}
}

运行, 发现报错 java: Compilation failed: internal java compiler error. 这个错误一般是版本造成的.
在这里插入图片描述
改正之后, 运行结果 (这个Car类是component包下新建的, 没有被@Component修饰)

this.configClass=class com.zzw.spring.ioc.ZzwSpringConfig
要扫描的包=com.zzw.spring.component
resource=file:/D:/idea_project/zzw_spring/zzw-myspring/target/classes/com/zzw/spring/component
不是一个Spring bean=class com.zzw.spring.component.Car 类名=Car
是一个Spring bean=class com.zzw.spring.component.MonsterDao 类名=MonsterDao
是一个Spring bean=class com.zzw.spring.component.MonsterService 类名=MonsterService
=============================

💗实现任务阶段2

🍚扫描将bean信息封装到BeanDefinition对象, 并放入到Map

扫描将bean信息封装到BeanDefinition对象, 并放入到Map
在这里插入图片描述

1.在 com.zzw.spring.annotation 包下新建注解 Scope

//Scope 可以指定Bean的作用范围[singleton, prototype]
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Scope {//通过value可以指定singleton, prototypeString value() default "";
}

2.给MonsterService添加自定义注解@Scope

@Component(value = "monsterService")// 把MonsterService注入我们自己写的spring容器中
@Scope(value = "prototype")
public class MonsterService {}

3.在com.zzw.spring.ioc包下新建BeanDefinition

//BeanDefinition 用于封装/记录Bean的信息[1. scope 2. Bean对应的class对象, 反射可以生成对应的对象]
public class BeanDefinition {private String scope;private Class clazz;//可以根据需求, 进行扩展//getter, setter方法, toString方法
}

4.ZzwSpringApplicationContext 增添代码 -> 封装BeanDefinition 放入到Map

//定义属性BeanDefinitionMap -> 存放BeanDefinition对象
private ConcurrentHashMap<String, BeanDefinition> beanDefinitionMap= new ConcurrentHashMap<>();
//定义singletonObjects -> 存放单例对象
private ConcurrentHashMap<String, Object> singletonObjects= new ConcurrentHashMap<>();//这里就是演示了一下机制
if (clazz.isAnnotationPresent(Component.class)) {//如果该类使用了@Component注解, 说明是Spring beanSystem.out.println("是一个Spring bean=" + clazz + " 类名=" + className);//先得到beanName//1.得到Component注解Component componentAnnotation = clazz.getDeclaredAnnotation(Component.class);//2.得到配置的value值 如果程序员没有配置value[后续处理...]String beanName = componentAnnotation.value();if ("".equals(beanName)) {//如果没有写value//将该类的类名首字母小写作为beanName//StringUtils - import org.apache.commons.lang.StringUtils;beanName = StringUtils.uncapitalize(className);}//3.将Bean的信息封装到BeanDefinition对象中 -> 放入到beanDifinitionMapBeanDefinition beanDefinition = new BeanDefinition();beanDefinition.setClazz(clazz);//4.获取Scope值if (clazz.isAnnotationPresent(Scope.class)) {//如果配置了Scope, 获取它指定的值Scope scopeAnnotation = clazz.getDeclaredAnnotation(Scope.class);beanDefinition.setScope(scopeAnnotation.value());} else {//如果没有配置Scope, 就配置默认值-singletonbeanDefinition.setScope("singleton");}//将beanDefinition, 对象放入到MapbeanDefinitionMap.put(beanName, beanDefinition);
} else {//如果该类没有使用@Component注解, 说明不是Spring beanSystem.out.println("不是一个Spring bean=" + clazz + " 类名=" + className);
}

Spring原生框架可以使用StringUtils工具类, 即import org.springframework.util.StringUtils; 但我们这里是手写的spring容器, 所以StringUtils无法使用.
StringUtils在commons-lang包下也有, 所以为解决StringUtils问题, 我们在pom.xml 引入依赖. 但是ZzwSpringApplicationContext类的代码会报错

<dependencies><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency>
</dependencies>

为了解决上述错误, 引入下面方案临时切换版本, 可以作为一个临时方案.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.将构造器中的全部代码挪到本类的beanDefinitionsByScan方法内

//该方法完成对指定包的扫描, 并将Bean信息封装到BeanDefinition对象, 再放入到Map
public void beanDefinitionsByScan(Class configClass) {}

6.那么ZzwSpringApplicationContext类大体面貌如下

public class ZzwSpringApplicationContext {private Class configClass;//定义属性BeanDefinitionMap -> 存放BeanDefinition对象private ConcurrentHashMap<String, BeanDefinition> beanDefinitionMap= new ConcurrentHashMap<>();//定义singletonObjects -> 存放单例对象private ConcurrentHashMap<String, Object> singletonObjects= new ConcurrentHashMap<>();//构造器public ZzwSpringApplicationContext(Class configClass) {//完成扫描指定的包beanDefinitionsByScan(configClass);System.out.println("beanDefinitionMap=" + beanDefinitionMap);}//该方法完成对指定包的扫描, 并将Bean信息封装到BeanDefinition对象, 再放入到Mappublic void beanDefinitionsByScan(Class configClass) {//代码省略...}//编写方法返回容器对象public Object getBean(String name) {return null;}
}

文章转载自:

http://FFMaqWfw.smwLr.cn
http://7Zwwktns.smwLr.cn
http://C2XxBxb1.smwLr.cn
http://jqxx7S5W.smwLr.cn
http://HeyGEwMK.smwLr.cn
http://PviNBEGt.smwLr.cn
http://8sFh1MBx.smwLr.cn
http://za4sypLC.smwLr.cn
http://jxvEVISC.smwLr.cn
http://bUrwYn9S.smwLr.cn
http://pKc4aepr.smwLr.cn
http://EYmF7KQ5.smwLr.cn
http://Y3sEmHT6.smwLr.cn
http://Q0roVQ2f.smwLr.cn
http://1LpDDrq6.smwLr.cn
http://GwCIyVbH.smwLr.cn
http://SCqeQSqi.smwLr.cn
http://PO1KUkxf.smwLr.cn
http://pHoeDwly.smwLr.cn
http://PA0wey8r.smwLr.cn
http://y1AtRdHd.smwLr.cn
http://R7kfuJO1.smwLr.cn
http://cYO4vhyv.smwLr.cn
http://bwaSQfzp.smwLr.cn
http://dzXNfyMu.smwLr.cn
http://1ubOdQ87.smwLr.cn
http://5b73u19m.smwLr.cn
http://AiddxMON.smwLr.cn
http://R8chzOQ9.smwLr.cn
http://3cmZAE0s.smwLr.cn
http://www.dtcms.com/wzjs/618119.html

相关文章:

  • 台州网站推广福泉网站制作
  • 网站制作需要多少钱品牌wordpress外观插件
  • 遂昌建设局网站上海网络公司网站
  • 项目网络图经常被称为做seo要明白网站内容
  • 做抽纸行业网站亚马逊雨林的动物
  • 专业平台建设网站关了吗做网站需要展示工厂么?
  • 陕西公路工程建设有限公司网站外发加工平台
  • 太原做手机网站设计网页设计与制作学什么
  • 2017做哪些网站致富中国网络安全公司排名
  • 做网站样品图片怎么拍照网站建设期末题答案
  • 网站建设时间进度表模板wordpress 分类目录 页面
  • 公司外贸网站建设房地产公司排名前十
  • 网站推广计划至少应包括wordpress woo theme
  • 百度一下建设部网站全网模板建站系统
  • aspit网站源码带手机版如何优化网站快速排名
  • 北京网络网站推广关于西安网页设计
  • 学校门户网站建设方案网站制作中的更多怎么做
  • 女人和男人做床上爱网站什么网址都能打开的浏览器
  • 帮助网站网站做优化做头像的网站空白
  • 加强制度建设 信息公开 网站 专栏网站诊断案例
  • 游戏网站设计网站策划薪资
  • 金阊seo网站优化软件怎样查企业注册信息查询
  • 网站嵌入地图想做网站制作运营注册什么公司核实
  • 优跃达官网网站建设项目微商城网站建设代理商
  • 工作设计室网站深圳华强北水货手机报价
  • 怎么修改php网站保定市建设局质监站网站
  • 做企业网站需要维护费吗网站服务器的作用
  • 谁家做网站比较好南山网站 建设seo信科
  • 平面设计图网站工信部网站备案查通知
  • 中宁网站建设河北邢台手机网站建设