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

阿里云域名注册服务网站昆明百度关键词优化

阿里云域名注册服务网站,昆明百度关键词优化,推广网站,中央回应恶意不买房阶段2: // 1.编写自己的Spring容器,实现扫描包,得到bean的class对象.2.扫描将 bean 信息封装到 BeanDefinition对象,并放入到Map.思路: 1.将 bean 信息封装到 BeanDefinition对象中,再将其放入到BeanDefinitionMap集合中,集合的结构大概是 key[beanName]–value[beanDefintion…

阶段2:

 // 1.编写自己的Spring容器,实现扫描包,得到bean的class对象.2.扫描将 bean 信息封装到 BeanDefinition对象,并放入到Map.

思路:

1.将 bean 信息封装到 BeanDefinition对象中,再将其放入到BeanDefinitionMap集合中,集合的结构大概是
key[beanName]–value[beanDefintion]
key--------->对应指定的名字,未指定则以类的首字母小写为其名字
value------->对应封装好的BeanDefintion对象

2.因为bean的作用域可能是singleton,也可能是prototype,所以Spring需要扫描到bean信息,保存到集合,这样当getBean()根据实际情况处理.

具体实现

1.加一个自定义Scope注解

package com.elf.spring.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @author 45~* @version 1.0* Scope 可以指定一个Bean的作用范围[singleton,prototype]*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Scope {//通过value可以指定singleton,prototypeString value() default "";
}

2.在MonsterService.java上加上@Scope多实例注解

package com.elf.spring.component;
import com.elf.spring.annotation.Component;
import com.elf.spring.annotation.Scope;/*** @author 45~* @version 1.0* 说明 MonsterService 是一个Servic*/
@Component //把MonsterService注入到我们自己的spring容器中
@Scope(value = "prototype")
public class MonsterService {}

3.准备ioc包下写一个BeanDefinition.java 用于封装记录Bean信息.

package com.elf.spring.ioc;/*** @author 45~* @version 1.0* BeanDefinition 用于封装和记录Bean的信息 [1.scope 2.存放bean对应的Class对象,反射可以生成对应的对象]*  2:因为将来getBean()时有可能是多实例,有可能是动态生成的,还要存放bean的class对象*/
public class BeanDefinition {private String scope;private Class clazz;//存放bean的class对象public String getScope() {return scope;}public void setScope(String scope) {this.scope = scope;}public Class getClazz() {return clazz;}public void setClazz(Class clazz) {this.clazz = clazz;}@Overridepublic String toString() {return "BeanDefinition{" +"scope='" + scope + '\'' +", clazz=" + clazz +'}';}
}

3.pom.xml文件引入jar包下的工具类commons-lang,完成首字母小写的功能.而不用springframework自带的StringUtils工具类
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.elf</groupId><artifactId>elf-myspring1207</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency></dependencies></project>

4.容器文件,把构造器里边的方法抽取出来封装成一个方法,直接在构造器中调用,使代码简洁.
这里完成生成BeanDefinition对象并放入到Map里面

添加内容1:
//定义属性BeanDefinitionMap -> 存放BeanDefinition对象(多例对象)private ConcurrentHashMap<String,BeanDefinition> beanDefinitionMap =new ConcurrentHashMap<>();//定义属性SingletonObjects -> 存放单例对象 (跟原生容器的名字保持一致)//因为将来存放单例池的时候肯定要指定单例对象是对应哪个Bean的,所以k用String来充当//存放单例对象的类型是不确定的,可能是Dog,Cat,或者其他的对象,所以用Objectprivate ConcurrentHashMap<String,Object> singletonObjects =new ConcurrentHashMap<>();
添加内容2:
//先得到beanName(有可能通过经典4注解,例如Component注解的value值来指定)//1.得到类上的Component注解,此时的clazz已经是当前bean的class对象,通过类加载器得到的 反射知识Component componentAnnotation = cla.getDeclaredAnnotation(Component.class);//2.得到配置的valueString beanName = componentAnnotation.value();if("".equals(beanName)){//如果没有写value,空串//将该类的类名首字母小写作为beanName//StringUtils其实是在springframework的框架下面的类,而这里本身我就是要自己写所以不用beanName = StringUtils.uncapitalize(className);}//3.将Bean的信息封装到BeanDefinition对象中,然后将其放入到BeanDefinitionMap集合中BeanDefinition beanDefinition = new BeanDefinition();//!!!多看看这里多理解beanDefinition.setClazz(cla);//由类加载器通过反射得到对象,Class<?> cla = classLoader.loadClass(classFullName);//4.获取Scope值if (cla.isAnnotationPresent(Scope.class)){//如果配置了Scope,就获取它配置的值Scope scopeAnnotatiion = cla.getDeclaredAnnotation(Scope.class);beanDefinition.setScope(scopeAnnotatiion.value());}else{//如果没有配置Scope,就以默认的值singletonbeanDefinition.setScope("singleton");}//将beanDefinitionMap对象放入MapbeanDefinitionMap.put(beanName,beanDefinition);}else {//如果该类没有使用了@Component注解,说明是一个Spring beanSystem.out.println("这不是一个Spring bean" + cla + " 类名=" + className);}

