Spring IoC之DI实现XML配置形式

本文详细介绍了Spring的DI(依赖注入)概念,通过XML配置方式演示了bean的定义、属性注入、自动装配等操作,并探讨了初始化方法、销毁方法、不同scope的影响,同时讲解了如何引入外部配置文件。最后提到了JavaConfig和注解方式作为其他实现DI的途径。
摘要由CSDN通过智能技术生成

DI

什么是DI

DI 及依赖注入(Dependency Injection),实现IOC设计原则的一种方式

如何以DI方式开发

XML配置的方式

测试环境请移步这里

XML标签约束
bean标签
bean 标签的属性

一个Book类,用来测试

public class Book {
    private int bId;
    private String bName;
    private Date createDate;
    private String[] cite;
    private List press;
    private Map authors;
    private Set catalogue;
    public Book() {
        System.out.println("this is book no arg constructor");
    }
    public Book(int bId) {
        this.bId = bId;
    }
    public Book(int bId, String bName) {
        System.out.println("this is book arg constructor,bid:"+bId+",bName:"+bName);
        this.bId = bId;
        this.bName = bName;
    }
    public Book(int bId, String bName, Date createDate) {
        System.out.println("this is book arg constructor");
        this.bId = bId;this.bName = bName;this.createDate = createDate;
    }
    public void setbId(int bId) {
        System.out.println("invoke book set method");this.bId = bId;
    }
    public void bookInit(){
        System.out.println("invoke book init method");
    }
    public void bookDestroy(){
        System.out.println("invoke book destroy method");
    }
   /** bName createDate 为了减少展示太多代码 set方法 和 book toString 方法在这里删除了 **/
}
id,class 属性
<!-- spring xml 配置 resources/application.xml -->
<!--
	id:给要管理的bean编号,与name相同
 	class:管理bean的路径
-->
<bean id="book1" class="com.test.springdome01.Book"></bean>
/** 这里展示测试的代码 之后只展示测试类容 **/
public class Test {
    private ApplicationContext cpxac = new ClassPathXmlApplicationContext("application.xml");
    @org.junit.Test
    public void test01(){
        System.out.println(cpxac.getBean("book1"));
    }
}

在这里插入图片描述

结论:以上可以看出调用的是Book类的无参构造方法

autowire属性

请看自动装配位置

scope属性

spring_context 下 scope 属性只包含两个,一个singleton单例,一个prototype原型形式

<bean id="book1" class="com.test.springdome01.Book" scope="singleton"></bean>
<bean id="book2" class="com.test.springdome01.Book" scope="prototype"></bean>
System.out.println("singleton getBean 两次引用是否相等:"+ (cpxac.getBean("book1", Book.class) == cpxac.getBean("book1",Book.class)));
        System.out.println("prototype getBean 两次引用是否相等:"+ (cpxac.getBean("book2", Book.class) == cpxac.getBean("book2",Book.class)));

在这里插入图片描述

结论:singleton:仅生成id为book1的bean一次

​ prototype:每次getBean是都会重新创建新的book2对象

lazy-init

初始化的策略是否懒加载

<bean id="book3" class="com.test.springdome01.Book" lazy-init="true">
	<property name="bId" value="1"></property>
</bean>
private ApplicationContext cpxac = new ClassPathXmlApplicationContext("application.xml");
@org.junit.Test
    public void test03(){
        System.out.println("invoke test03");
    }

在这里插入图片描述

<bean id="book3" class="com.test.springdome01.Book" lazy-init="false">
	<property name="bId" value="1"></property>
</bean>

测试代码相同
在这里插入图片描述

结论: lazy-init="true"时,意为懒加载:及spring容器第一次只生成无参对象,只到调用getBean时才设置参数

​ lazy-init="false"或者default时,意为饿汉模式:及spring容器直接根据bean配置生成对象

init-method,destroy-method 属性

给bean配置初始化方法和销毁方法

请看spring生命周期

注入属性
添加属性方法

property子标签

<bean id="book4" class="com.test.springdome01.Book">
    <property name="bId" value="100"></property>
</bean>
 System.out.println("invoke test04");
 System.out.println(cpxac.getBean("book4", Book.class));
