Spring 基于 XML 的 IOC配置


一、 spring基于 XML 的IOC入门

Inverse of Control 反转控制,把创建对象的权利交给框架,包括依赖注入和依赖查找。
以前对象的创建时由我们开发人员自己维护,包括依赖关系也是自己注入。
使用了Spring之后,对象的创建及依赖关系可以由Spring完成创建以及注入。
反转控制就是反转了对象的创建方式,从我们自己创建反转给了程序(Spring)。


//User对象
import java.util.*;

public class User {

    private Integer id;
    private String username;
    private Character sex;
    private Date birthday;
	
	public User() {
    }
	
	public User(Integer id, String username, Character sex, Date birthday) {
        this.id = id;
        this.username = username;
        this.sex = sex;
        this.birthday = birthday;
    }
	
	//getter and setter此处省略
}

1. 引入依赖
  <!--引入spring 的最核心依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        
2.创建UserDao接口和UserDaoImpl实现类。

3. 创建beans.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
http://www.springframework.org/schema/beans: 引入bean的名称空间
约束:现在xml的书写
    dtd:mybatis
    schema:spring

http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 引入约束
-->
<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="userDao" class="com.testfan.dao.impl.UserDaoImpl"></bean>

</beans>

4. 创建容器对象,根据id获取对象
 //创建spring的IOC容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        //获取对象
        Object userDao = ac.getBean("userDao");
        System.out.println(userDao);

二、IOC 中 bean 标签和管理对象细节

2.1 bean 标签

作用:用于配置对象让 spring 来创建的。默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。

属性:

  • id: 给对象在容器中提供一个唯一标识。用于获取对象。
  • class: 指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
  • scope: 指定对象的作用范围。
    • singleton :默认值,单例的.
    • prototype :多例的.
    • request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
    • session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.
    • global session :WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么globalSession 相当于 session.
  • init-method: 指定类中的初始化方法名称。
  • destroy-method: 指定类中销毁方法名称

2.2 bean 的作用范围和生命周期

  • 单例对象: scope="singleton"

    一个应用只有一个对象的实例。它的作用范围就是整个引用。
    生命周期:
       对象出生:当应用加载,创建容器时,对象就被创建了。
       对象活着:只要容器在,对象一直活着。
       对象死亡:当应用卸载,销毁容器时,对象就被销毁了。

  • 多例对象: scope="prototype"

    每次访问对象时,都会重新创建对象实例。
    生命周期:
       对象出生:当使用对象时,创建新的对象实例。
       对象活着:只要对象在使用中,就一直活着。
       对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。

2.3 实例化 Bean 的三种方式

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

<!--在默认情况下:
它会根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败。
-->
<bean id="accountService" class="com.testfan.service.impl.AccountServiceImpl"/>

第二种方式: spring 管理静态工厂-使用静态工厂的方法创建对象

/**
* 模拟一个静态工厂,创建业务层实现类
*/
public class StaticFactory {
	public static IAccountService createAccountService(){
		return new AccountServiceImpl();
	}
}
<!-- 此种方式是:
使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器
id 属性:指定 bean 的 id,用于从容器中获取
class 属性:指定静态工厂的全限定类名
factory-method 属性:指定生产对象的静态方法
-->
<bean id="accountService" class="com.testfan.factory.StaticFactory"
factory-method="createAccountService"></bean>

第三种方式: spring 管理实例工厂-使用实例工厂的方法创建对象

/**
* 模拟一个实例工厂,创建业务层实现类
* 此工厂创建对象,必须现有工厂实例对象,再调用方法
*/
public class InstanceFactory {
	public IAccountService createAccountService(){
		return new AccountServiceImpl();
	}
}
<!-- 此种方式是:
先把工厂的创建交给 spring 来管理。然后在使用工厂的 bean 来调用里面的方法
factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。
-->
<bean id="instancFactory" class="com.testfan.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instancFactory"
factory-method="createAccountService"></bean>

三、DI

Dependency Injection依赖注入,实现IoC思想需要DI做支持。 它是 spring 框架核心 IoC的具体实现。
我们的程序在编写时, 通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。IoC解耦只是降低他们的依赖关系,但不会消除。 例如:我们的业务层仍会调用持久层的方法。那这种业务层和持久层的依赖关系, 在使用 spring 之后, 就让 spring 来维护了。简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。


