2019/12/20 ~ 21:黑马Spring学习笔记(一)—— IOC与依赖注入

使用Spring的IOC解决程序耦合

搭建基于xml的Spring开发环境和入门

1. 创建maven工程(不适用骨架)
此时创建好service和dao
在这里插入图片描述
2. 在pom中导入spring ioc需要的依赖

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.0.2.RELEASE</version>
</dependency>

可以查看依赖图:
在这里插入图片描述
在这里插入图片描述

3. 创建配置文件并导入约束(文件名称不限制)

bean.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">
</beans>

4. 把对象的创建交给spring管理
在bean.xml中加入bean标签

<?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管理:
            id: 唯一标识
            class: 全限定类名
     -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>

</beans>

5. 获取spring的IOC容器并根据id获取对象

/**
 * 模拟表现层,用于调用业务层
 */
public class Client {
    /**
     * 获取spring的IOC容器并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {

        // 1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        // 2.根据id获取对象
        IAccountService accountService = (IAccountService) ac.getBean("accountService"); //强转
        IAccountDao accountDao = ac.getBean("accountDao", IAccountDao.class); //传入 类名.class

        System.out.println(accountService);
        System.out.println(accountDao);

    }
}

在这里插入图片描述


ApplicationContext的三个常用实现类

ClassPathXmlApplicationContext
可以加载类路径下的配置文件,配置文件必须放在类路径下

AnnotationConfigApplicationContext
可以加载任意磁盘下的配置文件(必须有访问权限)’
例如上面的例子获取核心容器可以这样

		//ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ApplicationContext ac = new FileSystemXmlApplicationContext("D:\\JavaWebStudy\\day01_eesy_03Spring\\src\\main\\resources\\bean.xml"); 

FileSystemXmlApplicationContext
用于读取注解创建容器


核心容器的两个接口引发的问题

ApplicationContext:(单例对象适用)创建容器的时候,创建对象采取的策略是立即加载。即只要一读取完配置文件就立即创建配置文件中配置的对象。

BeanFactory:(多例对象适用)创建容器的时候,创建对象采取的策略是延迟加载。即什么时候根据id获取对象,什么时候才真正的创建对象。


创建bean的三种方式

第一种方式:使用默认构造函数构建

<?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和class属性之后,且没有其他属性和标签时。
            采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>


</beans>

 
第二种方式:使用普通工厂中的方法创建对象
在com.itheima.factory包下创建一个模拟工厂类InstanceFactory:

package com.itheima.factory;

import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;

/**
 * 模拟一个工厂类,该类存在于jar包,无法通过修改源码的方式来提供默认构造函数
 */
public class InstanceFactory {
    public IAccountService getAccountService(){
        return new AccountServiceImpl();
    }
}

当我们想调用里面的方法创建IAccountService 对象时:

    <!--第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象并存入spring容器)-->
    <bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>

 
第三种方式:使用工厂中静态方法创建对象
在com.itheima.factory包下创建一个模拟工厂类StaticFactory:

package com.itheima.factory;

import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;

/**
 * 模拟一个工厂类,该类存在于jar包,无法通过修改源码的方式来提供默认构造函数
 */
public class StaticFactory {
    public static IAccountService getAccountService(){
        return new AccountServiceImpl();
    }
}

当我们想调用里面的静态方法创建IAccountService 对象时:

<!-- 第三种方式:使用工厂中静态方法创建对象(使用某个类中的静态方法创建对象并存入spring容器)-->
    <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>

bean对象的作用范围
 bean的作用范围调整
            bean标签的scope属性
                作用:用于指定bean的作用范围
                取值:
                    singleton:单例的(默认值)
                    prototype:多例的
                    request:作用于web应用的请求范围
                    session:作用于web应用的会话范围
                    global-session:作用于集群环境的会话范围(全局会话范文),当不是集群环境时它就是session

当我们创建两个bean对象时,他们是一样的,即默认情况下spring的bean对象是单例的

IAccountService accountService1 = (IAccountService) ac.getBean("accountService");
IAccountService accountService2 = (IAccountService) ac.getBean("accountService");

System.out.println(accountService1 == accountService2); // true

