二、IOC容器
说明
二、IOC容器
IOC 概念和原理
1、什么是IOC
(1) 控制反转,把对象创建和对象之间的调用过程,交给Spring 进行管理
(2)使用IOC目录:为了耦合度降低
(3)做入门案例就是IOC实现
2、IOC底层原理
(1)xml 解析、工厂模式、反射
3、画图讲解IOC底层原理
IOC (接口)
1、IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
2、Spring 提供IOC容器实现两种方式:(两个接口)
(1)BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员使用
- 加载文件时候不会创建对象,在获取(使用)对象才去创建对象
(2)ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能,一般有开发人员进行使用
3、ApplicationContext接口的实现类
FileSystemXmlApplicationContext:电脑盘符路径
ClassPathXmlApplicationContext:类相对工程路径
IOC 操作 Bean 管理(概念)
1、什么是Bean 管理
(0)Bean管理指的是两个操作
(1)Spring创建对象
(2)Spring注入属性
2、Bean管理有两种方式
(1)基于xml 配置文件方式实现
(2)基于注解方式实现
IOC 操作 Bean 管理(基于xml方式)
1、基于xml 方式创建对象
(1)在spring配置文件中,使用bean标签,在标签里面添加对应属性,就可以实现对象创建
(2)在bean标签有很多属性,介绍常用的属性
- id 属性:唯一标识
- class属性:类全路径(包类路径)
(3)创建对象的时候,默认执行无参数的构造方法完成对象创建
2、基于xml 方式注入属性
(1)DI:依赖注入,就是注入属性
3、第一种注入方式:使用set方法进行注入
(1)创建类,定义属性和对应的set方法
spring5/新建Book类
package com.atguigu.spring5;
/**
* 演示使用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 void testDemo(){
System.out.println(bname+"::"+bauthor);
}
// //有参数的构造注入
//
// public Book(String bname) {
// this.bname = bname;
// }
//
// public static void main(String[] args) {
Book book =new Book();
book.setBname("abc");
// Book book=new Book("abc");
// }
}
(2)在spring配置文件配置对象创建,配置属性注入
bean1.xml
<!-- 2 set方法注入属性 -->
<bean id="book" class="com.atguigu.spring5.Book">
<!-- 使用property 完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
</bean>
TestSpring5
@Test
public void testBook1(){
//1 加载spring的配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext("bean1.xml");
//2 获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.testDemo();
}
4、第二种注入方式:使用有参构造方法进行注入
(1)创建类,定义属性,创建属性对应有参数构造方法
spring5/创建Orders类
package com.atguigu.spring5;
public class Orders {
//属性
private String name;
private String address;
//有参数的构造
public Orders(String name, String address) {
this.name = name;
this.address = address;
}
public void ordersTest(){
System.out.println(oname+"::"+address);
}
}
(2)在spring配置文件中配置
bean1.xml
<!-- 3 有参数构造注入属性 -->
<bean id="orders" class="com.atguigu.spring5.Orders">
<constructor-arg name="oname" value="abc"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
<!-- <constructor-arg index="0" value=""></constructor-arg>-->
</bean>
TestSpring5
@Test
public void testOrders(){
//1 加载spring的配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext("bean1.xml");
//2 获取配置创建的对象
Orders orders = context.getBean("orders", Orders.class);
System.out.println(orders);
orders.ordersTest();
}
}
5、p名称空间注入(了解)
(1)使用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">
2)进行属性注入,在bean标签里面进行操作
bean1.xml
<!-- p方法注入属性-->
<bean id="book" class="com.atguigu.spring5.Book" p:bname="九阳神功" p:bauthor="无名氏">
</bean>
IOC 操作 Bean 管理(xml注入其他类型属性)
1、字面量
(1)null值
修改Book
package com.atguigu.spring5;
/**
* 演示使用set方法进行注入属性
*/
public class Book {
//创建属性
private String bname;
private String bauthor;
private String address;
//set方法注入
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void setAddress(String address) {
this.address = address;
}
public void testDemo(){
System.out.println(bname+"::"+bauthor+"::"+address);
}
bean1.xml
<!-- 2 set方法注入属性 -->
<bean id="book" class="com.atguigu.spring5.Book">
<!-- 使用property 完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
<!-- null值 -->
<property name="address">
<null/>
</property>
</bean>
(2)属性值包含特殊符号
bean1.xml
<!-- 2 set方法注入属性 -->
<bean id="book" class="com.atguigu.spring5.Book">
<!-- 使用property 完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
<!-- null值 -->
<!-- <property name="address">-->
<!-- <null/>-->
<!-- </property>-->
<!-- 属性值中包含特殊符号
1 把<>进行转移 < >
2 把带特殊符号内容写到CDATA
-->
<property name="address">
<value><![CDATA[<<南京>>]]><!--CD 回车 --></value>
</property>
</bean>
2、注入属性-外部bean
(1)创建两个类service类和dao类
(2)在service调用dao里面的方法
spring5/新建service包/新建UserService类
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import com.atguigu.spring5.dao.UserDaoImpl;
public class UserService {
public void add(){
System.out.println("service add..............");
//原始方式:创建UserDAO对象
// UserDao userdao =new UserDaoImpl();
// userdao.update();
}
}
spring5/新建dao包/新建UserDao类
package com.atguigu.spring5.dao;
public interface UserDao {
public void update();
}
dao包/新建UserDaoImpl类
package com.atguigu.spring5.dao;
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("dao update..............");
}
}
(3)在spring牌配置文件中进行配置
UserService
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import com.atguigu.spring5.dao.UserDaoImpl;
public class UserService {
// 创建UserDao类型属性,生成set方法
private UserDao userDao=new UserDaoImpl();
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add..............");
userDao.update();
//原始方式:创建UserDAO对象
// UserDao userdao =new UserDaoImpl();
// userdao.update();
}
}
src下/新建bean2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 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>
</beans>
testdemo/新建TestBean
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testAdd(){
//1 加载spring的配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext("bean2.xml");
//2 获取配置创建的对象
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
3、注入属性-内部bean和级联赋值
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行
spring下/新建bean包下/新建Dept类
package com.atguigu.spring5.bean;
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
bean包下/新建Emp类
package com.atguigu.spring5.bean;
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,属于对象形式表示
private Dept dept;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void add(){
System.out.println(ename+"::"+gender+"::"+dept);
}
}
bean3.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 内部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>
</beans>
TestBean
@Test
public void testBean2(){
//1 加载spring的配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext("bean3.xml");
//2 获取配置创建的对象
Emp emp = context.getBean("emp", Emp.class);
emp.add();
}
4、注入属性-级联赋值
(1)第一种写法
src/新建bean4.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 级联赋值 -->
<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>
</beans>
(2)第二种写法
bean4.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 级联赋值 -->
<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><!-- 生成属性dept的get方法-->
</bean>
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>
</beans>
IOC 操作 Bean 管理(xml注入集合属性)
1、注入数组类型属性
2、注入List集合类型属性
3、注入Map集合类型属性
(1)创建类,定义数组、list、map、set类型属性,并生成set方法
新建模块(项目)spring5_demo2 下/spring5/collectiontype包/Stu类
package com.atguigu.spring5.collectiontype;
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 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;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
}
(2)在spring配置文件进行配置
bean1
<?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.集合类型属性注入 -->
<bean id="stu" class="com.atguigu.spring5.collectiontype.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="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!-- set集合类型属性注入 -->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>
</beans>
spring5/testdemo/新建TestSpring5Demo1
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.collectiontype.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5Demo1 {
@Test
public void testCollection(){
ApplicationContext context= new ClassPathXmlApplicationContext("bean1.xml");
Stu stu = context.getBean("stu", Stu.class);
stu.test();
}
}
4、在集合里面设置对象类型值
collectiontype下/新建Course类
package com.atguigu.spring5.collectiontype;
public class Course {
private String cname;
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
bean1.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">
<!-- 1.集合类型属性注入 -->
<bean id="stu" class="com.atguigu.spring5.collectiontype.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="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!-- set集合类型属性注入 -->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
<!-- 注入list集合类型,值是对象 -->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!-- 创建多个course对象 -->
<bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
<property name="cname" value="Spring5框架"></property>
</bean>
<bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
<property name="cname" value="MyBatis框架"></property>
</bean>
</beans>
5、把集合注入部分提取出来
(1)在spring牌配置文件引入名称空间util
(2)使用util标签完成list集合注入提取
collectiontype/新建Book
package com.atguigu.spring5.collectiontype;
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);
}
}
src下/bean2.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"
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="bookList">
<value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
</util:list>
<!-- 2.提取list集合类型属性注入使用 -->
<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
<property name="list" ref="bookList"></property>
</bean>
</beans>
TestSpring5Demo1
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.collectiontype.Book;
import com.atguigu.spring5.collectiontype.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5Demo1 {
@Test
public void testCollection1(){
ApplicationContext context= new ClassPathXmlApplicationContext("bean1.xml");
Stu stu = context.getBean("stu", Stu.class);
stu.test();
}
@Test
public void testCollection2(){
ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean("book", Book.class);
book.test();
}
}
IOC 操作 Bean 管理(FactoryBean)
1、Spring有两种类型的bean,一种普通bean,另外一种工厂bean(FactoryBean)
2、普通bean:在配置文件中定义bean类型就是返回类型
3、工厂bean:在配置文件定义bean类型可以和返回类型不一样
第一步 创建一个类,让这个类作为工厂bean,实现接口FactoryBean
第二步 实现接口里面的方法,在实现的方法中定义返回的bean类型
spring5/新建factorybean/MyBean类
package com.atguigu.spring5.factorybean;
import com.atguigu.spring5.collectiontype.Course;
import org.springframework.beans.factory.FactoryBean;
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course=new Course();
course.setCname("abc");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
bean3.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"
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">
<bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean">
</bean>
</beans>
TestSpring5Demo1
@Test
public void test3(){
ApplicationContext context= new ClassPathXmlApplicationContext("bean3.xml");
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
IOC 操作 Bean 管理(Bean的作用域)
1、在Spring里面,设置创建bean实例是单实例还是多实例
2、在Spring里面,默认情况下,bean是单实例对象
TestSpring5Demo1
@Test
public void testCollection2(){
ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml");
Book book1 = context.getBean("book", Book.class);
Book book2 = context.getBean("book", Book.class);
// book.test();
System.out.println(book1);//com.atguigu.spring5.testdemo.TestSpring5Demo1
System.out.println(book2);//com.atguigu.spring5.testdemo.TestSpring5Demo1
}
3、如何设置单实例还是多实例
(1)在spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例
(2)scope属性值
第一个值 默认值,singleton,表示单实例对象
第二个值 prototype,表示多实例对象
bean2.xml
<!-- 2.提取list集合类型属性注入使用 -->
<bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
<property name="list" ref="bookList"></property>
</bean>
TestSpring5Demo1
@Test
public void testCollection2(){
ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml");
Book book1 = context.getBean("book", Book.class);
Book book2 = context.getBean("book", Book.class);
// book.test();
System.out.println(book1);//com.atguigu.spring5.testdemo.TestSpring5Demo1
System.out.println(book2);//com.atguigu.spring5.testdemo.TestSpring5Demo1
// com.atguigu.spring5.collectiontype.Book@38afe297
// com.atguigu.spring5.collectiontype.Book@2df3b89c
}
(3)singleton和prototype区别
第一 singleton单实例,prototype多实例
第二 设置scope值是singleton时候,加载spring配置文件时候就会创建单实例对象
设置scope值是prototype时候,不是在加载spring配置文件时候就会创建对象,在调用getBean方法时候创建多实例对象
request 了解
session 了解
IOC 操作 Bean 管理(bean生命周期)
1、生命周期
(1)从对象创建到对象销毁的过程
2、bean生命周期
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)调用bean的初始化的方法(需要进行配置)
(4)bean可以使用了(对象获取到了)
(5)当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁的方法)
3、演示bean生命周期
bean4.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"
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">
<bean id="orders" class="com.atguigu.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oname" value="手机"></property>
</bean>
</beans>
spring5/bean/新建Orders类
package com.atguigu.spring5.bean;
public class Orders {
private String oname;
//无参构造
public Orders() {
System.out.println("第一步 执行无参构造器创建bean实例");
}
public void setOname(String oname) {
System.out.println("第二步 调用set方法设置属性值");
this.oname = oname;
}
//创建执行的初始化的方法
public void initMethod(){
System.out.println("第三步 执行初始化的方法");
}
//创建执行的销毁的方法
public void destroyMethod(){
System.out.println("第五步 执行销毁的方法");
}
}
TestSpring5Demo1
@Test
public void testBean3(){
// ApplicationContext context= new ClassPathXmlApplicationContext("bean4.xml");
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("bean4.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步 获取创建bean实例对象");
System.out.println(orders);
//手动让bean实例销毁
context.close();
}
执行testBean3
com.atguigu.spring5.bean.Orders@3754a4bf
第五步 执行销毁的方法
Process finished with exit code 0
4、bean的后置处理器,bean生命周期有七步
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)把bean实例传递bean后置处理器的方法postProcessBeforeInitialization
(4)调用bean的初始化的方法(需要进行配置)
(5)把bean实例传递bean后置处理器的方法postProcessAfterInitialization
(6)bean可以使用了(对象获取到了)
(7)当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁的方法)
5、演示添加后置处理器效果
(1)创建类,实例接口 BeanPostProcessor,创建后置处理器
bean下/创建MyBeanPost
package com.atguigu.spring5.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
public class MyBeanPost implements BeanPostProcessor {//F4 或Ctrl+鼠标左键 BeanPostProcessor
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}
bean4.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"
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">
<bean id="orders" class="com.atguigu.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oname" value="手机"></property>
</bean>
<!-- 配置后置处理器 -->
<bean id="myBeanPost" class="com.atguigu.spring5.bean.MyBeanPost"></bean>
</beans>
执行testBean3
com.atguigu.spring5.bean.Orders@1d296da
第五步 执行销毁的方法
Process finished with exit code 0
IOC 操作 Bean 管理(xml自动装配)
1、什么是自动装配
(1)根据指定装配规则(属性名称或属性类型),Spring自动将匹配的属性值进行注入
2、演示自动装配过程
spring/5新建autowire/新建Dept
package com.atguigu.spring5.autowire;
public class Dept {
@Override
public String toString() {
return "Dept{}";
}
}
autowire/新建Emp
package com.atguigu.spring5.autowire;
public class Emp {
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp{" +
"dept=" + dept +
'}';
}
public void test(){
System.out.println(dept);
}
}
src下/新建bean5.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"
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">
<bean id="emp" class="com.atguigu.spring5.autowire.Emp">
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
</beans>
TestSpring5Demo1
@Test
public void test4(){
ApplicationContext context= new ClassPathXmlApplicationContext("bean5.xml");
Emp emp = context.getBean("emp", Emp.class);
System.out.println(emp);
}
(1)根据属性名称自动注入
bean5.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"
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">
<!-- 实现自动装配
bean标签属性autowire,配置自动装配
autowire属性有两个值:
byName 根据属性名称注入,注入值bean的id值和类属性名称一样
byType 根据属性类型注入
-->
<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byName">
<!-- <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
</beans>
(2)根据属性类型自动注入
bean5.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"
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">
<!-- 实现自动装配
bean标签属性autowire,配置自动装配
autowire属性有两个值:
byName 根据属性名称注入,注入值bean的id值和类属性名称一样
byType 根据属性类型注入
-->
<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byType">
<!-- <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
<!-- 多个bean byType会报错 ,一般用注解进行自动装配-->
<!-- <bean id="dept1" class="com.atguigu.spring5.autowire.Dept"></bean>-->
</beans>
IOC 操作 Bean 管理(外部属性文件)
1、直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖jar包
JDBC中包含官网下载
src下/新建bean6.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"
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">
<!-- 直接配置数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Drive"></property><!-- 为8.0之后 前为 com.mysql.jdbc.Drive -->
<property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
2、引入外部的属性文件配置数据库连接池
(1)创建外部属性文件,properties格式文件,数据库信息
src下/新建 jdbc.properties
prop.driverClass=com.mysql.cj.jdbc.Drive
prop.url=jdbc:mysql://localhost:3306/userDb
prop.username=root
prop.password=root
(2)把外部properties属性文件引入到spring配置文件中
- 引入context名称空间
- 在spring配置文件使用标签引入外部属性文件
bean6.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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 直接配置数据库连接池 -->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
<!-- <property name="driverClassName" value="com.mysql.cj.jdbc.Drive"></property><!– 为8.0之后 前为 com.mysql.jdbc.Drive –>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>-->
<!-- <property name="username" value="root"></property>-->
<!-- <property name="password" value="root"></property>-->
<!-- </bean>-->
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 直接配置数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>
IOC 操作 Bean 管理(基于注解方法)
1、什么是注解
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解目的:简化xml配置
2、Spring针对Bean管理中创建对象提供注解
(1)@Component
(2)@Service
(3)@Controller
(4)@Repository
- 上面的四个注解功能是一样的,都可以用来创建bean实例
3、基于注解方式实现对象创建
第一步 引入依赖
spring-aop-5.3.10.jar
第二步 开启组件扫描
新建一个模块(项目)spring5_demo3/src下/新建bean1.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启组件扫描
1 如果扫描多个包,多个包使用逗号隔开
2 扫描包上层目录
-->
<context:component-scan base-package="com.atguigu"></context:component-scan>
</beans>
第三步 创建类,在类上面添加创建对象注解
spring5/新建service/新建UserService
package com.atguigu.spring5.service;
import org.springframework.stereotype.Component;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService")//<bean id="userService" class",,"/>
public class UserService {
public void add(){
System.out.println("service add.........");
}
}
TestSpring5Demo1
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5Demo1 {
@Test
public void testService(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
UserService uservice = context.getBean("userService", UserService.class);
System.out.println(uservice);
uservice.add();
}
}
4、开启组件扫描细节配置
bean1.xml
<!-- 实例1
use-default-filters="false" 表示现在不使用默认filter,自己配置filter
context:include-filter ,设置扫描哪些内容
-->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 实例2
下面配置扫描所有内容
context:exclude-filter ,设置哪些内容 不 进行扫描
-->
<context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
5、基于注解方式实现属性注入
(1)@Autowired:根据属性类型进行自动装配
第一步 把service和dao对象创建,在service和dao类添加创建对象注解
spring5/新建dao/新建UserDao接口
package com.atguigu.spring5.dao;
public interface UserDao {
public void add();
}
dao/新建UserDaoImpl
package com.atguigu.spring5.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add.....");
}
}
第二部在service注入dao对象,在service类中添加dao类型属性,在属性上面使用注解
UserService
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
//@Component(value = "userService")//<bean id="userService" class",,"/>
//@Controller
//@Repository
@Service
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired
private UserDao userDao;
public void add(){
System.out.println("service add.........");
userDao.add();
}
}
执行TestSpring5Demo1
com.atguigu.spring5.service.UserService@73e22a3d
service add.........
dao add.....
Process finished with exit code 0
(2)@Qualifier:根据属性名称进行自动注入
这个@Qualifier注解的使用,和上面@Autowired一起使用
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
//@Component(value = "userService")//<bean id="userService" class",,"/>
//@Controller
//@Repository
@Service
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired//根据类型进行注入
@Qualifier(value = "userDaoImpl")//根据名称进行注入,当UserDao有多个实现类时,可以精确找到
private UserDao userDao;
public void add(){
System.out.println("service add.........");
userDao.add();
}
}
(3)@Resource:可以根据类型注入,可以根据名称注入
jdk11之后开始移除了,spring官方不建议用因为在javax.annotation.Resource
(4)@Value:注入普通类型属性
UserService
package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService -- userService
//@Component(value = "userService")//<bean id="userService" class",,"/>
//@Controller
//@Repository
@Service
public class UserService {
@Value(value = "abc")
private String name;
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
@Autowired//根据类型进行注入
@Qualifier(value = "userDaoImpl")//根据名称进行注入,当UserDao有多个实现类时,可以精确找到
private UserDao userDao;
public void add(){
System.out.println("service add........."+name);
userDao.add();
}
}
6、完全注解开发
(1)创建配置类,替代xml配置文件
spring5下/新建config包/新建SpringConfig
package com.atguigu.spring5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.atguigu"})
public class SpringConfig {
}
(2)编写测试类
TestSpring5Demo1
@Test
public void testService2(){
//加载配置类
ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
UserService uservice = context.getBean("userService", UserService.class);
System.out.println(uservice);
uservice.add();
}