• Spring深入浅出(二),IoC容器,3种方法实现Bean初始化回调,以及@PostConstruct注解的配置


    一、三种方式实现Bean初始化回调

    1. 使用接口实现Bean初始化回调

     org.springframework.beans.factory.InitializingBean 接口提供了以下方法:

    void afterPropertiesSet() throws Exception;

    实现代码如下:

        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("其次,调用InitializingBean的 afterPropertiesSet方法......");
        }

    2. 配置XML实现Bean初始化回调

    可以通过 init-method 属性指定 Bean 初始化后执行的方法。

        <bean id="helloWorld" class="com.clzhang.spring.demo.HelloWorld" init-method="init2">
            <property name="message" value="Hello World!" />
        </bean>

    同时加入实现代码:

       public void init2() {
            System.out.println("最后,调用XML配置文件中指定的初始化方式......");
        }

    3. 使用注解实现Bean初始化回调

    使用 @PostConstruct 注解标明该方法为 Bean 初始化后的方法。

        @PostConstruct
        public void init1() {
            System.out.println("最先调用@PostConstruct方式的初始化方法......" );
        }

    二、@PostConstruct 注解的配置

    1. 在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    2. 然后才是引用

    <context:annotation-config/>

    3. 需要在项目中引用若干Spring包

    包括但不限于:commons-logging-1.2.jar,以及常用Spring包。

      三、全部代码参考如下

    1 .实体Bean 

    package com.clzhang.spring.demo;
    
    import org.springframework.beans.factory.InitializingBean;
    
    import javax.annotation.PostConstruct;
    
    public class HelloWorld implements InitializingBean {
        private String message;
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public void getMessage() {
            System.out.println("message : " + message);
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("其次,调用InitializingBean的 afterPropertiesSet方法......");
        }
    
        @PostConstruct
        public void init1() {
            System.out.println("最先调用@PostConstruct方式的初始化方法......" );
        }    
    
        public void init2() {
            System.out.println("最后,调用XML配置文件中指定的初始化方式......");
        }
    }

    2. 调用者

    package com.clzhang.spring.demo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MainApp {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    
            HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
            objA.setMessage("对象A");
            objA.getMessage();
            HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
            objB.getMessage();
        }
    }

    3. Bean.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <bean id="helloWorld" class="com.clzhang.spring.demo.HelloWorld" init-method="init2">
            <property name="message" value="Hello World!" />
        </bean>
        <context:annotation-config/>
    </beans>

    四、输出如下

    另:销毁回调同样有三种方式

    1. 实现org.springframework.beans.factory.DisposableBean 接口的destroy()方法

    2. 配置XML文件,设置destroy-method 属性指定 Bean 销毁后执行的方法

    3. 使用 @PreDestory 注解标明该方法为 Bean 销毁前执行的方法

    本文参考:

    http://c.biancheng.net/spring/bean-life-cycle.html

  • 相关阅读:
    Zlib编译
    最新Webstrom, Idea 2019.2.3 的激活
    图像理解与深度学习开篇
    C# 反射(Reflection)
    SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解
    Navicat for Oracle中如何使用外键
    【数据库】主键,外键,主表,从表,关联表,父表,子表
    onclick事件没有反应的五种可能情况
    button小手设置 css的cursor
    Spring MVC F5刷新问题
  • 原文地址:https://www.cnblogs.com/nayitian/p/14994790.html
Copyright © 2020-2023  润新知