3.1 注入方式

  • set方法注入【常用推荐】
	<bean id="birthday" class="java.util.Date"></bean>
    <!--通过set方法注入-->
    <bean id="user" class="com.testfan.domain.User">
        <!--property :属性注入 , 先找到setter方法,才能最终找到属性-->
        <property name="username" value="张三"></property>
        <property name="birthday" ref="birthday"></property>
        <property name="sex" value=""></property>
        <property name="id" value="5"></property>
    </bean>
  • 构造方法注入
    <!--默认创建对象方式,使用默认的空的构造方法创建对象-->
    <bean id="user" class="com.testfan.domain.User">
        <!--3.通过构造方法参数的索引赋值-->
        <!--<constructor-arg index="0" value="1"></constructor-arg>-->
        <!--<constructor-arg index="1" value="张三"></constructor-arg>-->
        <!--2.通过构造方法参数类型赋值-->
        <!--<constructor-arg type="java.lang.Integer" value="2"></constructor-arg>-->
        <!--<constructor-arg type="java.lang.String" value="李四"></constructor-arg>-->
        <!--1.通过构造方法参数名字赋值【重要】-->
        <constructor-arg name="id" value="3"></constructor-arg>
        <constructor-arg name="username" value="王五"></constructor-arg>
   		<constructor-arg name="sex" value=""></constructor-arg>
        <constructor-arg name="birthday" ref="birthday"></constructor-arg>
    </bean>

	<bean id="birthday" class="java.util.Date"></bean>
  • 字段注入
  • 注入集合属性【常用】
import java.util.*;

public class User {

    private List<String> list;
    private Set<String > set;
    private String[] strs;

    private Map<String ,String> map;
    private Properties properties;
	
	public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }
	
	public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }
	
	public String[] getStrs() {
        return strs;
    }

    public void setStrs(String[] strs) {
        this.strs = strs;
    }
	
    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
 
}

<!--注入集合属性-->
    <bean id="user4" class="com.testfan.domain.User">
        <!--array ,list ,set 结构相同,标签可以混用-->
        <property name="list">
            <set>
                <value>javaSE</value>
                <value>javaEE</value>
                <value>javaME</value>
            </set>
        </property>

        <property name="set">
            <array>
                <value>javaSE</value>
                <value>javaEE</value>
                <value>javaME</value>
            </array>
        </property>
        <property name="strs">
            <list>
                <value>javaSE</value>
                <value>javaEE</value>
                <value>javaME</value>
            </list>
        </property>
        
        <!--map集合properties结构相同,可以通用-->
        <property name="map">
           <props>
            <prop key="" >five</prop>
            <prop key="" >six</prop>
            <prop key="" >seven</prop>
        </props>
        </property>

        <property name="properties">
            <map>
                <!--键值对配置-->
                <entry key="" value="one"></entry>
                <entry key="" value="two"></entry>
                <entry key="" value="three"></entry>
                <entry key="" value="four"></entry>
            </map>
        </property>
    </bean>

3.2 注入类型

  • 值类型注入:8大基本数据类型使用此注入类型
  • 引用类型注入:将依赖对象注入使用此注入类型
    <!--
        value 属性只能赋值简单的类型:基本数据类型和String类型
        ref:  pojo类型,复杂类型, 关联创建好的对象
    -->
    <bean id="birthday" class="java.util.Date"></bean>
    <!--通过set方法注入-->
    <bean id="user" class="com.testfan.domain.User">
        <!--property :属性注入 , 先找到setter方法,才能最终找到属性-->
        <property name="username" value="张三"></property>
        <property name="birthday" ref="birthday"></property>
        <property name="sex" value=""></property>
        <property name="id" value="5"></property>
    </bean>

四、applicationContext&BeanFactory

Spring中的工厂类

  • applicationContext(现在使用)
  • BeanFactory(早年使用)

4.1 spring 中工厂的类结构图

在这里插入图片描述在这里插入图片描述

spring 中工厂的类结构图

4.2 BeanFactory接口

BeanFactory接口是 Spring 容器中的顶层接口,针对原始接口的实现类功能较为单一。
BeanFactory接口实现类的容器,特点是每次在获得对象时才会创建对象,即什么时候使用什么时候创建
这主要是因为,BeanFactory是早年使用的接口,当时硬件资源比较匮乏,内存配置低,所以当时设计的框架资源使用比较节约。


4.3 ApplicationContext接口

ApplicationContext 是BeanFactory接口的子接口。
ApplicationContext接口实现类的容器,特点是每次容器启动时就会创建容器中配置的所有对象,并提供更多功能


典型的实现类

  • ClassPathXmlApplicationContext:它是从类的根路径下加载配置文件 推荐使用这种
  • FileSystemXmlApplicationContext:它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
  • AnnotationConfigApplicationContext:当我们使用注解配置容器对象时,需要使用此类来创建 spring容器。它用来读取注解。

4.4 BeanFactory 和 ApplicationContext 的区别

创建对象的时间点不一样。
ApplicationContext:只要一读取配置文件, 默认情况下就会创建对象。
BeanFactory:什么使用什么时候创建对象。

结论:web开发中,使用applicationContext. 在资源匮乏的环境可以使用BeanFactory.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值