SpringDI(依赖注入)的理解

目录

一、什么是SpringDI?

二、SpringDI(依赖注入)的作用是什么?

三、DI的实现方式

3.1 set注入

1.项目结构:

2.set注入对象

2.1applicationContext.xml:

 2.2 Student 实体类

 2.3 controller:

 2.4 dao: IUserDao:

2.5  service:IUserService:

2.6 Test01:

3. set注入基本类型与String

3.1  applicationContext.xml:

 4. set注入复杂类型

4.1  applicationContext.xml:

 4.2 Teacher实体类:

4.3 Text01:

3.2 构造注入

 1.applicationContext.xml:

2. Student:

3.3 注解注入

四、DI数据类型

五、DI使用步骤


一、什么是SpringDI?

DI(Dependecy Inject,中文释义:依赖注入)  是对IOC概念的不同角度的描述,是指应用程序在运行时,每一个bean对象都依赖IOC容器注入当前bean对象所需的另外一个bean对象。

(例如在 MyBatis 整合Spring 时,SqlSessionFactoryBean 依赖| IOC容器注入一个D
ataSource数据源bean ) 

二、SpringDI(依赖注入)的作用是什么?

作用:将springioc 容器所创建的各个组件,使用DI的语法进行关联,耦合(胶水)

三、DI的实现方式

1.set注入  2.构造注入  3.注解注入

3.1 set注入

语法:

        1.set方法
        2. set配置
                <property name value ref>

实现:

1.项目结构:

 

2.set注入对象
2.1applicationContext.xml:
    <!--name 属性代表的是Java类中的成员变量或属性名,它对应于类中的setter方法。-->
    <!--ref 属性用于指定要注入的依赖bean的名称。-->
    <!--通过 ref 属性,Spring可以将一个bean的实例注入到另一个bean的属性中,实现依赖注入。-->

<!--    注入数据访问-->
    <bean id="daoImp" class = "com.zad.dao.UserDaoImpl"></bean>
<!--    注入业务-->
    <bean id="serviceImp" class="com.zad.service.UserServiceImp">
        <!--将 serviceImp 这个bean注入到 controllerImp 中。-->
        <!--property相当于set方法-->
        <!--依赖注入-->
        <property name="dao" ref="daoImp"></property>
    </bean>

<!--   注入控制器-->
    <bean id="controllerImp" class="com.zad.controller.UserControllerImpl">
        <property name="service" ref="serviceImp"></property>
    </bean>
 2.2 Student 实体类
public class Student {
    private  String stuName;
    private int stuAge;
    private String stuHobby;


    /*****************set注入********************/
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public void setStuHobby(String stuHobby) {
        this.stuHobby = stuHobby;
    }
    /*****************set注入********************/
    @Override
    public String toString() {
        return "Student{" +
                "stuName='" + stuName + '\'' +
                ", stuAge=" + stuAge +
                ", stuHobby='" + stuHobby + '\'' +
                '}';
    }
}
 2.3 controller:

 IUserController:

public interface IUserController {
    public void save();
}

  UserControllerImpl:

public class UserControllerImpl implements IUserController {
    IUserService service ;

    public void setService(IUserService service){
        this.service = service;
    }

    @Override
    public void save() {
        System.out.println("===controller的新增方法===");
        service.save();

    }
}
 2.4 dao: IUserDao:
public interface IUserDao {
    public void save();
}

  UserDaoImpl:

public class UserDaoImpl implements IUserDao{

    @Override
    public void save() {
        System.out.println("===dao的新增方法===");
    }
}
2.5  service:IUserService:
public interface IUserService {
    public void save();
}

 UserServiceImp:

public class UserServiceImp implements IUserService{

    IUserDao dao ;

    public void setDao(IUserDao dao){
        this.dao = dao;
    }

    @Override
    public void save() {
        System.out.println("===service的新增方法===");
        dao.save();
    }
}
2.6 Test01:
public class Text01 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserController controller = (IUserController)applicationContext.getBean("controllerImp");
        Student student = (Student) applicationContext.getBean("student");
        controller.save();
        System.out.println(student);
      controller.save();

    }
}

结果:

===controller的新增方法===
===service的新增方法===
===dao的新增方法===
Student{stuName='zwy', stuAge=18, stuHobby='学习'}
3. set注入基本类型与String
3.1  applicationContext.xml:
    <bean id="student" class="com.zad.pojo.Student">
        <property name="stuName" value="zwy"/>
        <property name="stuAge" value="18"/>
        <property name="stuHobby" value="学习"/>
    </bean>

其他层同上:

结果:

===controller的新增方法===
===service的新增方法===
===dao的新增方法===
Student{stuName='zwy', stuAge=18, stuHobby='学习'}
 4. set注入复杂类型
