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

WordPress装不上jetpackwordpress网站速度优化

WordPress装不上jetpack,wordpress网站速度优化,广水网站定制,网站开发职业什么是SpringIOC#xff1f; 答#xff1a;IOC即控制反转#xff0c;就是我们不在手动的去new一个对象#xff0c;而是将创建对象的权力交给Spring去管理#xff0c;我们想要一个User类型的对象#xff0c;就只需要定义一个User类型的变量user1#xff0c;然后让Spring去…什么是SpringIOC 答IOC即控制反转就是我们不在手动的去new一个对象而是将创建对象的权力交给Spring去管理我们想要一个User类型的对象就只需要定义一个User类型的变量user1然后让Spring去给我们创建对象然后将创建的对象注入到user1中。 什么是依赖注入 答DI机制DependencyInjection依赖注入上面提到的Spring将它创建的对象交给我们创建的变量的过程。依赖注入的方式有三种set方法注入、构造器注入、注解注入。下面我们简答的实现注解注入了解IOC原理。 第一步创建注解 创建的注解其实没有太大的作用就是用来标记哪个类需要Spring帮我们去管理哪个成员变量需要Spring去给我们注入。 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** *创建注解文件Component.java *标记需要IOC的类 **/ Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) public interface Component { }import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** *创建注解文件Autowired.java *标记需要Spring帮忙DI的成员变量 **/ Target(ElementType.FIELD) Retention(RetentionPolicy.RUNTIME) public interface Autowired { } 第二部创建两个需要注解的类 我们想要将MyService和MyController都交给Spring管理项目启动后将这两个类实例化然后放入一个HashMap中等待调用 import com.xiaoran.springioc.annotation.Component; Component public class MyService {private int id;private String name;private int age;public void speck(String name,int age) {System.out.println(大家好,我叫name今年age岁了,请多多关照!);} }import com.xiaoran.springioc.annotation.Autowired; import com.xiaoran.springioc.annotation.Component; Component public class MyController {Autowiredprivate MyService myService;public void test(){myService.speck(moss,67);} }第三步IOC过程 创建MyIOC类用于IOC过程 在项目启动时实例化MyIOC加载MyIOC的无参构造器时对项目下所有的文件进行扫描调用实例化方法完成被注释类的实例化和依赖注入。 private String basePathD:\\Project\\XiaoRanIOC\\src\\main\\java\\com\\xiaoran\\springioc\\; //项目路径private String basePackagecom.xiaoran.springioc; //包路径private ListString filePaths;//所有文件的路径private ListString beanNames;//所有.java文件的全限定名private MapString, Object beans new HashMap();/*** 实例化MyIOC时,让IOC进程伴随无参构造器加载启动*/public MyIOC() throws FileNotFoundException, IllegalAccessException {//扫描路径下所有文件scan();beanNames new ArrayList();initBeanNames();initBeans();}扫描项目下所有文件,将所有文件的路径存入filePaths中 /*** 扫描项目下所有文件,将所有文件的路径存入filePaths中*/public void scan() throws FileNotFoundException {File file new File(basePath);filePathsnew ArrayList();if (file.exists()) {//将file放入列,出队后判断,如果是路径那就继续入队,如果是文件,就将文件路径放入filePaths中QueueFile queue new LinkedList();queue.add(file);while (!queue.isEmpty()) {File poll queue.poll();if(pollnull){continue;}if (poll.isDirectory()){File[] files poll.listFiles();for (File f :files) {queue.add(f);}}else{filePaths.add(poll.getPath());}}}else {throw new FileNotFoundException(basePath 不存在);}}将所有的.java文件的全限定名放入beanNames中 /***将所有的.java文件的全限定名放入beanNames中*/public void initBeanNames(){for (String string :filePaths) {String replace string.replace(basePath, );if (replace.endsWith(.java)) {replace replace.substring(0, replace.length() - 5);}char[] chars replace.toCharArray();for (int i 0; i chars.length; i) {if(chars[i]\\){chars[i] .;}}beanNames.add(basePackage.new String(chars));}}核心代码:将被Component注解的类实例化放入beans(HashMap)中,等待调用 /***核心代码:将被Component注解的类实例化放入beans(HashMap)中,等待调用*/public void initBeans() throws IllegalAccessException {//遍历包路径下所有类,是否被Component注解,如果被注解就将其实例化放入beansfor (String beanName :beanNames) {try {Class? aClass Class.forName(beanName);//获取类的所有注解Annotation[] declaredAnnotation aClass.getDeclaredAnnotations();//遍历所有注解,是否是Component注解for (Annotation annotation :declaredAnnotation) {if (annotation instanceof Component){Object o aClass.newInstance();beans.put(beanName, o);}}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();}}//遍历所有的beans的成员变量,如果有成员变量被Autowired修饰,就根据成员变量的类型从beans中查找到对应的对象,用此对象给成员变量注入//因为是从beans中查找对象,所以被注入的成员变量对应的类一定是已经被实例化放入beans中的for (Map.EntryString, Object entry : beans.entrySet()) {Object value entry.getValue();Field[] fields value.getClass().getDeclaredFields();for (Field f :fields) {Annotation[] declaredAnnotations f.getDeclaredAnnotations();for (Annotation annotation :declaredAnnotations) {if (annotation instanceof Autowired){//获取被Autowired注解成员变量的类型(全限定名)String typeName f.getType().getName();Object o beans.get(typeName);//暴力反射f.setAccessible(true);//将从beans中获得的对象o,注入到该属性上f.set(value,o);}}}}}/*** 对外提供一个方法:根据全限定名返回对象*/public Object getInstance(String beanName){return beans.get(beanName);}第四步测试 创建测试类IOCTest import com.xiaoran.springioc.entity.MyController; import com.xiaoran.springioc.ioc.MyIOC; import org.junit.Test; import java.io.FileNotFoundException; public class IOCTest {Testpublic void test() throws FileNotFoundException, IllegalAccessException {MyIOC myIOC new MyIOC();MyController myController (MyController)myIOC.getInstance(MyController.class.getName());myController.test();} } GitHub 手撕SpringIOC
http://www.w-s-a.com/news/717324/