修改bean标签的scope属性为prototype,就会bean就会变成多例的,再次运行上诉例子:
在这里插入图片描述
输出为false,即多例


bean对象的生命周期
bean对象的生命周期
            单例对象
                出生:容器创建时对象出生
                存活:只要容器还在,对象就存活
                死亡:容器销毁时,对象也被销毁
                总结:单例对象生命周期和容器相同
            多例对象
                出生:当我们使用对象时spring框架为我们创建
                存活:对象再使用过程中一直活着
                死亡:当对象长时间不用且没有别的对象引用时由java垃圾回收机制回收

Spring中的依赖注入

spring中的依赖注入
        依赖注入:
            Dependency Injection
        IOC的作用:
            降低程序间的耦合(依赖关系)
        依赖关系的管理:
            以后都交给spring维护
        在当前类中需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
        依赖关系的维护:
            称之为依赖注入
         依赖注入:
            能注入的数据有三类:
                基本类型和String
                其他的bean类型(在配置文件中或注解配置过的bean)
                复杂类型/集合类型
            注入的方式有三种
                第一种:使用构造函数提供
                第二种:使用set方法提供
                第三种:使用注解提供 
使用构造函数注入
构造函数注入
        使用的标签:constructor-arg
        标签出现的位置:bean标签内部
        标签中的属性
            type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的数据类型
            index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置从0开始
            name:用于指定给构造函数中指定名称的参数赋值(常用)
            ========================以上三个用于指定给构造函数中哪个参数赋值========================
            value:用于提供基本类型和String类型的数据
            ref:用于指定其他的bean类型数据。它指定就是在spring的ioc核心容器中出现过的bean对象

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

AccountServiceImpl 类中不提供无参构造方法,只有带参构造

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import java.util.Date;

/**
 * 账户业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    // 如果是经常变化的数据,并不适用于注入的方式
    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 saveUser() {
        System.out.println("AccountServiceImpl中的saveUser()方法执行了\n"+name+","+age+","+birthday);
    }


}

此时在spring配置文件中就要使用依赖注入
先说构造函数注入

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="小明"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>

    <!-- 配置一个日期对象 -->
    <bean name="now" class="java.util.Date"></bean>

在这里插入图片描述

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

类AccountServiceImpl2 提供了set方法

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import java.util.Date;

/**
 * 账户业务层实现类
 */
public class AccountServiceImpl2 implements IAccountService {

    // 如果是经常变化的数据,并不适用于注入的方式
    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 saveUser() {
        System.out.println("AccountServiceImpl中的saveUser()方法执行了\n"+name+","+age+","+birthday);
    }

}

在spring配置文件中就要使用set注入

<bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
        <property name="name" value="小红"></property>
        <property name="age" value="18"></property>
        <property name="birthday" ref="now"></property>
</bean>

<bean name="now" class="java.util.Date"></bean>
复杂类型的注入(集合的注入)

AccountServiceImpl3 类中需要给复杂类型注入

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import java.util.*;

/**
 * 账户业务层实现类
 */
public class AccountServiceImpl3 implements IAccountService {

    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 saveUser() {
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);
    }

}

<!--
        复杂类型注入/集合类型的注入
            用于给List结构集合注入的标签
                list array set
            用于给Map结构集合注入的标签
                map props
            结构相同,标签可以互换
    -->
    <bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
		 <!-- 数组注入 -->
        <property name="myStrs">
            <array>
                <value>array</value>
                <value>array</value>
                <value>array</value>
            </array>
        </property>
        <!--List注入-->
        <property name="myList">
            <list>
                <value>list</value>
                <value>list</value>
                <value>list</value>
            </list>
        </property>
        <!--Set注入-->
        <property name="mySet">
            <set>
                <value>set1</value>
                <value>set2</value>
                <value>set3</value>
            </set>
        </property>
        <!--Map注入-->
        <property name="myMap">
            <map>
                <entry key="map1" value="map1"></entry>
                <entry key="map2" value="map2"></entry>
                <entry key="map3">
                    <value>map3</value>
                </entry>
            </map>
        </property>
        <!--Properties注入-->
        <property name="myProps">
            <props>
                <prop key="prop1">prop1</prop>
                <prop key="prop2">prop2</prop>
            </props>
        </property>
    </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值