Spring5_02_IOC概念和原理_IOC 操作 Bean 管理_基于 xml 方式_ xml 方式创建对象_xml 方式注入属性(DI:依赖注入)_xml 注入集合属性

IOC 操作 Bean 管理

1、什么是 Bean 管理

Bean 管理指的是两个操作
(1)Spring 创建对象
(2)Spring 注入属性

2、Bean 管理操作有两种方式

(1)基于 xml 配置文件方式实现
(2)基于注解方式实现

IOC 操作 Bean 管理(基于 xml 方式)

1、基于 xml 方式创建对象

(1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建

<!--2 set 方法注入属性-->
	<bean id="book" class="com.atguigu.spring5.Book"></bean>

(2)在 bean 标签有很多属性,介绍常用的属性

  • id 属性:唯一标识
  • class 属性:类全路径(包类路径)

(3)创建对象时候,默认也是执行无参数构造方法完成对象创建

2、基于 xml 方式注入属性

(1)DI:依赖注入

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .

  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

3、第一种注入方式:使用 set 方法进行注入

(1)创建类,定义属性和对应的 set 方法

/**
* 演示使用 set 方法进行注入属性
*/
public class Book {
	//创建属性
	private String bname;
	private String bauthor;
	//创建属性对应的 set 方法
	public void setBname(String bname) {
		this.bname = bname;
	}
	public void setBauthor(String bauthor) {
		this.bauthor = bauthor;
	}
}

(2)在 spring 配置文件配置对象创建,配置属性注入 property

<!--2 set 方法注入属性-->
	<bean id="book" class="com.atguigu.spring5.Book">

		<!--使用 property 完成属性注入
			name:类里面属性名称
			value:向属性注入的值
		-->

		<property name="bname" value="易筋经"></property>
		<property name="bauthor" value="达摩老祖"></property>
	</bean>

4、第二种注入方式:使用有参数构造进行注入

(1)创建类,定义属性,创建属性对应有参数构造方法

/**
* 使用有参数构造注入
*/
public class Orders {
	//属性
	private String oname;
	private String address;
	//有参数构造
	public Orders(String oname,String address) {
			this.oname = oname;
			this.address = address;
	}
}

(2)在 spring 配置文件中进行配置 construction

<!--3 有参数构造注入属性-->
<bean id="orders" class="com.atguigu.spring5.Orders">
	<constructor-arg name="oname" value="电脑"></constructor-arg>
	<constructor-arg name="address" value="China"></constructor-arg>
</bean>

方式二:用索引index

<!--3 有参数构造注入属性-->
<bean id="orders" class="com.atguigu.spring5.Orders">
	<constructor-arg index="0" value="电脑"></constructor-arg>
	<constructor-arg index="1" value="China"></constructor-arg>
</bean>

5、P命名空间注入 : 需要在头文件中加入约束文件

  • 属性依然要设置set方法
xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.kuang.pojo.User" p:name="zhangsan" p:age="18"/>

6、c 命名空间注入 : 需要在头文件中加入约束文件

xmlns:c="http://www.springframework.org/schema/c"
 <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.kuang.pojo.User" c:name="lisi" c:age="18"/>

Spring5_02_IOC概念和原理_IOC 操作 Bean 管理_基于 xml 方式_ xml 方式创建对象_xml 方式注入属性(DI:依赖注入)_xml 注入集合属性
Spring5_02_IOC概念和原理_IOC 操作 Bean 管理_基于 xml 方式_ xml 方式创建对象_xml 方式注入属性(DI:依赖注入)_xml 注入集合属性

IOC 操作 Bean 管理(xml 注入其他类型属性)

1、字面量

(1)null 值

<!--null 值-->
<property name="address">
		<null/>
</property>

(2)属性值包含特殊符号

  • 1 把<>进行转义 < >
  • 2 把带特殊符号内容写到 CDATA
<!--属性值包含特殊符号
		1 把<>进行转义 &lt; &gt;
		2 把带特殊符号内容写到 CDATA	<value> <![值]> </value>
-->
<property name="address">
		<value> <![CDATA[<<南京>>]]> </value>
</property>

2、注入属性-外部 bean

(1)创建两个类 service 类和 dao 类
(2)在 service 调用 dao 里面的方法
(3)在 spring 配置文件中进行配置

public class UserService {
	//创建 UserDao 类型属性,生成 set 方法
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
			this.userDao = userDao;
	}
	public void add() {
			System.out.println("service add...............");
			userDao.update();
	}
}
<!--1 service 和 dao 对象创建-->
<bean id="userService" class="com.atguigu.spring5.service.UserService">

	<!--注入 userDao 对象
			name 属性:类里面属性名称
			ref 属性:创建 userDao 对象 bean 标签 id 值
	-->
	
	<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>

