Spring IOC——依赖注入

spring中的依赖注入

IOC作用:降低程序间的耦合(依赖关系)
依赖关系的管理:交给spring来维护(在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明)
依赖关系的维护:依赖注入
依赖注入:
能注入的数据:

  1. 基本类型和String
  2. 其他bean类型(在配置文件中或者注解配置过的bean)
  3. 复杂类型/集合类型

bean对象 注入的方式:

  1. 使用构造函数
  2. 使用set函数
  3. 使用注解

使用构造函数

构造函数往入: |
使用的标签:constructor- arg
标签出现的位置: bean标签的内部
标签中的属性
type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从e开始
name:用于指定给构造函数中指定名称的参数赋值常用的
=========以上三个用于指定给构造函数中哪个参效赋值
value: 用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的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">
    <bean id="accountService" class="com.ay.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="飞扬"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>
    <bean id="now" class="java.util.Date" ></bean>
</beans>
package com.ay.service;

public interface AccountService {
    public  void saveAccount();
}

package com.ay.service.impl;

 import com.ay.service.AccountService;

 import java.util.Date;

public class AccountServiceImpl implements AccountService {
    private String name;
    private Integer age;
    private Date birthday;
    @Override
    public void saveAccount() {
        System.out.println("方法创建成功了");
    }

    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "AccountServiceImpl{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}

package com.ay.ui;

import com.ay.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as = (AccountService)ac.getBean("accountService");
        as.saveAccount();
        System.out.println(as.toString());
    }
}

在这里插入图片描述
总结:
优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。

使用set函数

涉及的标签: property
出现的位置: bean标签的内部
标签的属性
name:用于指定往入时所调用的set方法名称
value: 用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象

package com.ay.service;

public interface AccountService {
    public  void saveAccount();
}

package com.ay.service.impl;

 import com.ay.service.AccountService;

 import java.util.Date;

public class AccountServiceImpl implements AccountService {
    private String name;
    private Integer age;
    private Date birthday;
    @Override
    public void saveAccount() {
        System.out.println("方法创建成功了");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "AccountServiceImpl{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}

package com.ay.ui;

import com.ay.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as = (AccountService)ac.getBean("accountService");
        as.saveAccount();
        System.out.println(as.toString());
    }
}

<?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="accountService" class="com.ay.service.impl.AccountServiceImpl">
        <property name="name" value="飞扬"></property>
        <property name="age" value="20"></property>
        <property name="birthday" ref="now"></property>
     </bean>
    <bean id="now" class="java.util.Date" ></bean>
</beans>

在这里插入图片描述
总结:
优势:创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行。

发布了8 篇原创文章 · 获赞 27 · 访问量 330

猜你喜欢

转载自blog.csdn.net/qq_44706044/article/details/103988392