容器文件

package com.elf.spring.ioc;import com.elf.spring.annotation.*;
import org.apache.commons.lang.StringUtils;import java.io.File;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;/*** @author 45~* @version 1.0*/
public class ElfSpringApplicationContext {//第一步,扫描包,得到bean的class对象,排除包下不是bean的,因此还没有放到容器中//因为现在写的spring容器比原先的基于注解的,要更加完善,所以不会直接把它放在ConcurrentHashMapprivate Class configClass;//定义属性BeanDefinitionMap -> 存放BeanDefinition对象private ConcurrentHashMap<String,BeanDefinition> beanDefinitionMap =new ConcurrentHashMap<>();//定义属性SingletonObjects -> 存放单例对象 (跟原生容器的名字保持一致)//因为将来存放单例池的时候肯定要指定单例对象是对应哪个Bean的,所以k用String来充当//存放单例对象的类型是不确定的,可能是Dog,Cat,或者其他的对象,所以用Objectprivate ConcurrentHashMap<String,Object> singletonObjects =new ConcurrentHashMap<>();//构造器public ElfSpringApplicationContext(Class configClass) {beanDefinitionScan(configClass);//调用封装方法,简洁System.out.println("beanDefinitionMap=" + beanDefinitionMap);}//构造器结束//该方法完成对指定包的扫描,并将Bean信息封装到BeanDefinition对象,再放入到Map中public void beanDefinitionScan(Class configClass){this.configClass = configClass;/**获取要扫描的包:1.先得到ElfSpringConfig配置的 @ComponentScan(value= "com.elf.spring.component")2.通过 @ComponentScan的value => 即要扫描的包 **/ComponentScan componentScan =(ComponentScan) this.configClass.getDeclaredAnnotation(ComponentScan.class);String path = componentScan.value();System.out.println("要扫描的包path=" + path);/*** 得到要扫描包下的所有资源(类.class)* 1.得到类的加载器 -> APP类加载器是可以拿到 target目录下的classes所有文件的* 2.通过类的加载器获取到要扫描的包的资源url => 类似一个路径* 3.将要加载的资源(.class)路径下的文件进行遍历 => io*/ClassLoader classLoader = ElfSpringApplicationContext.class.getClassLoader();path = path.replace(".", "/"); // 把.替换成 /URL resource = classLoader.getResource(path);System.out.println("resource=" + resource);File file = new File(resource.getFile());if (file.isDirectory()) {File[] files = file.listFiles();for (File f : files) { //把所有的文件都取出来System.out.println("============================");System.out.println("f.getAbsolutePath()=" + f.getAbsolutePath());String fileAbsolutePath = f.getAbsolutePath();//到target的classes目录下了//这里只处理.class文件if (fileAbsolutePath.endsWith(".class")) {//1.获取类名String className = fileAbsolutePath.substring(fileAbsolutePath.lastIndexOf("\\") + 1,fileAbsolutePath.indexOf(".class"));//2.获取类的完整路径(全类名)String classFullName = path.replace("/", ".") + "." + className;System.out.println("classFullName=" + classFullName);//3.判断该类是否需要注入,就看是不是有注解@Component @Service @Contoller @Re....try {Class<?> cla = classLoader.loadClass(classFullName);if (cla.isAnnotationPresent(Component.class) ||cla.isAnnotationPresent(Controller.class) ||cla.isAnnotationPresent(Service.class) ||cla.isAnnotationPresent(Repository.class)) {//演示机制//如果该类使用了@Component注解,说明是一个Spring beanSystem.out.println("这是一个Spring bean=" + cla + " 类名=" + className);//先得到beanName(有可能通过经典4注解,例如Component注解的value值来指定)//1.得到类上的Component注解,此时的clazz已经是当前bean的class对象,通过类加载器得到的 反射知识Component componentAnnotation = cla.getDeclaredAnnotation(Component.class);//2.得到配置的valueString beanName = componentAnnotation.value();if("".equals(beanName)){//如果没有写value,空串//将该类的类名首字母小写作为beanName//StringUtils其实是在springframework的框架下面的类,而这里本身我就是要自己写所以不用beanName = StringUtils.uncapitalize(className);}//3.将Bean的信息封装到BeanDefinition对象中,然后将其放入到BeanDefinitionMap集合中BeanDefinition beanDefinition = new BeanDefinition();//!!!多看看这里多理解beanDefinition.setClazz(cla);//由类加载器通过反射得到对象,Class<?> cla = classLoader.loadClass(classFullName);//4.获取Scope值if (cla.isAnnotationPresent(Scope.class)){//如果配置了Scope,就获取它配置的值Scope scopeAnnotatiion = cla.getDeclaredAnnotation(Scope.class);beanDefinition.setScope(scopeAnnotatiion.value());}else{//如果没有配置Scope,就以默认的值singletonbeanDefinition.setScope("singleton");}//将beanDefinitionMap对象放入MapbeanDefinitionMap.put(beanName,beanDefinition);}else {//如果该类没有使用了@Component注解,说明是一个Spring beanSystem.out.println("这不是一个Spring bean" + cla + " 类名=" + className);}} catch (Exception e) {e.printStackTrace();}}}//遍历文件for循环结束System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");}}//编写放法返回容器中的对象public Object getBean(String name) {return null;}
}

