Spring5进阶-03篇

学习插图.jpg

前篇

IOC操作-Bean管理(FactoryBean)

Sping中有两种类型的Bean.png

普通Bean的创建以及调用
public class MyBean {

}

<bean id="myBean" class="com.atguigu.factorybean.MyBean"></bean>


@Test
public void test3(){
//        读取配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean4.xml");
//        获取配置创建的对象
    MyBean myBean = context.getBean("myBean", MyBean.class);
    System.out.println("myBean = " + myBean);
}

复制代码
工厂bean的创建以及调用
    @Override
    public Courses getObject() throws Exception {
        Courses courses = new Courses();
        courses.setCname("China");
        return courses;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}


<bean id="myBean" class="com.atguigu.factorybean.MyBean"></bean>


@Test
public void test3(){
    //        读取配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean4.xml");
    //        获取配置创建的对象
    Courses courses = context.getBean("myBean", Courses.class);
    System.out.println("courses = " + courses);
}

复制代码

IOC操作-Bean管理(Bean作用域)

  1. 在Spring中,bean的作用域指的是:设置 创建bean 是单实例 还是多实例
  2. 在Spring中,默认情况下,Bean是单实例对象
    @Test
    public void test2(){
//        读取配置文件
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean01.xml");
//        获取配置创建的对象
        Book book1 = context.getBean("book", Book.class);
        Book book2= context.getBean("book", Book.class);

        System.out.println("book = " + book1);
        System.out.println("book = " + book2);
//        book1.test1();
    }
复制代码

2023-01-27_131220.jpg

3. 如何在Spring中设置单实例还是多实例?

1)在Spring配置文件bean标签里面有属性scope,用来设置单实例还是多实例。
2)scope属性的值:

 第一个值,默认值,singleton,表示是单实例对象
 
 第二个值,prototype,表示是多实例对象
复制代码
singleton 单实例对象实践:
<bean id="book" class="com.atguigu.Book" scope="singleton">
    <property name="bname">
        <null/>
    </property>
    <property name="bauthor" >
        <value>
            <![CDATA[<<张三>>]]>
        </value>
    </property>
 </bean>
 
@Test
public void test2(){
//        读取配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean01.xml");
//        获取配置创建的对象
    Book book1 = context.getBean("book", Book.class);
    Book book2= context.getBean("book", Book.class);

    System.out.println("book = " + book1);
    System.out.println("book = " + book2);
//        book1.test1();
}
复制代码

2023-01-27_132242.jpg

prototype 多实例对象实践:
<bean id="book" class="com.atguigu.Book" scope="prototype">
    <property name="bname">
        <null/>
    </property>
    <property name="bauthor" >
        <value>
            <![CDATA[<<张三>>]]>
        </value>
    </property>
 </bean>
 
@Test
public void test2(){
//        读取配置文件
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean01.xml");
//        获取配置创建的对象
    Book book1 = context.getBean("book", Book.class);
    Book book2= context.getBean("book", Book.class);

    System.out.println("book = " + book1);
    System.out.println("book = " + book2);
//        book1.test1();
}
复制代码

2023-01-27_132744.jpg 4. singleton和prototype的区别?

第一 :singleton 单实例;prototype是多实例

第二 :

设置scope值是singleton的时候,加载spring配置文件的时候就会创建单实例对象
设置scope值是prototype的时候,加载spring配置文件的时候不会创建对象,而是调用getBean方法的时候就会创建多实例对象。
复制代码

IOC操作-Bean管理(Bean的生命周期)

  1. 生命周期:从对象创建到对象销毁的过程。

  2. Bean的生命周期:

    1)通过构造器创建Bean实例

    2)为Bean的属性设置值和对其他对象的引用(调用set方法)

    3)调用Bean的初始化方法(需要进行配置初始化方法)

    4)bean可以使用了

    5)当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)

  3. 演示Bean的生命周期

public class Orders {
    private String oname;

    public Orders() {
        System.out.println("第一步 创建实例对象");
    }

    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("第二步 设置属性");
    }

    public void initMethod(){
        System.out.println("第三步 执行初始化方法");
    }

    public void destory(){
        System.out.println("第五步 执行销毁方法");
    }
}

