程序员社区

Spring的依赖注入

0.概述

  1. Spring中的依赖注入:
     依赖注入:Dependency Injection
     IOC的作用:降低程序之间的耦合(依赖关系)

  2. 依赖关系的管理:以后都交给Spring来维护
    在当前类需要用到其他类的对象,由Spring为我们提供,我们只需要在配置文件中说明依赖关系的维护,就称为依赖注入。

  3. 能注入的类型有三类:
     基本数据类型和Spring
     其他bean类型(在配置文件中或者注解配置过的bean)
     复杂类型/集合类型

  4. 注入的方式有三种:
     使用构造函数提供
     使用set方法提供
     使用注解提供

1.使用构造函数注入

使用的标签:constructor-org
标签出现的位置:bean标签的内部
标签中的属性:
type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中的某个或某些参数的类型
index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值,索引的位置从0开始
name:用于指定给构造函数中指定名称的参数赋值(常用)
以上三个用于指定给构造函数中哪个参数赋值

value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型的数据,指的是Spring的IOC的核心容器中出现过的bean对象。
优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功
弊端:改变了类的实例化方式,导致如果用不到这些参数也要提供
环境准备:
AccountService:

public interface AccountService {
    void saveAccount();
}
public class AccountServiceImpl implements AccountService{
    private String name;
    private Integer age;
    private Date birthday;
	//提供带参构造方法,那么默认的无参数构造方法便没有了
    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了"+name+","+age+","+birthday);
    }
}
<?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 id="date" class="java.util.Date"/>
    <!--构造函数注入-->
    <bean id="accountService" class="com.hh.service.AccountServiceImpl">
        <constructor-arg name="name" value="zhangsan"/>
        <constructor-arg name="age" value="18"/>
        <constructor-arg name="birthday" ref="date"/>
    </bean>
   
</beans>

在这里插入图片描述

2.使用set方法注入

涉及的标签:property
出现的位置:bean标签的位置
标签的属性:
name:用于指定注入时所调用的set方法名称
value:用于提供基本类型和String类型的数据
ref:用于指定其他bean类型的数据,指的是Spring的IOC的核心容器中出现过的bean对象。
优势:创建对象时没有明确的限制。可以直接使用默认构造函数
弊端:如果某个成员必须有值,则获取对象时有可能set方法没有执行

public interface AccountService {
    void saveAccount();
}
public class AccountServiceImpl2 implements AccountService{
    //如果经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了"+name+","+age+","+birthday);
    }
}
<?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">
    <!--set方法注入-->
    <bean id="accountService2" class="com.hh.service.AccountServiceImpl2">
        <property name="name" value="lisi"/>
        <property name="age" value="21"/>
        <property name="birthday" ref="date"/>
    </bean>
    <bean id="date" class="java.util.Date"/>
</beans>

总结:
可以看成我们java中的创建对象的过程,初始化有两种方式:
1.通过带参数的构造函数(构造函数注入),指定构造函数参数
2.通过set方法(set方法注入),指定set方法的属性参数

3.复杂类型注入

用于List集合注入的标签有:list,array,set
用于Map结构注入的标签:map props

AccountServiceImpl3:

public class AccountServiceImpl3 implements AccountService{
    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String,String> myMap;
    private Properties myProps;

    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }
    public void setMyList(List<String> myList) {
        this.myList = myList;
    }
    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }
    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }
    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }
    public void saveAccount() {
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(myMap);
        System.out.println(mySet);
        System.out.println(myProps);
    }
}

bean3.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="accountService3" class="com.hh.service.AccountServiceImpl3">
    <!--Array-->
    <property name="myStrs">
        <array>
            <value>aaa</value>
            <value>bbb</value>
            <value>ccc</value>
        </array>
    </property>
    <!--List-->
    <property name="myList">
        <list>
            <value>123</value>
            <value>355</value>
            <value>345</value>
        </list>
    </property>
    <!--Set-->
    <property name="mySet">
        <set>
            <value>jianghui</value>
            <value>ai</value>
            <value>hengheng</value>
        </set>
    </property>
    <!--Map-->
    <property name="myMap">
        <map>
            <entry key="jianghui" value="hengheng"/>
            <entry key="zhengshuang" value="zhangheng"/>
        </map>
    </property>
    <!--Properties-->
    <property name="myProps">
        <props>
            <prop key="hengheng">hengheng</prop>
        </props>
    </property>
</bean>
</beans>
public class Client {
    public static void main(String[] args) {
        //获取核心容器
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean3.xml");
        AccountService service = context.getBean("accountService3", AccountService.class);
        service.saveAccount();
    }
}

在这里插入图片描述

赞(0) 打赏
未经允许不得转载:IDEA激活码 » Spring的依赖注入

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区