• Spring-IOC和DI


    提示:

    本文只含有使用方法,更多详细内容请访问《其他优秀博客》.
    POJO类中必须加上get/set方法.

    IOC(控制反转)

    导入相关jar包

    配置applicationcontext.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    
        <bean id="tea" class="org.wm.pojo.Teacher"></bean>
    
        <bean id="stu" class="org.wm.pojo.Studnet">
            <property name="sid" value="1"></property>
            <property name="sname" value="张三"></property>
            <property name="sage" value="18"></property>
            <property name="teacher" ref="tea"></property>
        </bean>
    
    </beans>
    

    使用Spring容器对象获取容器中创建的对象完成操作

            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
            Studnet stu = (Studnet) ac.getBean("stu");
            System.out.println(stu);
    

    结果:

    Studnet{sid=1, sname='张三', sage=18, teacher=Teacher{tid=null, tname='null'}}
    

    IOC创建对象的方式:

    • 构造器。
    • get/set方法。
    • 工厂方式。
    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <!--配置对象的全限定路径-->
            <!--构造器方式:-->
                <!--无参构造器:单独的一个Bean标签配置,默认使用的是无参数构造器创建对象-->
                    <bean id="stu" class="com.bjsxt.pojo.Student"></bean>
                <!--
                    有参构造器:
                           在bean标签中使用constructor-arg子标签,一个constructor-arg子标签构造器的一个参数。
                           再使用constructor-arg标签的属性
                               index:参数的角标
                               type:参数的类型
                               name:参数的名字
                               value:参数的值
                           来唯一的确认一个构造器
                      缺点:
                            必须在类中声明对应的构造器。
                -->
                    <bean id="stu2" class="com.bjsxt.pojo.Student">
                        <constructor-arg  index="0" type="java.lang.Integer" name="sid" value="1"></constructor-arg>
                        <constructor-arg index="1" type="java.lang.String"  name="sname" value="张三"></constructor-arg>
                    </bean>
            <!--
                    get/set方法方式:
                           在bean标签下使用property标签,一个property相当于调用一个set方法
                           属性:
                                name:要赋值的属性的属性名
                                value:值
            -->
                    <bean id="stu3" class="com.bjsxt.pojo.Student">
                        <property name="sid" value="2"></property>
                        <property name="sname" value="李四"></property>
                    </bean>
            <!--工厂方式:由Spring容器帮助我们创建工厂对象,并使用工厂完成对象的生产,我们直接从Spring中获取生产的对象使用-->
                <!--静态工厂方式-->
                    <bean id="a3" class="com.bjsxt.pojo.AFactory2" factory-method="newInstance"></bean>
                <!--动态工厂方式-->
                    <bean id="factory" class="com.bjsxt.pojo.AFactory"></bean><!--创建工厂对象-->
                    <bean id="a4" factory-bean="factory" factory-method="newInstance"></bean><!--生产对象-->
            <!--依赖注入:-->
                    <!--构造器方式注入:使用constructor-arg属性来注入,使用属性ref,值为要注入的bean的ID-->
                        <!--创建学生对象-->
                            <bean id="stu4" class="com.bjsxt.pojo.Student">
                                <constructor-arg index="0" type="com.bjsxt.pojo.Teacher" name="teacher" ref="tea"></constructor-arg>
                            </bean>
                        <!--创建教师对象-->
                            <bean id="tea" class="com.bjsxt.pojo.Teacher"></bean>
                    <!--属性注入:必须提供对应的get/set方法 ,使用property标签,使用属性ref,属性值为要注入的bean的ID-->
                        <bean id="stu5" class="com.bjsxt.pojo.Student">
                            <property name="teacher" ref="tea2"></property>
                        </bean>
                        <bean id="tea2" class="com.bjsxt.pojo.Teacher"></bean>
    </beans>
    

    DI(依赖注入)

    问题:

    在责任链开发模式中,传统方式责任链的机制是A对象中有B对象,B对象中有C对象,在A中会调用B的资源,在B中会调用C的资源。代码的执行流程是A-->B--->C但是我们从Spring容器中获取的B对象中是没有C的,这样A可以获取Spring容器帮助我们创建的B,但是流程A-->B就结束了,而Spring返回的B中没有C。

    解决:

    将A,B,C,D在Spring容器中配置为bean标签,然后让Spring容器根据对象之间的依赖关系将对象分别注入到指定的对象中。我们直接获取使用即可。

    注意:

    在对象中需要声明依赖的对象属性,而且需要提供get/set方法或者对应的构造器。

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    
        <!-- 需要注入的对象 -->
        <bean id="tea" class="org.wm.pojo.Teacher"></bean>
    
        <bean id="stu" class="org.wm.pojo.Studnet">
            <property name="sid" value="1"></property>
            <property name="sname" value="张三"></property>
            <property name="sage" value="18"></property>
            <!-- 把名字对应的属性的类型引入 -->
            <property name="teacher" ref="tea"></property>
        </bean>
    
    </beans>
    

    IOC之自动注入

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
           default-autowire="byName"
        >
        <!--
            依赖注入:
                    自己在Spring配置文件中通过ref属性来声明注入内容。
            自动注入:
                    Spring容器根据指定的规则在创建对象时,自主的完成依赖关系的注入。
                    使用:
                          局部使用:
                          <bean id="stu" class="bjsxt.pojo.Student" autowire="byName"></bean>
                                   在bean标签上使用autowire属性声明注入规则。
                                       byName:根据属性名和某个bean的ID相同的规则注入。
                                       byType:根据属性的类型和某个bean的类型相同的规则注入。
                                                注意:同类型的bean标签只能有一个
                                       constructor:根据构造器中属性的类型和某个bean的类型相同的规则注入。
                                                 注意:必须有对应的构造器,而且构造器的参数必须全部为要注入的属性。
                                       default:使用默认方式,全局配置。默认值
                                       no:不使用自动注入,使用依赖注入。
                          全局使用:
                                在beans标签中使用属性default-autowire
                                        byName:根据属性名和某个bean的ID相同的规则注入。
        -->
        <!--配置学生bean对象-->
            <bean id="stu" class="bjsxt.pojo.Student">
                <!--<property name="teacher" ref="tea"></property>-->
             </bean>
        <!--配置教师bean对象-->
            <bean id="teacher" class="bjsxt.pojo.Teacher"></bean>
    </beans>
    

    IOC之注解

    注意:当使用注解配置bean时,若不加ID名,系统自动生成bean名为:类名首字母小写!

    注解的作用:

    替换XML的配置,提升开发效率。

    注解的内容:

    -@Component("ID名")

    • 使用:在类名上使用
    • 作用:相当于配置了该类的一个bean标签。默认类名首字母小写即为对象的ID
    • 一般使用在实体类上

    -@Service("ID名")

    • 使用:在类名上使用
    • 作用:相当于配置了该类的一个bean标签。默认类名首字母小写即为对象的ID
    • 一般使用在业务层类上

    -@Controller("ID名")

    • 使用:在类名上使用
    • 作用:相当于配置了该类的一个bean标签。默认类名首字母小写即为对象的ID
    • 一般使用在控制层类上,SpringMVC使用

    -@Resource:默认使用byName,如果不行再使用byType

    • 使用:在要依赖注入的属性上声明
    • 作用:相当于bean标签中的依赖注入声明
    • 好处:可以不声明get/set方法

    -@AutoWire:默认使用byType

    • 使用:在要依赖注入的属性上声明
    • 作用:相当于bean标签中的依赖注入声明
    • 好处:可以不声明get/set方法

    注意:

    Spring的注解必须在其配置文件中声明注解扫描,来告诉Spring哪些类使用注解配置bean对象。

    bean标签的scope属性

    Spring容器对象在被创建的时候就自动完成配置文件中相关bean的创建,造成我们从Spring容器中获取的对象不管获取多少次都是同一个。
    这是因为bean标签中的scope属性默认为singleton。

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <!--
            Spring容器默认在创建的时候就完成对bean对象的初始化创建,多次获取同一个bean
            返回的是同一个对象。
            注意:
                我们可以通过设置bean标签上的scope属性来告诉Spring容器,每次获取该bean
                都重新创建返回。
                scope属性:
                       singleton:默认值,该bean在Spring容器被创建的时候完成初始化创建
                       prototype:Spring容器被床架的时候并不会完成该bean的初始化创建,而是在
                                    每次获取的时候重新创建。
        -->
        <!--配置学生bean对象-->
            <bean id="stu" class="com.bjsxt.pojo.Student" scope="prototype"></bean>
    </beans>
    
  • 相关阅读:
    四则运算
    3.12----对potplayer的使用评价
    对软件工程的一点思考
    个人附加作业
    附加题
    个人最终总结
    结对编程总结
    修改后的四则运算
    阅读程序回答问题
    Visual studio 2013的安装和单元测试
  • 原文地址:https://www.cnblogs.com/kpsmile/p/12290701.html
Copyright © 2020-2023  润新知