【Spring 框架(自学)】Day02--2022/3/21

基于XML的DI

IOC的技术实现之di(依赖注入)

di:依赖注入,表示创建对象,给属性赋值


di的实现有两种

  • 在Spring的配置中,使用标签和属性完成,叫做基于XML的di实现
  • 使用Spring中的注解,完成属性赋值,叫做基于注解的di实现

di的语法分类

  • set注入(设置注入):Spring调用类的set方法,在set方法中可以实现属性的赋值(80%都是使用set注入)
  • 构造注入:Spring调用类的有参构造方法,创建对象,在构造方法中完成赋值

set注入:

//创建Student实体类
public class Student{
    private String name;
    private int age;
    private School school;//创建引用数据类型
    
    //创建setter()方法以及toString()方法
    public void setSchool(School school){
        sout("setSchool=="+school);
        this.school = school;
    }
}
//创建School实体类
public class School{
    private String name;
    private String address;
    
    //创建setter()方法和toString()方法
}
<!--头文件-->

<!--声明Student对象
        简单类型:spring中规定java的基本数据类型和string都是简单类型
        di:给属性赋值

        1.set注入(设置注入):spring调用类的set方法,可以在set方法中完成属性赋值

            1)简单类型的注入
            <bean id="xx" class="yy">
                <property name="属性的名字" value="此属性的值">
                <property...>
            </bean>
        需要注意的是 set方法通过反射去找到pojo类中的set方法并实现属性的赋值,所以pojo类中必须要		setter()方法

            2)引用数据类型的set注入
            <bean id="xx" class="yy">
                <property name="属性的名字" value="此属性的值">
                <property name="属性的名字" ref="bean的id(也就是对象名)">
            </bean>
-->




	<!--给student实体类set值-->
<bean id="myStudent" class="xx.yy.实体类文件">
	<property name="实体类中属性名" value="设置值"></property>
    <property name="实体类中属性名" value="设置值"></property>
    <property name="实体类中属性名" ref="mySchpool"></property> <!--bean的id(对象的名称)-->
</bean>

	<!--给school实体类set值-->
<bean id="mySchool" class="xx.yy.实体类文件">
    <property......></property>
    <property......></property>
</bean>
//Junit单元测试
public class StudentDaoTest{
    
    @Test
    public void test(){
        String config ="xx/yy/xml文件"
        ApplicationContext ac = new ClassPathAppliCationContext(config);
        Student student = (Student) ac.getBean("myStudent");
        sout(student);
    }
}

构造注入:

<!--头文件-->

<!--构造注入
        Spring调用类有参数构造方法,在创建对象的同时,在构造方法中给属性赋值
        构造注入使用<constructor-arg>标签
        <constructor-arg>属性:
        name:表示构造方法的形参名
        index:表示构造方法参数从左往右的形参个数,通常是下标0,1,2
        value:简单数据类型,赋值
        ref:引用数据类型,ref传递bean的id
    -->
<bean id="myStudent" class="com.spring.Pojo2.Student">
    <constructor-arg name="name" value="张三"></constructor-arg>
    <constructor-arg index="1" value="77"></constructor-arg>
    <constructor-arg name="school" ref="mySchool"></constructor-arg>
</bean>

    <bean id="mySchool" class="com.spring.Pojo2.School">
        <constructor-arg index="0" value="构造"></constructor-arg>
        <constructor-arg name="address" value="武汉"></constructor-arg>
    </bean>

<!--除此之外也可以用省略index的写法来传参:可读性不高-->
<bean id="myStudent" class="com.spring.Pojo2.Student">
    <constructor-arg  value="张三"></constructor-arg>
    <constructor-arg  value="77"></constructor-arg>
    <constructor-arg  ref="mySchool"></constructor-arg>
</bean>
posted @   VVS,冷漠的心  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

囚鸟该如何超越今生?

点击右上角即可分享
微信分享提示