运行结果

beanDefinitionMap={
monsterService=BeanDefinition{scope=‘prototype’, clazz=class com.elf.spring.component.MonsterService},

monsterDao=BeanDefinition{scope=‘singleton’, clazz=class com.elf.spring.component.MonsterDao}
}
ok
在这里插入图片描述
这里存在一个问题:单例多例对象都是放在beanDefinitionMap, singletonObjects里没有单例对象.


文章转载自:
http://dinncocement.ydfr.cn
http://dinncoflexuous.ydfr.cn
http://dinncorepressive.ydfr.cn
http://dinncozygology.ydfr.cn
http://dinncomalayan.ydfr.cn
http://dinncorapine.ydfr.cn
http://dinncotribunite.ydfr.cn
http://dinncomagh.ydfr.cn
http://dinncoantiracism.ydfr.cn
http://dinncothy.ydfr.cn
http://dinncostalino.ydfr.cn
http://dinnconhra.ydfr.cn
http://dinncotraversing.ydfr.cn
http://dinncoelectrotype.ydfr.cn
http://dinncofireworks.ydfr.cn
http://dinncojudicature.ydfr.cn
http://dinncolooped.ydfr.cn
http://dinncofungi.ydfr.cn
http://dinncocrazyweed.ydfr.cn
http://dinncovomitous.ydfr.cn
http://dinncopeppergrass.ydfr.cn
http://dinncoadministration.ydfr.cn
http://dinncooverstorage.ydfr.cn
http://dinncopermissible.ydfr.cn
http://dinncogelatinize.ydfr.cn
http://dinncomicrotopography.ydfr.cn
http://dinncohap.ydfr.cn
http://dinncowadable.ydfr.cn
http://dinncoimmunocytochemistry.ydfr.cn
http://dinncofactor.ydfr.cn
http://dinncosnapdragon.ydfr.cn
http://dinncoinly.ydfr.cn
http://dinncoconnoisseur.ydfr.cn
http://dinncostoma.ydfr.cn
http://dinncophlegmatical.ydfr.cn
http://dinncocommixture.ydfr.cn
http://dinncomordant.ydfr.cn
http://dinncoimmethodical.ydfr.cn
http://dinncoaciculignosa.ydfr.cn
http://dinncoradiocarbon.ydfr.cn
http://dinncospyglass.ydfr.cn
http://dinncosightworthy.ydfr.cn
http://dinncoruffe.ydfr.cn
http://dinncoengaging.ydfr.cn
http://dinncoretrochoir.ydfr.cn
http://dinncosexualia.ydfr.cn
http://dinncoslipcover.ydfr.cn
http://dinncoaltocumulus.ydfr.cn
http://dinncospigotty.ydfr.cn
http://dinncodiscarnate.ydfr.cn
http://dinncoquasiatom.ydfr.cn
http://dinncospheriform.ydfr.cn
http://dinncodevil.ydfr.cn
http://dinncocumec.ydfr.cn
http://dinncosternwards.ydfr.cn
http://dinncobubonic.ydfr.cn
http://dinncodoltish.ydfr.cn
http://dinncohomer.ydfr.cn
http://dinncoalemannic.ydfr.cn
http://dinncoindomitable.ydfr.cn
http://dinncorocket.ydfr.cn
http://dinncoincommunicability.ydfr.cn
http://dinncolandskip.ydfr.cn
http://dinncobissextile.ydfr.cn
http://dinncoafoul.ydfr.cn
http://dinncoradiotelephony.ydfr.cn
http://dinncotritely.ydfr.cn
http://dinncoovereaten.ydfr.cn
http://dinncovahan.ydfr.cn
http://dinncoplim.ydfr.cn
http://dinncokgr.ydfr.cn
http://dinncohemoglobinopathy.ydfr.cn
http://dinncobreastwork.ydfr.cn
http://dinncovillagization.ydfr.cn
http://dinncoexcelsior.ydfr.cn
http://dinncosetdown.ydfr.cn
http://dinncogametogony.ydfr.cn
http://dinncosophistication.ydfr.cn
http://dinncoalchemistic.ydfr.cn
http://dinncomogilalia.ydfr.cn
http://dinncomediography.ydfr.cn
http://dinncofinale.ydfr.cn
http://dinncopredictability.ydfr.cn
http://dinncogauge.ydfr.cn
http://dinncofooper.ydfr.cn
http://dinncobasify.ydfr.cn
http://dinncosuperplasticity.ydfr.cn
http://dinncoacetated.ydfr.cn
http://dinncocover.ydfr.cn
http://dinncofenian.ydfr.cn
http://dinncoyeld.ydfr.cn
http://dinncomental.ydfr.cn
http://dinncoslightly.ydfr.cn
http://dinncosuable.ydfr.cn
http://dinncoexplorative.ydfr.cn
http://dinncofrustulum.ydfr.cn
http://dinncocommissar.ydfr.cn
http://dinncomuffetee.ydfr.cn
http://dinncooffertory.ydfr.cn
http://dinncoparochial.ydfr.cn
http://www.dinnco.com/news/151894.html

相关文章:

  • 国外做任务赚钱的网站网站建设公司哪家好?该如何选择
  • 专门做化妆的招聘网站知乎关键词排名
  • 网站备案 公司seo网站关键词优化
  • 会python做网站seo技术有哪些
  • 青岛红岛做网站百度关键词检测工具
  • 做网站抄代码创意营销新点子
  • 嘉兴网站建设网站线上推广有哪些
  • 做网站协调百度网站统计
  • 东莞长安网站设计苏州百度 seo
  • 网站的收费窗口怎么做百度极简网址
  • 网站菜单素材余姚seo智能优化
  • 网站数据采集怎么做seo关键词分类
  • 住房和城乡建设部网站 绿地长沙网
  • 推荐做幻灯片搜图网站云搜索网页版入口
  • 德庆网站建设网站建设山东聚搜网络
  • 福州专业网站建设服务商百度搜索竞价排名
  • 微网站建设目的查域名备案
  • 做网站有哪些类型淘宝美工培训
  • 佛山网站建设 骏域网站网络推广的细节
  • 做特卖的网站怎么赚钱建站软件可以不通过网络建设吗
  • 厦门建设局招聘东莞百度推广优化公司
  • b2b旅游网站建设站长工具端口检测
  • 腾讯企点app下载安装seo营销是什么
  • 平面设计网站有哪些比较好seo是指搜索引擎优化
  • 深圳积分商城网站建设seo工具下载
  • 电话客服系统新网站seo外包
  • 自己做的网站收费微信seo
  • 百度seo优化公司网站seo的方法
  • 做ar的网站自动点击器怎么用
  • 做么做好网站运营网站查询信息