spring框架-IOC容器_Bean管理xml方式_其他类型属性的注入_demo02/03
Bean管理xml方式
注入空值和特殊符号
注入外部bean
注入内部bean和级联赋值
注入集合类型属性
1.XML方式注入空值和特殊符号
1.1注入空值null
<?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 https://www.springframework.org/schema/util/spring-util.xsd">
<!--使用构造方法进行属性的注入-->
<!--1、创建对象 注入属性-->
<bean id="orders" class="com.zzy.Orders"> <!--报错的原因是去找无参构造方法-->
<property name="name" value="保时捷"></property>
<property name="address" value="太原市"></property>
<!--属性值为null-->
<property name="tel">
<null/>
</property>
</bean>
</beans>
@Test
public void testOrders(){
//1.加载spring配置文件
ApplicationContext context= new ClassPathXmlApplicationContext("beanOrders.xml");
//2.获取配置创建的对象
Orders order = context.getBean("orders", Orders.class);
//3.输出对象book
System.out.println(order);
//4.调用方法
order.pay();
}
1.2 注入特殊符号
<?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 https://www.springframework.org/schema/util/spring-util.xsd">
<!--使用构造方法进行属性的注入-->
<!--1、创建对象 注入属性-->
<bean id="orders" class="com.zzy.Orders"> <!--报错的原因是去找无参构造方法-->
<property name="name" value="保时捷"></property>
<property name="address" value="太原市"></property>
<!--属性值为null
<property name="tel">
<null/>
</property>
-->
<!--属性值中有特殊符号
1.把<>进行转译,< >
2.把带特殊符号的内容写到CDATA
-->
<property name="tel">
<value>
<![CDATA[<<0351-1111>>]]>
</value>
</property>
</bean>
</beans>
@Test
public void testOrders(){
//1.加载spring配置文件
ApplicationContext context= new ClassPathXmlApplicationContext("beanOrders.xml");
//2.获取配置创建的对象
Orders order = context.getBean("orders", Orders.class);
//3.输出对象book
System.out.println(order);
//4.调用方法
order.pay();
}
2 注入外部bean
(1)创建两个类Service和Dao类
public class UserService {
//创建UserDao类属性 生成set方法
private UserDao userDao;
//生成set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
//设置普通方法
public void add() {
System.out.println("UserService.add...............");
//调用dao层方法
userDao.update();
}
}
public interface UserDao {
public void update();
}
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("update ok .............");
}
}
(2)在Spring配置文件中进行配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Service和Dao对象的创建-->
<bean id="userService" class="com.service.UserService">
<!--注入UserDao对象属性
name是注入的属性名称
ref指的是UserDao对象bean标签的id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.dao.UserDaoImpl"></bean>
</beans>
(3)在Service调用Dao里面的Dao的方法
public class TestService {
/*注入外部bean案例测试*/
@Test
public void testService(){
//1.加载spring配置文件
ApplicationContext context= new ClassPathXmlApplicationContext("beanService.xml");
//2.获取配置创建的对象
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
3.注入内部bean
(1)一对多的关系:部门和员工关系 (案例)
(2)创建实体类之间的对应关系,员工表示所属部门,使用对象类型属性进行表示,创建pojo
public class Dept {
//部门名称
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"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配置文件中进行配置,再次测试
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 内部bean -->
<bean id="emp" class="com.zzy.innerbean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Tom"></property>
<property name="gender" value="man"></property>
<!--设置对象类型属性 也就数内部bean-->
<property name="dept">
<bean id="dept>" class="com.zzy.innerbean.Dept">
<property name="dname" value="国安部"></property>
</bean>
</property>
</bean>
</beans>
public class TestInnerBean {
@Test
public void testInner(){
ApplicationContext context = new ClassPathXmlApplicationContext("beanInnerBean.xml");
Emp emp =context.getBean("emp", Emp.class);
emp.add();
}
}
4.内部/外部bean注入属性的级联赋值
(1)内部bean注入属性级联赋值第写法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 内部bean -->
<bean id="emp" class="com.zzy.innerbean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Tom"></property>
<property name="gender" value="man"></property>
<!--设置对象类型属性 也就数内部bean-->
<property name="dept">
<bean id="dept>" class="com.zzy.innerbean.Dept">
<property name="dname" value="国安部"></property>
</bean>
</property>
</bean>
</beans>
/*注入内部bean对象的级联属性*/
@Test
public void testInner(){
ApplicationContext context = new ClassPathXmlApplicationContext("beanInnerBean.xml");
Emp emp =context.getBean("emp", Emp.class);
emp.add();
}
(2)外部bean注入属性级联赋值写法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 外部bean 级联赋值 -->
<bean id="emp" class="com.zzy.innerbean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Tom"></property>
<property name="gender" value="man"></property>
<!--设置外部bean对象类型-->
<property name="dept" ref="dept"></property>
<!--再给bean对象属性赋值-->
<property name="dept.dname" value="财政部"></property>
</bean>
<bean id="dept" class="com.zzy.innerbean.Dept"></bean>
</beans>
注意:dept.name 报红的原因:未在emp类中给dept属性设置get方法
/*注入外部bean对象的级联属性*/
@Test
public void testOuter(){
ApplicationContext context = new ClassPathXmlApplicationContext("beanOutBean.xml");
Emp emp =context.getBean("emp", Emp.class);
emp.add();
}
5 Bean管理-基于xml方式注入集合属性(一)
5.1 注入数组类型属性
5.2 注入List集合类型属性
5.3 注入Map集合类型
(1)创建类,定义数组,List集合,Map集合,Set集合,生成对应的set方法
package com.zzy.conllection.type;
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> map;
//4.创建Set类型的属性
private Set<String> sets;
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
}
public void out(){
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(map);
System.out.println(sets);
}
(2)在Spring配置文件中进行操作
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--完成集合类型属性的注入-->
<bean id="stu" class="com.zzy.conllection.type.Stu">
<!--数组类型属性注入-->
<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="map">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!--Set类型属性注入-->
<property name="sets">
<set>
<value>Mysql</value>
<value>Oracle</value>
</set>
</property>
</bean>
</beans>
(3)测试输出结果
package com.test;
import com.zzy.conllection.type.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestConllection {
@Test
public void testCon(){
ApplicationContext context =
new ClassPathXmlApplicationContext("beanConllection.xml");
Stu stu =context.getBean("stu", Stu.class);
stu.out();
}
}
6 Bean管理-基于xml方式注入集合属性(二)
创建Student和Course的pojo实体类
package com.zzy.conllection.type2;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Student {
//学生选择多门课程
private List<Course> courseList;
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public void out(){
System.out.println(courseList);
}
}
package com.zzy.conllection.type2;
//课程类
public class Course {
private String cname;//课程名称
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
编辑Spring配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--完成集合类型属性的注入-->
<bean id="student" class="com.zzy.conllection.type2.Student">
<!--注入list集合类型 集合中的值是对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.zzy.conllection.type2.Course">
<property name="cname" value="Sprng5框架"></property>
</bean>
<bean id="course2" class="com.zzy.conllection.type2.Course">
<property name="cname" value="Shiro框架"></property>
</bean>
</beans>
测试并输出结果
package com.test;
import com.zzy.conllection.type.Stu;
import com.zzy.conllection.type2.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestConllection2 {
/*测试list集合注入属性的第二种方式*/
@Test
public void testCon(){
ApplicationContext context =
new ClassPathXmlApplicationContext("beanConllection2.xml");
Student stu =context.getBean("student", Student.class);
stu.out();
}
}
7 把集合注入部分提取出来(用作公共部分)
在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">
</beans>
创建Book的pojo实体类
package com.zzy.conllection.type3;
import java.util.List;
public class Book {
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void test(){
System.out.println(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集合类型属性的注入 map、set类似-->
<util:list id="bookList">
<value>围城</value>
<value>论语</value>
<value>已经</value>
</util:list>
<!--获取list集合类型属性的注入使用-->
<bean id="book" class="com.zzy.conllection.type3.Book">
<property name="list" ref="bookList"></property>
</bean>
</beans>
测试输出结果
package com.test;
import com.zzy.conllection.type.Stu;
import com.zzy.conllection.type3.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestConllection3 {
/*测试注入集合属性*/
@Test
public void testCon(){
ApplicationContext context =
new ClassPathXmlApplicationContext("beanConllection3.xml");
Book book =context.getBean("book", Book.class);
book.test();
}
}