4.1  applicationContext.xml:
<bean id="teacher" class="com.zad.pojo.Teacher">
        <property name="myList">
            <list>
                <value>老米家泡馍</value>
                <value>红红炒米</value>
                <value>马昆砂锅</value>
                <value>石家包子</value>
            </list>
        </property>
        <property name="array">
            <array>
                <value>游戏</value>
                <value>跑步</value>
                <value>谈恋爱</value>
                <value>学习S</value>
            </array>
        </property>
        <property name="mySet">
            <set>
                <value>茶话弄</value>
                <value>茶百道</value>
                <value>蜜雪冰城</value>
                <value>霸王茶姬</value>
                <value>茉莉奶绿</value>
            </set>
        </property>
        <property name="myMap">
            <map>
                <entry key="杨文琪" value="心动"/>
                <entry key="贺佳瑶" value="小贺"/>
                <entry key="张甜甜" value="小猪佩奇"/>
                <entry key="林航宇" value="林"/>
            </map>
        </property>
        <property name="myProp">
            <props>
                <prop key="法国">巴黎</prop>
                <prop key="英国">伦敦</prop>
                <prop key="日本">东京</prop>
                <prop key="美国">华盛顿</prop>
                <prop key="俄罗斯">乌克兰</prop>
            </props>
        </property>
    </bean>
 4.2 Teacher实体类:
public class Teacher {
    private List myList;
    private Set mySet;
    private String[] array;
    private Map myMap;
    private Properties myProp;

    public void setMyList(List myList) {
        this.myList = myList;
    }

    public void setMySet(Set mySet) {
        this.mySet = mySet;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    public void setMyMap(Map myMap) {
        this.myMap = myMap;
    }

    public void setMyProp(Properties myProp) {
        this.myProp = myProp;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "myList=" + myList +
                ", mySet=" + mySet +
                ", array=" + Arrays.toString(array) +
                ", myMap=" + myMap +
                ", myProp=" + myProp +
                '}';
    }
}
4.3 Text01:
public class Text01 {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserController controller = (IUserController)applicationContext.getBean("controllerImp");

        Teacher teacher = (Teacher) applicationContext.getBean("teacher");
        controller.save();
        System.out.println(teacher);


    }
}

结果:

===controller的新增方法===
===service的新增方法===
===dao的新增方法===
Teacher{myList=[老米家泡馍, 红红炒米, 马昆砂锅, 石家包子], mySet=[茶话弄, 茶百道, 蜜雪冰城, 霸王茶姬, 茉莉奶绿], array=[游戏, 跑步, 谈恋爱, 学习S], myMap={杨文琪=心动, 贺佳瑶=小贺, 张甜甜=小猪佩奇, 林航宇=林}, myProp={法国=巴黎, 俄罗斯=乌克兰, 英国=伦敦, 日本=东京, 美国=华盛顿}}
3.2 构造注入

1.构造方法
2.构造配置
        <constructor-arg name type index value ref>

 1.applicationContext.xml:
    <bean id="student2" class="com.zad.pojo.Student">
        <constructor-arg name="stuName" value="xn"/>
        <constructor-arg name="stuAge" value="18"/>
        <constructor-arg name="stuHobby" value="睡觉"/>
    </bean>
    <bean id="student3" class="com.zad.pojo.Student">
        <constructor-arg  type="java.lang.String" value="xn"/>
        <constructor-arg  type="int" value="18"/>
        <constructor-arg  type="java.lang.String" value="睡觉"/>
    </bean>
    <bean id="student4" class="com.zad.pojo.Student">
        <constructor-arg index="0" value="xn"/>
        <constructor-arg index="1" value="18"/>
        <constructor-arg index="2" value="睡觉"/>
    </bean>

<!--   注入控制器-->
    <bean id="controllerImp2" class="com.zad.controller.UserControllerImpl">
        <property name="service" ref="serviceImp2"></property>
    </bean>
    <!--    注入业务-->
    <bean id="serviceImp2" class="com.zad.service.UserServiceImp">
        <property name="dao" ref="daoImp2"></property>
    </bean>
    <!--    注入数据访问-->
    <bean id="daoImp2" class = "com.zad.dao.UserDaoImpl"></bean>
2. Student:
public class Student {
    private  String stuName;
    private int stuAge;
    private String stuHobby;
    /*****************构造注入********************/
    public Student(String stuName, int stuAge, String stuHobby) {
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.stuHobby = stuHobby;
    }

    public Student() {
    }
}

3. Text01:

public class Text01 {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        Student student2 = (Student) applicationContext.getBean("student2");
        Student student3 = (Student) applicationContext.getBean("student3");
        Student student4 = (Student) applicationContext.getBean("student4");
        System.out.println(student2);
        System.out.println(student3);
        System.out.println(student4);

        IUserController controller = (IUserController) applicationContext.getBean("controllerImp2");
        controller.save();

    }
}

结果:

Student{stuName='xn', stuAge=18, stuHobby='睡觉'}
Student{stuName='xn', stuAge=18, stuHobby='睡觉'}
Student{stuName='xn', stuAge=18, stuHobby='睡觉'}
===controller的新增方法===
===service的新增方法===
===dao的新增方法===
3.3 注解注入

四、DI数据类型

1.基本类型与String
2.JavaBean
3.复杂类型,list、 set、 array、 map、 properties ( 构造注入不支持)

五、DI使用步骤

1.思考,什么方式,什么数据类型
2.给属性提供set(构造)方法
3.编写配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值