120_Java_Spring5_IOC1_xml1
Spring5 框架
1 Spring 是轻量级的开源的 JavaEE 框架,解决企业应用开发的复杂性
2 Spring 有两个核心部分:IOC 和 Aop
(1) IOC:控制反转,把创建对象过程交给 Spring 进行管理
(2) Aop:面向切面,不修改源代码进行功能增强
3 Spring 特点
(1)方便解耦,简化开发; Aop 编程支持
(2)方便程序测试, 方便和其他框架进行整合, 方便进行事务操作
(3)降低 API 开发难度

IOC(Inversion of Control 概念和原理)
1 IOC
(1) 控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理
(2) 使用IOC目的:为了耦合度降低
2、IOC 底层原理 : xml 解析、工厂模式、反射(通过字节码.class文件 类加载器进行操作)

IOC(BeanFactory 接口)
1 IOC思想基于IOC 容器完成,IOC容器底层就是对象工厂
2 Spring提供IOC容器实现两种方式:(两个接口)
(1) BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象
(2) ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,加载配置文件时候就会把在配置文件对象进行创建

IOC 操作Bean管理(概念)
1 Bean 管理两个操作; (1)Spring 创建对象 ; (2)Spirng 注入属性
2 Bean 管理操作有两种方式 : (1)基于 xml 配置文件方式实现 ; (2)基于注解方式实现
IOC 操作 Bean 管理(基于 xml 方式)
1 基于 xml 方式创建对象
2 基于 xml 方式注入属性 (1)DI:依赖注入, 就是注入属性
3 第一种注入方式:使用 set 方法进行注入
(1) 创建类,定义属性,创建属性对应有参数构造方法
(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">
<!-- (1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建-->
<!-- 常用的属性-->
<!-- * id 属性:唯一标识-->
<!-- * class 属性:类全路径(包类路径)-->
<!-- (2)创建对象时候,默认也是执行无参数构造方法完成对象创建-->
<!-- set 方法注入属性-->
<bean id="book" class="com.alex.Book">
<property name="bname" value="alex"></property>
<property name="bauthor" value="China"></property>
<!--属性值包含特殊符号
1 把<>进行转义 < >
2 把带特殊符号内容写到CDATA
-->
<property name="address">
<value> <![CDATA[<<沈阳>>]]></value>
</property>
<property name="shippment">
<null/>
</property>
</bean>
</beans>
@Test
public void test1(){
// 1 set 方法
//1 加载spring配置文件
//BeanFactory 加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象
BeanFactory context = new ClassPathXmlApplicationContext("bean1.xml");
//2 获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.testDemo();
//加载配置文件时候就会把在配置文件对象进行创建, 把资源压力交给服务启动阶段
ApplicationContext context1 = new ClassPathXmlApplicationContext("bean1.xml");
System.out.println("****");
Book book1 = context1.getBean("book", Book.class);
System.out.println(book1);
}
第二种注入方式:使用有参数构造进行注入
(1) 创建类,定义属性,创建属性对应有参数构造方法
(2)在 spring 配置文件中进行配置
<bean id="orders" class="com.alex.Orders">
<constructor-arg name="oname" value="alex"></constructor-arg>
<constructor-arg name= "address" value="China_Shenyang"></constructor-arg>
</bean>
@Test
public void test2(){
// 2 构造器方式
ApplicationContext order = new ClassPathXmlApplicationContext("bean1.xml");
Orders orders = order.getBean("orders", Orders.class);
orders.ordersTest();
}
第二种注入方式: p名称空间注入(了解) 使用p名称空间注入,可以简化基于 xml 配置方式
<?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="user" class="com.alex.User" p:id="001" p:name="alex"></bean>
</beans>
@Test
public void test3(){
ApplicationContext user = new ClassPathXmlApplicationContext("bean1.xml");
User user1 = user.getBean("user", User.class);
System.out.println(user1);
}
IOC 操作 Bean 管理(xml 注入其他类型属性)
1 字面量 1)null , 2) 属性值包含特殊符号 <![CDATA[<<XXX>>]]>
2 注入属性-外部 bean
3 注入属性-内部 bean
4 注入属性-级联赋值
注入属性-外部/内部 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"
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="userService" class="com.alex.service.UserService">
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.alex.dao.UserDaoImpl"></bean>
<!-- 注入属性-内部 bean-->
<bean id="emp" class="com.alex.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Sumi"></property>
<property name="gender" value="女"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.alex.bean.Dept">
<property name="dname" value="RD"></property>
</bean>
</property>
</bean>
</beans>
注入属性-级联赋值
<?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 id="emp" class="com.alex.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="Sumi"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="RD"></property>
</bean>
<bean id="dept" class="com.alex.bean.Dept">
<!-- 可以直接使用外部引用过去-->
<!-- <property name="dname" value="财务部"></property>-->
</bean>
</beans>
IOC 操作 Bean 管理(xml 注入集合属性)
1)注入数组类型属性
2)注入 List 集合类型属性
3)注入 Map 集合类型属性
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;
//学生所学多门课程
private List<Course> courseList;
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public class Course {
private String cname; //课程名称
public void setCname(String cname) {
this.cname = cname;
}}
<bean id="stu" class="com.alex.bean.Stu">
<property name="courses" >
<array>
<value>java</value>
<value>mysql</value>
</array>
</property>
<property name="list">
<list>
<value>alex</value>
<value>sully</value>
</list>
</property>
<property name="maps">
<map>
<entry key="interest1" value="learn"></entry>
<entry key="interest2" value="femal"></entry>
</map>
</property>
<property name="sets" >
<set>
<value>mysql</value>
<value>redis</value>
</set>
</property>
</bean>
4、在集合里面设置对象类型值
5、把集合注入部分提取出来
<bean id="stu" class="com.alex.bean.Stu">
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.alex.bean.Course">
<property name="cname" value="Spring5框架"></property>
</bean>
<bean id="course2" class="com.alex.bean.Course">
<property name="cname" value="MyBatis"></property>
</bean>
5、把集合注入部分提取出来
(1)在 spring 配置文件中引入名称空间 util
(2)使用 util 标签完成 list 集合注入提取
public class Book2 {
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void test1() {
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: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">
<!--1 提取list集合类型属性注入-->
<util:list id="book2list2">
<value>oracle</value>
<value>mysql</value>
<value>redis</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用-->
<bean id="book2" class="com.alex.bean.Book2" >
<property name="list" ref="book2list2" ></property>
</bean>
</beans>
@Test
public void test4(){
ApplicationContext context5 = new ClassPathXmlApplicationContext("bean5.xml");
Book2 book2 = context5.getBean("book2", Book2.class);
book2.test1();
}
- 点赞
- 收藏
- 关注作者
评论(0)