基于 xml 方式的bean管理
1、基于 xml 方式创建对象
(1)在 spring 配置文件,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
(2)在 bean 标签有很多属性,介绍常用的属性
- id 属性:唯一标识
- class 属性:类全路径(包类路径)
(3)创建对象时候,默认也是执行无参数构造方法完成对象创建
2、基于 xml 方式注入属性
(1)DI:依赖注入,就是注入属性
3、第一种注入方式:使用 set 方法进行注入
(1)创建类,定义属性和对应的 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;
}
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", bauthor='" + bauthor + '\'' +
'}';
}
}
(2)在 spring 配置文件配置对象创建,配置属性注入
<!--2set方法注入属性-->
<bean id="book" class="com.atguigu.spring5.Book">
<!--使用property属性完成属性注入
name:类里面的属性名称
value:向属性中注入的值
-->
<property name="bname" value="java学习指南"></property>
<property name="bauthor" value="大佬"></property>
</bean>
(3)测试
@Test
public void testBook(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml");
//2获取配置创建的对象
Book book = context.getBean ("book", Book.class);
System.out.println (book);
}
(4)结果
4、第二种注入方式:使用有参数构造进行注入
(1)创建类,定义属性和有参数的构造方法
public class Orders {
//属性
private String oname;
private String address;
//有参数构造
public Orders(String oname, String address) {
this.oname = oname;
this.address = address;
}
public String toString() {
return "Orders{" +
"oname='" + oname + '\'' +
", 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>
(3)测试
@Test
public void testOrders(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml");
//2获取配置创建的对象
Orders orders = context.getBean ("orders", Orders.class);
System.out.println (orders);
}
(4)结果
第三种注入方式: p 名称空间注入
(1)添加 p 名称空间在配置文件中
在约束中增加:xmlns:p="http://www.springframework.org/schema/p"
(2)进行属性注入,在 bean 标签里面进行操作
<!--4p名称空间注入-->
<bean id="book1" class="com.atguigu.spring5.Book" p:bauthor="绝世神功" p:bname="带佬"></bean>
(3)测试
@Test
public void testp(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml");
//2获取配置创建的对象
Book book1 = context.getBean ("book1", Book.class);
System.out.println (book1);
}
(4)结果
xml 注入其他类型属性
1、字面量null 值
(1)null 值在xml中配置
<!--5字面量-->
<bean id="book1" class="com.atguigu.spring5.Book">
<property name="bname" value="字面量null"></property>
<property name="bauthor">
<null></null>
</property>
</bean>
(2)测试
@Test
public void testnull(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean2.xml");
//2获取配置创建的对象
Book book1 = context.getBean ("book1",Book.class);
System.out.println (book1);
}
(3)结果
2、字面量,属性值包含特殊符号
<!--5字面量,属性值包含特殊属性-->
<bean id="book2" class="com.atguigu.spring5.Book">
<property name="bname" value="字面量包含特殊值"></property>
<property name="bauthor">
<value><![CDATA[<<哈哈哈>>]]></value>
</property>
</bean>
(2)测试
@Test
public void testteshu(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean2.xml");
//2获取配置创建的对象
Book book2 = context.getBean ("book2",Book.class);
System.out.println (book2);
}
(3)结果
2、注入属性-外部 bean
(1)创建两个类 service 类和 dao 类
package com.atguigu.spring5.dao;
public interface UserDao {
public void update();
}
package com.atguigu.spring5.dao;
public class UserDaoImp implements UserDao {
@Override
public void update() {
System.out.println ("update...");
}
}
(2)在 service 调用 dao 里面的方法
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
//1创建UserDao属性,生成set方法
System.out.println ("add...");
userDao.update ();
}
}
(3)在 spring 配置文件中进行配置
<bean id="userService" class="com.atguigu.spring5.service.UserService">
<property name="userDao" ref="userDaoImp"></property>
</bean>
<bean id="userDaoImp" class="com.atguigu.spring5.dao.UserDaoImp"></bean>
(4)测试
@Test
public void test1(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");
//2获取配置创建的对象
UserService bean = context.getBean ("userService", UserService.class);
System.out.println (bean);
bean.add ();
}
(5)结果
3、注入属性-内部 bean
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门,部门是一,员工是多
package com.atguigu.spring5;
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;
} }
package com.atguigu.spring5;
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
(3)在 spring 配置文件中进行配置
<bean id="userDaoImp" class="com.atguigu.spring5.dao.UserDaoImp"></bean>
<bean id="emp" class="com.atguigu.spring5.Emp">
<property name="ename" value="dd"></property>
<property name="gender" value="男"></property>
<property name="dept">
<bean id="dept" class="com.atguigu.spring5.Dept">
<property name="dname" value="保安部门"></property>
</bean>
</property>
</bean>
(4)测试
@Test
public void test2(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");
//2获取配置创建的对象
Emp emp = context.getBean ("emp", Emp.class);
System.out.println (emp);
}
(5)结果
4、xml 注入集合属性
1、注入数组类型属性
2、注入 List 集合类型属性
3、注入 Map 集合类型属性
(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
package com.atguigu.spring5;
import java.util.List;
import java.util.Map;
import java.util.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 配置文件进行配置
<!--注入集合类型-->
<bean id="stu" class="com.atguigu.spring5.Stu">
<property name="courses">
<array>
<value>java</value>
<value>php</value>
</array>
</property>
<property name="list">
<list>
<value>张三</value>
<value>王五</value>
</list>
</property>
<property name="maps">
<map>
<entry key="java" value="JAVA"></entry>
<entry key="K1" value="V1"></entry>
</map>
</property>
<property name="sets">
<set>
<value>1</value>
<value>2</value>
</set>
</property>
</bean>
(3)测试
@Test
public void test3(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");
//2获取配置创建的对象
Stu stu = context.getBean ("stu", Stu.class);
System.out.println (stu);
}
(4)结果
4、在集合里面设置对象类型值
(1)创建课程和课程List
package com.atguigu.spring5;
public class Course {
private String cname;
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
package com.atguigu.spring5;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.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;
//5学生学的多门课程
private List<Course> courseList;
@Override
public String toString() {
return "Stu{" +
"courses=" + Arrays.toString (courses) +
", list=" + list +
", maps=" + maps +
", sets=" + sets +
", courseList=" + courseList +
'}';
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
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)配置xml文件
<!--注入集合类型-->
<bean id="stu" class="com.atguigu.spring5.Stu">
<property name="courses">
<array>
<value>java</value>
<value>php</value>
</array>
</property>
<property name="list">
<list>
<value>张三</value>
<value>王五</value>
</list>
</property>
<property name="maps">
<map>
<entry key="java" value="JAVA"></entry>
<entry key="K1" value="V1"></entry>
</map>
</property>
<property name="sets">
<set>
<value>1</value>
<value>2</value>
</set>
</property>
<!--集合中的对象注入-->
<property name="courseList">
<list>
<ref bean="c1"></ref>
<ref bean="c2"></ref>
</list>
</property>
</bean>
<bean id="c1" class="com.atguigu.spring5.Course">
<property name="cname" value="数据库课程"></property>
</bean>
<bean id="c2" class="com.atguigu.spring5.Course">
<property name="cname" value="java课程"></property>
</bean>
(3)测试
@Test
public void test3(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");
//2获取配置创建的对象
Stu stu = context.getBean ("stu", Stu.class);
System.out.println (stu);
}
(4)结果
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: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集合类型的注入-->
<util:list id="bookList">
<value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用-->
<bean id="b" class="com.atguigu.spring5.util.Books">
<property name="list" ref="bookList"></property>
</bean>
(2) Books类,与前面的book不一样。
package com.atguigu.spring5.util;
import java.util.List;
public class Books {
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
}
(3)测试
@Test
public void test4(){
//1加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");
//2获取配置创建的对象
Books b = context.getBean ("b", Books.class);
System.out.println (b);
}
(4)结果