网站建设找d云世家,怎么样才能让网站,新项目首码对接平台,新泰网站建设方案从上学期开始决心开始学习Spring#xff0c;自己总是利用不好时间#xff0c;到处瞎忙#xff0c;结果浪费了好多时间。想着利用暑假的时间#xff0c;专心看会儿书。最初我在Spring官网下载jar包的时候#xff0c;忙会儿了半天愣是没找到下载的链接#xff0c;瞬间觉得学…从上学期开始决心开始学习Spring自己总是利用不好时间到处瞎忙结果浪费了好多时间。想着利用暑假的时间专心看会儿书。最初我在Spring官网下载jar包的时候忙会儿了半天愣是没找到下载的链接瞬间觉得学Spring好难莫名的抵触心理开始泛滥无奈只好强抱度娘。这是我找到的链接。 教您怎么从spring 官网下载参考文档 附上小图 Spring官网下载 嘿嘿其实好简单哦可是人家当时就是没找到嘛也不想那么快地拥抱度娘23333…… Spring下载解压后可以看到lib目录下jar包确实有点多哦每个jar包都有三个不同类型的一种是含.class文件的jar包….jar)这也是我们项目中需要的还有两种是帮助文档…-javadoc.jar)和源码…-sources.jar。以下内容纯粹是我的看书笔记哦我是看的李刚的那本轻量级javaee。自己总是太快忘记一些事还有一些人所以记录下罗。 我看书写第一个Demosetter、getter都删掉罗 src/cn/edu/cqupt/MySpring/bean/Person.java package cn.edu.cqupt.MySpring.bean.impl;
public class Person {private String name;private int age;public String toString(){return Name:this.name,age:this.age;}
} src/beans.xml ?xml version1.0 encodingutf-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
bean idperson classcn.edu.cqupt.MySpring.bean.impl.Personproperty namename valuexiaofeig/property nameage value20/
/bean
/beans src/cn/edu/cqupt/MyString/test/TestMain.java package cn.edu.cqupt.MySpring.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.cqupt.MySpring.bean.impl.Person;
public class TestMain {public static void main(String[] args) {ApplicationContext contextnew ClassPathXmlApplicationContext(beans.xml);Person personcontext.getBean(person,Person.class);System.out.println(person);} 打印结果Name:xiaofeig,age:20 Spring的核心机制依赖注入Dependency Injection 我们在xml文件中配置了一个编号为person的实例之后Spring容器就负责帮助我们来创建这个实例这个实例包含两个属性分别为name和age通过property标签来设置它的值value。配置完成之后Spring容器就通过反射机制调用Person类的无参构造方法创建一个实例然后再调用属性nameage的setter方法为这两个属性设值。这应该就是最简单的依赖注入啦。我们只需要在容器ApplicationContext中通过实例编号person去取就可以啦。书上还有一些关于依赖关系的一些介绍加入A组件调用了B组件的方法我们可称A组件依赖于B组件。 依赖注入还有个特别高大上的名字控制反转Inversion of ControlIoC有木有是不是很难懂。 Spring推荐面向接口编程更好地让规范和实现分离写代码会更愉快……好有道理的样子好吧我会试试的 下面的示例是注入一个复杂属性就是将一个配置好的bean作为另一个bean的属性 src/cn/edu/cqupt/MySpring/bean/Axe.java package cn.edu.cqupt.MySpring.bean;
/**斧子六级没过原谅我不认识这个单词*/
public interface Axe {public String chop();
} src/cn/edu/cqupt/MySpring/bean/impl/StoneAxe.java package cn.edu.cqupt.MySpring.bean.impl;
import cn.edu.cqupt.MySpring.bean.Axe;
public class StoneAxe implements Axe {Overridepublic String chop() {return 石斧...;}
} src/cn/edu/cqupt/MySpring/bean/impl/Person.javasetter、getter都删掉罗 package cn.edu.cqupt.MySpring.bean.impl;
import cn.edu.cqupt.MySpring.bean.Axe;
public class Person {private String name;private int age;private Axe axe;public String toString(){return Name:this.name,age:this.age;}
} src/beans.xml部分代码 ?xml version1.0 encodingutf-8?bean idperson classcn.edu.cqupt.MySpring.bean.impl.Personproperty namename valuexiaofeig/property nameage value20/property nameaxe refstoneAxe/
/bean
bean idstoneAxe classcn.edu.cqupt.MySpring.bean.impl.StoneAxe/ src/cn/edu/cqupt/MyString/test/TestMain.java package cn.edu.cqupt.MySpring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.cqupt.MySpring.bean.Axe;
import cn.edu.cqupt.MySpring.bean.impl.Person;
public class TestMain {public static void main(String[] args) {ApplicationContext contextnew ClassPathXmlApplicationContext(beans.xml);Person personcontext.getBean(person,Person.class);Axe axeperson.getAxe();System.out.println(axe.chop());}
} 打印结果石斧...看这句property nameaxe refstoneAxe/axe表示Person的属性stoneAxe是我们配置的是一个实例的编号通过ref这个标签属性我们就可以轻松地将stoneAxe实例注入到Person的属性中看起来是不是也很easy。 我看书到537页的时候觉得最重要的一点就是依赖注入通过配置的方式将Bean实例之间代码层次的耦合分离了出来我们可以清晰地透过xml文件观察到Bean实例之间的依赖关系。 书上原话总结了Spring IoC容器的3个基本要点 面向接口编程可以将各组件之间的耦合提升到接口层次从而有利项目后期的扩展 应用程度的各组件不再有程序主动产生而是有Spring容器来负责产生、并初始化 Spring采用配置文件、或Annotation注解书后面会有提到不过我总是用不来新事物的自然抵触来管理Bean的实现类、依赖关系Spring容器通过配置文件利用反射来创建实例所谓的依赖注入 可以看出上面写的Demo都是Spring容器利用setter方法来为实例注入属性值的其实我们也可以让容器利用有参构造方法来直接创建一个实例这就是书上提到的构造注入。 src/cn/edu/cqupt/MySpring/bean/impl/Person.javasetter、getter都删掉罗 package cn.edu.cqupt.MySpring.bean.impl;
import cn.edu.cqupt.MySpring.bean.Axe;
public class Person {private String name;private int age;private Axe axe;public Person(){}public Person(String name,int age,Axe axe){this.namename;this.ageage;this.axeaxe;}public String toString(){return Name:this.name,age:this.age;}
} src/beans.xml ?xml version1.0 encodingutf-8?
bean idperson classcn.edu.cqupt.MySpring.bean.impl.Personconstructor-arg valuexiaofeig/constructor-arg value20/constructor-arg refstoneAxe/
/bean
bean idstoneAxe classcn.edu.cqupt.MySpring.bean.impl.StoneAxe/ src/cn/edu/cqupt/MySpring/test/TestMain.java package cn.edu.cqupt.MySpring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.cqupt.MySpring.bean.impl.Person;
public class TestMain {public static void main(String[] args) {ApplicationContext contextnew ClassPathXmlApplicationContext(beans.xml);Person personcontext.getBean(person,Person.class);System.out.println(person);System.out.println(person.getAxe().chop());}
} 打印结果 Name:xiaofeig,age:20 石斧... 标签constructor-arg/就是用来构造注入的哦标签的顺序就是构造参数的顺序上面的实例比较简单Person类只有一个有参构造方法第二个属性在注入的时候直接将”20”转换成了整型现在来假想一种情况如果Person类还有一个构造方法如 打印结果
Name:xiaofeig,age:0
石斧... 是不是秒懂啊Spring容器会优先考虑上面的那个构造方法如果我们想让Spring容器先调用含int age的构造方法可以设置标签property的属性type值为’int’。 constructor-arg value20 typeint/ hint: specify index/type/name arguments for simple parameters to avoid type ambiguities 好了简单地了解了这两种构造方法设值注入和构造注入个人还是比较喜欢设值注入啦。转载于:https://www.cnblogs.com/fei-er-blog/p/4741892.html