<bean id="orders" class="com.atguigu.factorybean.Orders" init-method="initMethod" destroy-method="destory">
    <property name="oname" value="手机"/>
</bean>

@Test
public void test4(){
//        读取配置文件
    ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("bean4.xml");
//        获取配置创建的对象
    Orders orders = context.getBean("orders", Orders.class);
    System.out.println("第四步 获取bean对象   "+orders);
    context.close();
}
复制代码

2023-01-27_160817.jpg 4. Bean的后置处理器

Bean的生命周期有七步:

1)通过构造器创建Bean实例

2)为Bean的属性设置值和对其他对象的引用(调用set方法)

3)把bean实例传递给bean后置处理器的方法postProcessBeforeInitialization

4)调用Bean的初始化方法(需要进行配置初始化方法)

5)把bean实例传递给bean后置处理器的方法postProcessAfterInitialization

6)bean可以使用了

7)当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
复制代码

5. 演示添加后置处理器的效果

1)创建类,实现BeanPostProcessors接口,创建后置处理器

public class MyBeanPost implements 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;
    }
}

在配置文件中把后置处理器创建后,会作用到该文件中的所有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 id="orders" class="com.atguigu.factorybean.Orders" init-method="initMethod" destroy-method="destory">
        <property name="oname" value="手机"/>
    </bean>
    <bean id="myBean" class="com.atguigu.factorybean.MyBeanPost"/>
</beans>
复制代码

2023-01-27_163358.jpg

IOC操作-Bean管理(XML自动装配)

  1. 什么是自动装配?

    根据指定的装配规则(根据属性或名称),Spring自动将匹配的属性值进行注入。

  2. 演示自动装配的过程

    XML方式的自动装配:bean标签中使用属性autowire

<!--实现自动装配

bean 标签属性 autowire,配置自动装配

autowire 属性常用两个值:

byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样

byType 根据属性类型注入

-->
根据属性名称自动注入
<bean id="emp" class="com.atguigu.factorybean.Emp" autowire="byName"></bean>
<bean id="dept" class="com.atguigu.factorybean.Dept">
    <property name="dname" value="研发部"/>
</bean>

@Test
public void test5(){
//        读取配置文件
    ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("bean4.xml");
//        获取配置创建的对象
    Emp emp = context.getBean("emp", Emp.class);
    System.out.println(emp);
}
复制代码

2023-01-27_175227.jpg

根据属性类型自动注入
<bean id="emp" class="com.atguigu.factorybean.Emp" autowire="byType"></bean>
<bean id="dept" class="com.atguigu.factorybean.Dept">
    <property name="dname" value="研发部"/>
</bean>

@Test
public void test5(){
//        读取配置文件
    ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("bean4.xml");
//        获取配置创建的对象
    Emp emp = context.getBean("emp", Emp.class);
    System.out.println(emp);
}
复制代码

2023-01-27_175227.jpg

IOC操作-Bean管理(引入外部的属性文件)

  1. 配置数据库信息

    (1) 配置德鲁伊连接池

    (2) 引入德鲁伊连接池的依赖jar包

2023-01-28_104536.jpg

  1. 引入外部属性文件
    (1)配置context名称空间
<?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: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">
复制代码

(2)创建外部属性文件,properties格式,写数据库信息

2023-01-28_111727.jpg (3)把外部properties属性文件引入到spring配置文件中

<beans xmlns="http://www.springframework.org/schema/beans"

(4)在spring配置文件使用标签引入外部属性文件

<!--    配置外部属性文件-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"/>
        <property name="url" value="${prop.url}"/>
        <property name="username" value="${prop.username}"/>
        <property name="password" value="${prop.password}"/>
    </bean>
复制代码

最终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: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">
<!--    引入外部的属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

<!--    配置外部属性文件-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"/>
        <property name="url" value="${prop.url}"/>
        <property name="username" value="${prop.username}"/>
        <property name="password" value="${prop.password}"/>
    </bean>
</beans>
复制代码

猜你喜欢

转载自juejin.im/post/7193538907919614007