二、IOC容器3

简介: 二、IOC容器3

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
第一步 执行无参构造器创建bean实例
第二步 调用set方法设置属性值
第三步 执行初始化的方法
第四步 获取创建bean实例对象
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
第一步 执行无参构造器创建bean实例
第二步 调用set方法设置属性值
在初始化之前执行的方法
第三步 执行初始化的方法
在初始化之后执行的方法
第四步 获取创建bean实例对象
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>
相关文章
|
6天前
|
Java 测试技术 开发者
IoC容器有什么作用?
【4月更文挑战第30天】IoC容器有什么作用?
37 0
|
6天前
|
Java 测试技术 开发工具
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
|
6天前
|
XML Java 数据格式
Spring IoC容器初始化过程(xml形式)
Spring IoC容器初始化过程(xml形式)
54 0
|
6天前
|
Java 测试技术 开发者
Spring IoC容器通过依赖注入机制实现控制反转
【4月更文挑战第30天】Spring IoC容器通过依赖注入机制实现控制反转
22 0
|
6天前
|
Java 开发者 容器
IoC容器如何实现依赖注入?
【4月更文挑战第30天】IoC容器如何实现依赖注入?
21 0
|
6天前
|
XML Java 数据格式
如何配置IoC容器?
【4月更文挑战第30天】如何配置IoC容器?
20 0
|
6天前
|
XML Java 程序员
什么是Spring的IoC容器?
【4月更文挑战第30天】什么是Spring的IoC容器?
20 0
|
6天前
|
存储 前端开发 Java
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
|
6天前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (下)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界
|
6天前
|
XML Java 数据格式
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)
Spring 的奇幻起源:从 IoC 容器到 Bean 的魔法世界 (上)

热门文章

最新文章