通过有参构造器添加属性
<bean id="book5" class="com.test.springdome01.Book">
        <constructor-arg name="bId" value="101"></constructor-arg>
        <constructor-arg index="1" value="Spring"></constructor-arg>
</bean>
System.out.println("invoke test05");
System.out.println(cpxac.getBean("book5", Book.class));

在这里插入图片描述

P/C标签的含义

简化了 property 和 constructor-arg

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 添加p/c 标签的约束 -->
    <!-- p标签 = property  -->
    <bean id="book6" class="com.test.springdome01.Book" p:bId="1" p:bName="springboot"></bean>
    <!-- c标签 = constructor-arg -->
    <bean id="book7" class="com.test.springdome01.Book" c:_0="1" c:_1="JVM"></bean>
</beans>
System.out.println(cpxac.getBean("book6", Book.class));
System.out.println(cpxac.getBean("book7", Book.class));

在这里插入图片描述

引用对象引入

**ref是对引用对象进行赋值 **

<bean id="date" class="java.util.Date"></bean>
<bean id="book8" class="com.test.springdome01.Book">
    <property name="createDate" ref="date"></property>
</bean>
 System.out.println(cpxac.getBean("book8", Book.class));
arry、list、set、map对象注入
<bean id="book9" class="com.test.springdome01.Book">
    <property name="cite">
        <array>
            <value>Spring.org</value>
            <value>JAVA.org</value>
            <value>JAVAEE.org</value>
        </array>
    </property>
    <property name="press">
        <list>
            <value>中国xxx出版社</value>
            <value>美国xxx出版社</value>
        </list>
    </property>
    <property name="catalogue">
        <set>
            <value>第一章xxx</value>
            <value>第二章xxx</value>
        </set>
    </property>
    <property name="authors">
        <map>
            <entry key="作者1" value="小明"></entry>
            <entry key="作者2" value="小红"></entry>
        </map>
    </property>
</bean>
System.out.println(cpxac.getBean("book9", Book.class));
util标签

基于集合类型进行封装

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd">
 <!-- 添加util标签的约束 -->
    <util:list id="presslist" >
        <value>中国xxx出版社</value>
        <value>美国xxx出版社</value>
    </util:list>
    <bean id="book10" class="com.test.springdome01.Book">
        <property name="press" ref="presslist"></property>
    </bean>
</beans>
System.out.println(cpxac.getBean("book10", Book.class));
自动装配

bean的属性autowire

spring在创建bean时,会根据spring容器中存在的bean进行对属性装配

创建一个Books类

public class Books {
    private Book book1;
	// 给他添加 无参构造 set方法 toString方法
}
<bean id="book1" class="com.test.springdome01.Book"></bean>
<bean id="books1" class="com.test.springdome01.Books" autowire="byName"></bean>
<!--
	<bean id="book" class="com.test.springdome01.Book"></bean>
    <bean id="books1" class="com.test.springdome01.Books" autowire="byType"></bean>
-->

注意:参数为 byName时,Books中的属性名book1必须和配置中Book的id相同

如何将配置文件引入Spring的XML配置文件中

**创建resources/book.propertiser文件

id=102
name=spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
 	<!-- 添加context标签的约束 -->
    <context:property-placeholder location="book.propertiser"></context:property-placeholder>
    <bean id="book12" class="com.test.springdome01.Book">
        <property name="bId" value="${id}"></property>
        <property name="bName" value="${name}"></property>
    </bean>
</beans>
System.out.println(cpxac.getBean("book12", Book.class));

其他方式

后续更新

更新

  • 2020-1-1 更新

结语

你好!^ _ ^ 谢谢你能看到这里
如果你对文章类容有不同看发或文章类容存在错误,请在下方评论处指出,谢谢^ _ ^
你好!如果文章符合你的胃口,你是否愿意点个赞呢?
你好!如果文章对你有帮助,你不妨分享出去,让更多人成长!
如果文章让你喜欢,不妨点个关注交个朋友,一起共同学习!
谢谢,你的点赞,关注,分享是对我写文章不小的动力!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值