Bean管理

1什么是 Bean管理(概念)

(0)Bean 管理指的是两个操作

(1)Spring 创建对象

(2)Spirng 注入属性

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

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

(2)基于注解方式实现

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

1.基于xml方式创建对象

  • 在Spring配置文件中,使用Bean标签,标签里面添加对应属性,就可以实现对象创建
  • 在baen标签有很多属性,介绍常用的属性
    id:唯一标识符
    class:类路径,也是包路经
  • 创建对象的时候,默认也是执行无参数构造方法完成对象创建

2、基于xml 方式注入属性

(1)DI:依赖注入,就是注入属性

3.第一种注入方式:使用set方法经行注入

set注入属性 property

有参数构造方法 constructor-arg

(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 配置文件配置对象创建,配置属性注入

<!--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 配置文件中进行配置

<!--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>

5、p名称空间注入(了解)

(1)使用 p 名称空间注入,可以简化基于 xml 配置方式

第一步 添加 p 名称空间在配置文件中

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

第二步 进行属性注入,在 bean 标签里面进行操作

<bean id="book" class="com.atguigu.spring5.Book" p:bname="九阳神功" 
p:bauthor="无名氏"></bean>

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

<!--2 set 方法注入属性--> <bean id="book" class="com.atguigu.spring5.Book" p:bname="九阳神功" 
p:bauthor="无名氏"></bean>

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

1、字面量

(1)null 值

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

2.注入属性_外部bean

外部bean注入要引用 ​​ref​

(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>

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>
<!--设置对象类型属性-->
<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>

(2)第二种写法

Spring5--02---Bean管理_spring

<!--级联赋值--> <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" value="技术部"></property>
</bean> <bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>

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

1、注入数组类型属性

2、注入List集合类型属性

3 注入 :Map 集合类型属性

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

package com.company.collentiontype;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Stu {
//数组
private String[] courses;

// list集合类型属性
private List<String> list;


//map集合属性
private Map<String, String> maps;


//set集合属性
private Set<String> set;


//学生所学的课程
private List<Course> courseList;



public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}

public void setSet(Set<String> set) {
this.set = set;
}

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


public void setMaps(Map<String, String> maps) {
this.maps = maps;
}

public void setCourses(String[] courses) {
this.courses = courses;
}
public void test() {
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(set);
System.out.println(courseList);
}
}

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

<bean id="stu" class="com.company.collentiontype.Stu">
<property name="courses">
<array>
<array>
<value>java</value>
<value>数据库</value>
</array>
</array>
</property>

<!-- list集合注入-->
<property name="list">
<list>
<value>11</value>
<value>222</value>
<value>342</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="set">
<set>
<value>sql</value>
<value>mysql</value>
</set>
</property>

<!-- 创建多个对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>

</bean>

<!-- 创建多个course对象-->
<bean id="course1" class="com.company.collentiontype.Course">
<property name="cname" value="Springe5框架"></property>

</bean>
<bean id="course2" class="com.company.collentiontype.Course">
<property name="cname" value="Mybatis框架"></property>
</bean>

在集合里设置对象类型值

<!--    创建多个course对象-->
<bean id="course1" class="com.company.collentiontype.Course">
<property name="cname" value="Springe5框架"></property>

</bean>
<bean id="course2" class="com.company.collentiontype.Course">
<property name="cname" value="Mybatis框架"></property>
</bean>

<!-- 创建多个对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>

把集合注入部分提取出来

(1)在 spring 配置文件中引入名称空间 util

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

<?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">


<!--提取list集合属性注入-->
<util:list id="booklist">
<value>已经将</value>
<value>就养生</value>
<value>九阳神经</value>
<value>哎哎哎</value>
</util:list>

<!--2 提取list集合类型属性注入使用-->
<bean id="book" class="com.company.collentiontype.Book" scope="prototype">
<property name="list" ref="booklist"></property>
</bean>
</beans>