美文网首页
spring学习笔记一-IOC

spring学习笔记一-IOC

作者: 大赟哥 | 来源:发表于2017-06-14 18:44 被阅读0次

    [TOC]

    DIP

    一种软件设计原则

    DIP1.png DIP2.png DIP3.png DIP4.png

    IOC

    一种设计模式,DIP的具体实现。即控制反转,将依赖(低层模块)对象的获得交给第三方(系统)来控制。

    DI

    IOC的一种重要的实现方式

    DI.png

    IOC容器

    IOC1.jpg IOC2.png

    spring简介

    作用:IOC容器,控制反转,将创建对象的权利交给容器去做

    好处:不用new对象,降低了类与类之间的耦合度

    功能:IOC+AOP+DATA+WEB

    spring.png

    spring的原理

    将bean的类名以及类与类的关系配置在xml文件中,通过反射的方式创建对象,并且组装对象。

    spring快速入门

    1、导包

    core、context、expression、bean

    2、引入schema文档(类似dtd文档)约束xml的文档

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd">
    </beans>
    

    3、通过xml配置bean

    <!-- 1、配置bean -->
    <bean class="com.hemi.bean.CandidateA" id="canA" />
    
    <bean class="com.hemi.bean.CandidateB" id="canB" />
    
    <bean class="com.hemi.bean.Personnel" id="personnel">
        <!-- 通过构造函数将候选人canA注入到人事部中 -->
        <constructor-arg name="programme" ref="canB" />
    </bean>
    

    4、创建测试类

    //1、获取xml文件,并且创建出ApplicationContext
    ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
    //2、获取人事部
    Personnel personnel=(Personnel)context.getBean("personnel");
    
    personnel.interview();
    

    注入方式

    • 构造函数方式注入
      • constructor-arg 构造函数参数
        • type 使用构造函数参数类型
        • name 使用构造函数参数名
        • index 使用位置 0代表构造函数的第一个位置,1代表第二个位置,依次类推
    <bean class="com.hemi.bean.Personnel" id="personnel">
        <constructor-arg index="0" ref="canB" />
        <constructor-arg index="1" ref="canA" />
    </bean>
    
    • get、set方式注入
      • property 代表属性名称
        • value 属性值
        • ref 对象的引用
    <bean class="com.hemi.bean.Personnel" id="personnel">
        <property name="name" value="lili"></property>
        <property name="programme" ref="canA"></property>
    </bean>
    
    • p名称空间

    在文档定义中添加xmlns:p="http://www.springframework.org/schema/p"

    <bean class="com.hemi.bean.Personnel" id="personnel" p:name="lisi"></bean>
    

    总结:

    spring ioc容器特点:

    1、在启动的时候会将所有的对象按顺序创建完毕
    2、按需注入
    3、按需获取

    bean参数详解

    id:对象的名字
    destory-method:ioc容器摧毁时创建
    init-method:创建对象时执行的方法
    depends-on:创建对象之前应该创建好的对象
    lazy-init:延迟创建对象
    scope:设置作用域,singleton(单例)、prototype(多例)、request、sesssion、global session factory-method:工厂方法
    factory-bean:工厂对象
    abstract:标记为抽象类

    注解创建对象

    创建对象 @Component @Service @Repository @Controller

    //创建对象的时候可以使用参数,设置对象的引用变量
    //如果没有写,那么默认使用小驼峰命名
    @Component("blackBox")
    public class BlackBox{}
    

    注意:四者用法一致,一般使用@Service

    注解注入对象

    注入对象的注解 - @Resource - @Autowired

    //name:按照名称来查找
    @Resource(name="blackBox")
    private IBox box;
    
    //type:按照类型来查找
    @Resource(type=A4Paper.class)
    private IPaper paper;
    
    //如果没有写,那么name就是参数的变量名 box,所以找不到,然后按照type来查找,IBox类型,所以可以找得到
    //如果没有写,而内存中有多个相同类型的对象,那么就报错
    @Resource
    private IBox box1;
    
    //@Autowired不能写任何参数
    //按照类型来查找,如果内存中有多个相同类型的对象,那么报错
    //解决问题:使用@Qualifier来指定注入哪个名称的对象
    @Autowired
    @Qualifier("blackBox")
    private IBox box;
    
    @Autowired
    private IPaper paper;
    

    注意:用哪个注解根据实际需求选择

    相关文章

      网友评论

          本文标题:spring学习笔记一-IOC

          本文链接:https://www.haomeiwen.com/subject/ekdeqxtx.html