测试类TestBean

@Test
public void testAdd(){
//加载Sping配置文件的对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
//2、获取配置创建的对象
UserService userService = context.getBean("userService",UserService.class);
userService,add();
}

3、注入属性-内部 bean

(1)一对多关系:部门和员工

  • 一个部门有多个员工,一个员工属于一个部门
  • 部门是一,员工是多

(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

//部门类
public class Dept {
private String dname;

public void setDname(String dname) {
		this.dname = dname;
}
}

//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Dept dept;

public void setDept(Dept dept) {
		this.dept = dept;
}
public void setEname(String ename) {
		this.ename = ename;
}
public void setGender(String gender) {
		this.gender = gender;
	}
}

(3)在 spring 配置文件中进行配置

<!--内部 bean-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
	<!--设置两个普通属性-->
	<property name="ename" value="lucy"></property>
	<property name="gender" value="女"></property>
	
	<!--设置对象类型属性(用内部Bean)-->
	<property name="dept">
			<bean id="dept" class="com.atguigu.spring5.bean.Dept">
				<property name="dname" value="安保部"></property>
			</bean>
	</property>

</bean>

4、注入属性-级联赋值

(1)第一种写法

<!--级联赋值-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>

(1)第二种写法

<!--级联赋值-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
<property name="dept.dname" ref="dept"></property>    //要在Emp.java中有Dept的get方法
</bean>
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>
  • 注意:需要get方法**
    Spring5_02_IOC概念和原理_IOC 操作 Bean 管理_基于 xml 方式_ xml 方式创建对象_xml 方式注入属性(DI:依赖注入)_xml 注入集合属性

IOC 操作 Bean 管理(xml 注入集合属性)

1、注入数组类型属性

2、注入 List 集合类型属性

3、注入 Map 集合类型属性

测试:
(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法

public class Stu {
	//1 数组类型属性
	private String[] courses;
	//2 list 集合类型属性
	private List<String> list;
	//3 map 集合类型属性
	private Map<String,String> maps;
	//4 set 集合类型属性
	private Set<String> sets;
	
	public void setSets(Set<String> sets) {
			this.sets = sets;
	}
	public void setCourses(String[] courses) {
			this.courses = courses;
	}
	public void setList(List<String> list) {
			this.list = list;
	}
	public void setMaps(Map<String, String> maps) {
			this.maps = maps;
	}
}

(2)在 spring 配置文件进行配置

<!--1 集合类型属性注入-->
<bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">

<!--数组类型属性注入list array 都可以-->
<property name="courses">
		<array>
			<value>java 课程</value>
			<value>数据库课程</value>
		</array>
</property>

<!--list 类型属性注入-->
<property name="list">
		<list>
			<value>张三</value>
			<value>小三</value>
		</list>
</property>

<!--map 类型属性注入-->
<property name="maps">
		<map>
			<entry key="JAVA" value="java"></entry>
			<entry key="PHP" value="php"></entry>
		</map>
</property>

<!--set 类型属性注入-->
<property name="sets">
		<set>
			<value>MySQL</value>
			<value>Redis</value>
		</set>
</property>
</bean>

创建一个 List
Spring5_02_IOC概念和原理_IOC 操作 Bean 管理_基于 xml 方式_ xml 方式创建对象_xml 方式注入属性(DI:依赖注入)_xml 注入集合属性
在Spring配置文件中填加

<!-- 注入list集合类型,值是对象 -->
<property name="courseList">
<list>
		<ref bean="course1"></ref>
		<ref bean="course2"></ref>
</list>
</property>

4、在集合里面设置对象类型值

<!--创建多个 course 对象-->
<bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
	<property name="cname" value="Spring5 框架"></property>
</bean>
<bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
	<property name="cname" value="MyBatis 框架"></property>
</bean>

5、把集合注入部分提取出来

(1)在 spring 配置文件中引入名称空间 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:p="http://www.springframework.org/schema/p"
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">

(2)使用 util 标签完成 list 集合注入提取

<!--1 提取 list 集合类型属性注入-->

<util:list id="bookList">
<value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
</util:list>

<!--2 提取 list 集合类型属性注入使用-->

<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
<property name="list" ref="bookList"></property>
</bean>
上一篇:VB调用VS2013的C#DLL组件所得


下一篇:2021年Java技术下半场在哪,面试阿里巴巴