Spring基础:XML方式实现DI
Posted Lansonli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring基础:XML方式实现DI相关的知识,希望对你有一定的参考价值。
文章目录
XML方式实现DI
一、管理的内容概念讲解
1、spring中的Bean的管理
Bean(汉译咖啡豆)又称JAVABean,其实就是JAVA程序程序中的一个个对象,所以Bean的管理其实就是spring对于JAVA程序中的对象的管理
2、对象的创建IOC
IOC叫做控制反转,就是Spring给我们创建对象,然后我们直接用,不用自己NEW,前面已经解释过
IOC处理的是对象如何创建的问题
3、属性的赋值DI
DI Dependency Injection,即“依赖注入” 就是创建属性时给对象属性赋值
对象功能的实现往往要依赖属性的值,那么给对象属性赋值就可以说成是依赖注入
由于对象属性不仅仅是基本数据类型,还可能是其他类,或者引用类型
那么依赖注入将会把更多的对象之间的关系整理到一起,可以行程一个庞大的依赖关系
DI处理的是对象的属性赋值和互相依赖的关系
4、spring给我们提供了两种关于bean的方式
- 基于XML方式的Bean管理
- 基于注解方式的Bean管理
二、实现DI实战案例
1、创建新的模块并导入以下依赖
依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
2、创建spring配置文件
一般spring的配置文件很多人约定俗称为application****.xml
3、准备一个要实例化的类
public class User
private Integer userid;
private String username;
private String password;
@Override
public String toString()
return "User" +
"userid=" + userid +
", username='" + username + '\\'' +
", password='" + password + '\\'' +
'';
public User()
System.out.println("noArgConstructor");
public User(Integer userid, String username, String password)
System.out.println("allArgConstructor");
this.userid = userid;
this.username = username;
this.password = password;
public void setUserid(Integer userid)
System.out.println("setUserid");
this.userid = userid;
public void setUsername(String username)
System.out.println("setUsername");
this.username = username;
public void setPassword(String password)
System.out.println("setpassword");
this.password = password;
4、IOC创建对象
通过无参构造方法构造对象
<bean id="user1" class="com.lanson.bean.User"></bean>
<bean> 标签的常见属性
<bean id="user1" class="com.lanson.bean.User" name="user1" scope="prototype" lazy-init="true" ></bean>
- id 对象的id
- class 类的全路径名
- name 和id类似,一般不用
- scope 控制对象单例多例和使用范围
- singleton作用域(scope 默认值), Spring IOC容器中只会存在一个共享的bean实例
- prototype作用域部署的bean,每一次获取都会产生一个新的bean实例,相当与一个new的操作
- request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效
- session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效
- global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义
- lazy-init 懒加载 调用getBean的时候再去实例化对象
5、DI给对象属性赋值
5.1、通过set方法给对象属性赋值
<!--property 就是在使用set方法实现依赖注入-->
<bean id="user1" class="com.lanson.bean.User">
<property name="userid" value="1"></property>
<property name="username" value="张三"></property>
<property name="password" value="abcdefg"></property>
</bean>
5.2、通过有参构造给对象属性赋值
<!--
constructor-arg 就是在使用构造方法实现依赖注入
constructor-arg 的个数必须和某个构造方法的参数个数向对应
name指的是参数名
index指的是参数的索引
value指的是参数值
-->
<bean id="user2" class="com.lanson.bean.User">
<constructor-arg name="userid" value="2"></constructor-arg>
<constructor-arg name="username" value="小明"></constructor-arg>
<constructor-arg name="password" value="123456789"></constructor-arg>
</bean>
<bean id="user3" class="com.lanson.bean.User">
<constructor-arg index="0" value="3"></constructor-arg>
<constructor-arg index="1" value="小黑"></constructor-arg>
<constructor-arg index="2" value="987654321"></constructor-arg>
</bean>
5.3、通过p名称空间和c名称空间给对象属性赋值
添加约束
<?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名称空间,就是对property的简化处理-->
<bean id="user4" class="com.lanson.bean.User" p:userid="4" p:username="小东" p:password="111111" ></bean>
<!--c名称空间,就是对constructor-arg的简化-->
<bean id="user5" class="com.lanson.bean.User" c:userid="5" c:username="小西" c:password="222222" ></bean>
测试代码
public class Test1
@Test
public void testGetBean()
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = context.getBean("user1", User.class);
User user2 = context.getBean("user2", User.class);
User user3 = context.getBean("user3", User.class);
User user4 = context.getBean("user4", User.class);
User user5 = context.getBean("user5", User.class);
System.out.println(user1);
System.out.println(user2);
System.out.println(user3);
System.out.println(user4);
System.out.println(user5);
5.4、注入空值和特殊符号
<bean id="user1" class="com.lanson.bean.User">
<!--null值-->
<property name="userid">
<null></null>
</property>
<!--特殊符号 转译字符 < < >> & & -->
<property name="username" value="&xiaoming<>"></property>
<!-- 特殊符号 <![CDATA[内容]]> -->
<property name="password">
<value><![CDATA[&<123456>]]></value>
</property>
</bean>
5.5、关于bean引用
实体类
package com.lanson.bean;
import java.util.Date;
/**
* @Author: lansonli
* @Description: xiaomi
*/
public class Mouse
private String name;
private Date birthdate;
@Override
public String toString()
return "Mouse" +
"name='" + name + '\\'' +
", birthdate=" + birthdate +
'';
public Mouse()
public Mouse(String name, Date birthdate)
this.name = name;
this.birthdate = birthdate;
public String getName()
return name;
public void setName(String name)
this.name = name;
public Date getBirthdate()
return birthdate;
public void setBirthdate(Date birthdate)
this.birthdate = birthdate;
package com.lanson.bean;
/**
* @Author: lansonli
* @Description: xiaomi
*/
public class Cat
private String name;
private Mouse mouse1;
@Override
public String toString()
return "Cat" +
"name='" + name + '\\'' +
", mouse1=" + mouse1 +
'';
public Cat()
public Cat(String name, Mouse mouse1)
this.name = name;
this.mouse1 = mouse1;
public String getName()
return name;
public void setName(String name)
this.name = name;
public Mouse getMouse1()
return mouse1;
public void setMouse1(Mouse mouse1)
this.mouse1 = mouse1;
xml 配置
<!--告诉容器准备一个Date对象-->
<bean id="date1" class="java.util.Date"></bean>
<bean id="mouse1" class="com.lanson.bean.Mouse">
<property name="name" value="Jerry"></property>
<!--bean引用引用外部bean-->
<property name="birthdate" ref="date1"></property>
</bean>
<bean id="cat1" class="com.lanson.bean.Cat">
<property name="name" value="Tom"></property>
<!--引用内部bean-->
<property name="mouse1" >
<bean class="com.lanson.bean.Mouse">
<property name="name" value="Jerry2"></property>
<property name="birthdate">
<bean class="java.util.Date"></bean>
</property>
</bean>
</property>
</bean>
<bean id="mouse2" class="com.lanson.bean.Mouse"></bean>
<bean id="cat2" class="com.lanson.bean.Cat">
<property name="name" value="Tom2"></property>
<!--级联引入bean-->
<property name="mouse1" ref="mouse2"></property>
<!--用反射调用get*** 方法,获得对象之后,再赋值-->
<property name="mouse1.name" value="Jerry3"></property>
<property name="mouse1.birthdate">
<bean class="java.util.Date"></bean>
</property>
</bean>
5.6、关于集合注入
实体类
package com.lanson.bean;
/**
* @Author: lansonli
* @Description: xiaomi
*/
public class Book
private String bname;
private String author;
@Override
public String toString()
return "Book" +
"bname='" + bname + '\\'' +
", author='" + author + '\\'' +
'';
public Book()
public Book(String bname, String author)
this.bname = bname;
this.author = author;
public String getBname()
return bname;
public void setBname(String bname)
this.bname = bname;
public String getAuthor()
return author;
public void setAuthor(String author)
this.author = author;
package com.lanson.bean;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @Author: lansonli
* @Description: xiaomi
*/
public class Student
private String[] books;
private Set<String> bookSet;
private List<String> bookList;
private Map<String,String> bookMap;
private List<Book> bookList2;
@Override
public String toString()
return "Student" +
"books=" + Arrays.toString(books) +
", bookSet=" + bookSet +
", bookList=" + bookList +
", bookMap=" + bookMap +
", bookList2=" + bookList2 +
'';
public String[] getBooks()
return books;
public void setBooks(String[] books)
this.books = books;
public Set<String> getBookSet()
return bookSet;
public void setBookSet(Set<String> bookSet)
this.bookSet = bookSet;
public List<String> getBookList()
return bookList;
public void setBookList(List<String> bookList)
this.bookList = bookList;
public Map<String, String> getBookMap()
return bookMap;
public void setBookMap(Map<String, String> bookMap)
this.bookMap = bookMap;
public List<Book> getBookList2()
return bookList2;
public void setBookList2(List<Book> bookList2)
this.bookList2 = bookList2;
public Student()
public Student(String[] books, Set<String> bookSet, List<String> bookList, Map<String, String> bookMap, List<Book> bookList2)
this.books = books;
this.bookSet = bookSet;
this.bookList = bookList;
this.bookMap = bookMap;
this.bookList2 = bookList2;
配置文件
<?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"
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:list id="outerbookList">
<!--声明多个Book对象-->
<bean id="b1" class="com.lanson.bean.Book" p:bname="JAVA" p:author="跟Lansonli学习大数据"></bean>
<bean id="b2" class="com.lanson.bean.Book" p:bname="Go" p:author="跟Lansonli学习大数据"></bean>
<bean id="b3" class="com.lanson.bean.Book" p:bname="JVM" p:author="跟Lansonli学习大数据"></bean>
</util:list>
<bean id="student1" class="com.lanson.bean.Student">
<!--数组属性注入-->
<property name="books">
<array>
<value>JAVA</value>
<value>mysql</value>
<value>Spring</value>
</array>
</property>
<!--set集合注入-->
<property name="bookSet">
<set>
<value>JAVA</value>
<value>MySQL</value>
<value>Spring</value>
</set>
</property>
<!--list集合注入-->
<property name="bookList">
<list>
<value>JAVA</value>
<value>MySQL</value>
<value>Spring</value>
</list>
</property>
<!--map集合注入-->
<property name="bookMap">
<map>
<entry key="JAVA" value="跟Lansonli学习大数据"></entry>
<entry key="Go" value="跟Lansonli学习大数据"></entry>
<entry key="JVM" value="跟Lansonli学习大数据"></entry>
</map>
</property>
<!--List对象集合注入-->
<property name="bookList2" ref="outerbookList"></property>
</bean>
</beans>
5.7、工厂方式获取bean
特点 : bean标签中定义的class类不是返回的对象的类,而是生产该对象的工厂
工厂模式:GOF 设计模式
spring中给我们定义好了一个工厂接口,可以生产对象的接口,我们可以通过工厂来获取bean
定义工厂对象实现 FactoryBean接口
package com.lanson.bean;
import org.springframework.beans.factory.FactoryBean;
/**
* @Author: lansonli
* @Description: xiaomi
*/
public class BookFactory implements FactoryBean<Book>
@Override
public Book getObject() throws Exception
Book book=new Book();
book.setBname("JAVA");
book.setAuthor("蓝深李");
return book;
@Override
public Class<?> getObjectType()
return null;
配置文件
<bean id="book" class="com.lanson.bean.BookFactory"></bean>
测试代码
@Test
public void testGetBean()
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext5.xml");
Book book = applicationContext.getBean("book", Book.class);
System.out.println(book);
- 📢博客主页:https://lansonli.blog.csdn.net
- 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
- 📢本文由 Lansonli 原创,首发于 CSDN博客🙉
- 📢停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更美好的生活✨
以上是关于Spring基础:XML方式实现DI的主要内容,如果未能解决你的问题,请参考以下文章