相关文章:

  • 网站导航容易做黄冈网站建设报价
  • 美橙互联建站网站被截止徐州网站建站
  • 网站班级文化建设视频深圳企业网页设计公司
  • 钦州网站建设公司做宣传网站买什么云服务器
  • 58同城有做网站wordpress怎么改标题和meta
  • 安通建设有限公司网站东莞地铁app
  • 群晖nas做网站滨州教育平台 网站建设
  • 住房城市乡建设部网站装修平台有哪些
  • 小米网站 用什么做的深圳广告公司前十强
  • 勤哲网站开发视频瑞安 网站建设培训
  • 有个蓝色章鱼做标志的网站高端的网站建设怎么做
  • 建站网址导航hao123html网页设计实验总结
  • 西宁市网站建设价格丽水集团网站建设
  • 长宁怎么做网站优化好本机怎么放自己做的网站
  • 诚信网站备案中心网站字体怎么设置
  • 企业网站建设费是无形资产吗佛山网站建设哪个好点
  • 网站建设就业方向国开行网站毕业申请怎么做
  • 创建一个网站的费用wordpress 4.0 安装
  • 会员登录系统网站建设dw软件是做什么用的
  • 手机网站被做跳转长沙网上购物超市
  • 网站建设中网站需求分析设计网站推荐html代码
  • 容易收录的网站台州汇客网站建设
  • 企业网站源码百度网盘下载网站备案号如何查询密码
  • 个人网站建设课程宣传栏制作效果图
  • 自己做的网站能上传吗网上做彩票网站排名
  • 教育培训网站模板下载自己做商务网站有什么利弊
  • 平面设计公司网站兰州室内设计公司排名
  • 个人工作室注册条件温州seo结算
  • 360免费建站系统中国建设银行官网站黄金部王毅
  • 罗源福州网站建设个体户可以网站备案吗