78

Spring学习笔记(一):IOC-1

 5 years ago
source link: https://aodeng.cc/archives/springyi?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring IOC-1

本文属于学习笔记,非教程,再次感谢Java大联盟

Spring的两大核心机制是IOC(控制反转)和AOP(面向切面编程),从开发的角度讲,我们使用Spring框架就是用它的IOC和AOP。

OC是典型的工厂模式,通过工厂去注入对象。

AOP则是代理模式的体现。

IOC也叫控制反转,首先从字面意思理解,什么叫控制反转?反转的是什么?

在传统的程序开发中,需要调用对象时,通常由调用者来创建被调用者的实例,即对象是由调用者主动new出来的。

但在Spring框架中创建对象的工作不再由调用者来完成,而是交给IOC容器来创建,再推送给调用者,整个流程完成反转,所

以是控制反转。

通过IOC容器来创建对象

无参构造

spring.xml

<!-- 配置student对象-->
         <bean id="stu" class="实体类包路径"></bean>

main

//1.加载spring.xml配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        //2.通过id值获取对象
        Student stu = (Student) applicationContext.getBean("stu");
        //3.通过运行时类获取对象
        Student stu = applicationContext.getBean(Student.class);

spring中给实体类赋值

<!-- 配置student对象 -->
         <bean id="stu" class="实体类包路径">
            <property name="实体类属性1" value="1"></property>
            <property name="实体类属性2" value="张三"></property>
            <property name="实体类属性3" value="23"></property>
        <!--特殊字符赋值eg:name="<张三>"-->
        <property name="name">
                <value><![CDATA[<张三>]]></value>
            </property>
         </bean>

有参构造

name的值需要与有参构造的形参名对应,value是对应的值。

除了使用name对应参数外,还可以通过下标index对应。

<!-- 通过有参构造函数创建对象 -->
         <bean id="stu3" class="com.hzit.entity.Student">
            <constructor-arg name="id" value="3"></constructor-arg>
            <constructor-arg name="name" value="小明"></constructor-arg>
            <constructor-arg name="age" value="22"></constructor-arg>
         </bean>
    <!-- 通过有参构造函数创建对象 -->
         <bean id="stu3" class="com.hzit.entity.Student">
               <constructor-arg index="0" value="3"></constructor-arg>
            <constructor-arg index="1" value="小明"></constructor-arg>
            <constructor-arg index="2" value="22"></constructor-arg>
         </bean>

IOC容器管理多个对象,并且对象之间有级联关系实现

实体类

public class Student {
    private int id;
    private String name;
    private int age;
    private Classes classes;
}

spring.xml中配置classes对象,然后将该对象赋值给stu对象。

<!-- 创建classes对象 -->
     <bean id="classes" class="com.hzit.entity.Classes">
        <property name="id" value="1"></property>
        <property name="name" value="Java班"></property>
     </bean>

     <!-- 创建stu对象 -->
     <bean id="stu" class="com.hzit.entity.Student">
        <property name="id" value="1"></property>
        <property name="name">
            <value><![CDATA[<张三>]]></value>
        </property>
        <property name="age" value="23"></property>
        <!-- 将classes对象赋给stu对象 -->
        <property name="classes" ref="classes"></property>
     </bean>

在spring.xml中,通过ref属性将其他bean赋给当前bean对象,这种方式叫做依赖注入(DI),
是Spring非常重要的机制,DI是将不同对象进行关联的一种方式,是IOC的具体实现方式,
通常DI和IOC是紧密结合在一起的,所以一般说的IOC包括DI。

集合属性依赖注入

public class Classes {
    private int id;
    private String name;
    private List<Student> students;
}

spring.xml中配置2个student对象,1个classes对象,并将2个student对象注入到classes对象中。

<!-- 配置classes对象 -->
     <bean id="classes" class="com.hzit.entity.Classes">
        <property name="id" value="1"></property>
        <property name="name" value="Java班"></property>
        <property name="students">
            <!-- 注入student对象 -->
            <list>
                <ref bean="stu"/>
                <ref bean="stu2"/>
            </list>
        </property>
     </bean>

     <bean id="stu" class="com.hzit.entity.Student">
        <property name="id" value="1"></property>
        <property name="name">
            <value><![CDATA[<张三>]]></value>
        </property>
        <property name="age" value="23"></property>
     </bean>

     <bean id="stu2" class="com.hzit.entity.Student">
        <property name="id" value="2"></property>
        <property name="name" value="李四"></property>
        <property name="age" value="23"></property>
     </bean>

集合属性通过list标签和ref标签完成注入。ref的bean属性指向需要注入的bean对象。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK