IOC操作Bean管理
1. 什么是bean管理
Bean管理就是两个操作:
(1)
Spring创建对象
(2)Spring注入属性
2. bean管理操作的两种方式
(1) 基于xml配置文件方式实现
(2)基于注解方式实现
3. IOC操作bean管理(基于xml)
3.1 基于xml创建对象
<!--1 配置User对象创建-->
<bean id="user" class="com.atguigu.spring5.User"></bean>
a)在Spring配置文件中使用bean标签,在标签里面添加相应属性即可完成对象创建;
b)bean标签常用属性:id
:作为标签唯一标识符class
:类全路径(包名+类名)
c)创建对象时,默认调用无参构造方法
进行对象创建(若设置了有参构造而没声明无参构造,则会报错)
3.2 基于xml注入属性
DI:依赖注入(给对象注入属性)
(1)使用set方法
注入
第一步:创建book类,设置属性及对应set方法
package demo2;
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;
}
//测试方法
public void test(){
System.out.println("书名:"+bname+","+"作者:"+bauthor);
}
}
第二步,在Spring配置文件中配置对象创建,property标签配置属性注入
<!--使用bean标签配置对象创建-->
<bean id="book" class="demo2.Book">
<!--使用property标签注入属性
name:属性名称
value:注入的属性值
-->
<property name="bname" value="西游记"></property>
<property name="bauthor" value="吴承恩"></property>
</bean>
第三步,编写测试类TestBook
package demo2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testBook {
public static void main(String[] args) {
//1.加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("demo2/bean2.xml");
//2.创建对象
Book book = context.getBean("book",Book.class);
//3.调用book对象
book.test();
}
}
运行结果:
(2)使用有参构造
注入
第一步,创建order类,设置属性及类的有参构造方法
package demo3;
public class Order {
//属性
private String oname;
private String address;
//有参构造方法
public Order(String oname, String address) {
this.oname = oname;
this.address = address;
}
//测试方法
public void test(){
System.out.println(oname+","+address);
}
}
第二步,在Spring配置文件中配置对象创建,constructor-arg标签配置属性
<!--使用bean标签配置对象创建-->
<bean id="order" class="demo3.Order">
<!--使用constructor-arg标签注入属性
name:属性名称
value:注入的属性值
-->
<constructor-arg name="oname" value="123"></constructor-arg>
<constructor-arg name="address" value="山东"></constructor-arg>
</bean>
第三步,编写测试类TestOrder
package demo3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestOrder {
public static void main(String[] args) {
//1.加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("demo3/bean3.xml");
//2.创建对象
Order order = context.getBean("order",Order.class);
//3.调用order对象
order.test();
}
}
运行结果:
(3)P名称空间
注入(了解即可)
<!--1、添加p名称空间在配置文件头部-->
<?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"
<!--在这里添加一行p-->
<!--2、在bean标签进行属性注入(算是set方式注入的简化操作)-->
<bean id="book" class="com.atguigu.spring5.Book" p:bname="very" p:bauthor="good">
</bean>
</beans>
3.3 基于xml注入其他类型的属性(集合、空值、特殊符号…)
一、特殊字面量
(1)null值
<property name="btime">
<null/><!--属性里边添加一个null标签-->
</property>
(2)属性值包含特殊符号
<property name="btime">
<value><![CDATA[<<山东>>]]></value>
</property>
注:字面量加入特殊符号时,不可直接用value属性设置,需要把value单独作为标签使用,并且在<![CDATA[内容]]>
中加入含有特殊符号的属性值。
二、注入属性——外部bean(通过ref属性引入)
(1)创建两个类
UserImply类(实现User接口):
package User;
public class UserImply implements User {
@Override
public void update() {
System.out.println("UserImply实现类调用成功");
}
}
Userservice类(其中声明一个对象属性Userdao)★:
package Service;
import User.User;
public class UserService {
//声明一个User类型属性,该属性是对象,而不是一个字面值。
private User Userdao;
//该属性的set方法
public void setUserdao(User Userdao) {
this.Userdao = Userdao;
}
public void text(){
System.out.println("userservice..........");
//Userservice类中调用User实现类对象
Userdao.update();
}
}
(2)编写Spring配置文件 (在Userservice对象种注入外部bean作为属性)
<!--先创建Userservice对象-->
<bean id="userservice" class="Service.UserService">
<!--使用property注入外部bean
name:userservice中的属性值,
ref:要注入bean的id值,这里为下方bean的id值“User”
-->
<property name="userdao" ref="User"></property>
</bean>
<!--再创建User对象,注意class中为接口实现类的路径-->
<bean id="User" class="User.UserImply"></bean>
注:“ref” 用于引入外部bean,其值为:需要引入的bean的id值!
(3)编写testDemo测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testDemo {
public static void main(String[] args) {
//1.加载Spring配置文件
ApplicationContext context =new ClassPathXmlApplicationContext("Service/bean4.xml");
//2.创建Userservice对象
UserService userService = context.getBean("userservice",UserService.class);
//3.调用对象
userService.text();
}
}
运行结果:
三、注入属性——内部bean和级联赋值
- 一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门(部门是一,员工是多) - 在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
第一步,创建部门类和员工类
//部门类
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;
}
}
注入属性(1) —
(内部bean)
<!--内部bean-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Andy"></property>
<property name="gender" value="女"></property>
<!--设置对象类型属性-->
<property name="dept">
<!--内部bean赋值-->
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="组织部"></property>
</bean>
</property>
</bean>
注入属性(2)—
级联赋值(类似外部bean)
方式一:(通过ref引入外部bean)
<!--方式一:级联赋值-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Andy"></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>
方式二:(生成dept的get方法,用于获取dept中的属性,之后直接用property设置属性值)
//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Dept dept;
//生成dept的get方法
public void setDept(Dept dept) {
this.dept = dept;
}
//set方法
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
<!--级联赋值-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="jams"></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">
</bean>
注意写法:dept.dname
四、注入集合属性
(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
public class Stu{
//1.创建数组属性
private String[] courses;
//2.创建list属性
private List<String> lists;
//3.创建Map属性
private Map<String,String> maps;
//4.创建set属性
private Set<String> sets;
//对应属性的set方法
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
//测试方法
public void test(){
System.out.println(Arrays.toString(courses));
System.out.println(lists);
System.out.println(maps);
System.out.println(sets);
}
}
(2)在 spring 配置文件进行配置
<bean id="stu" class="demo4.Stu">
<!--1.数组类型属性注入-->
<property name="courses">
<array>
<value>java</value>
<value>数据库</value>
</array>
</property>
<!--2.list类型属性注入-->
<property name="lists">
<list>
<value>List1</value>
<value>List2</value>
</list>
</property>
<!--3.Map类型属性注入-->
<property name="maps">
<map>
<entry key="JAVA8" value="java8"></entry>
<entry key="JSP" value="jsp"></entry>
</map>
</property>
<!--4.set类型属性注入-->
<property name="sets">
<set>
<value>123</value>
<value>456</value>
</set>
</property>
</bean>
五、在集合里面设置对象类型值
(1)创建Course类
public class Course {
private String cname;
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
(2)创建Stu类,类中声明一个list类型属性(值为Course类的对象)
public class Stu {
//1.创建集合
private List<Course> courses;
//2.set方法
public void setCourses(List<Course> courses) {
this.courses = courses;
}
//测试方法
public void test() {
System.out.println(courses);
}
}
(3)在Spring配置文件中配置
<bean id="stu" class="demo4.Stu">
<!--注入list集合类型,值是对象(通过ref引入)-->
<property name="courses">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="demo4.Course">
<property name="cname" value="操作系统"></property>
</bean>
<bean id="course2" class="demo4.Course">
<property name="cname" value="组成原理"></property>
</bean>
六、把集合注入部分提取出来(作为公共部分,其他bean可以调用)
<!--第一步:在 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: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名称空间(bean全部换成util)-->
<!--第二步:使用 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" scope="prototype">
<property name="list" ref="bookList"></property>
</bean>