ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring 提供3种方法实现Bean初始化回调,含Spring中@PostConstruct注解的配置

2021-07-10 15:31:24  阅读:150  来源: 互联网

标签:初始化 Spring void PostConstruct Bean println public


一、使用接口实现Bean初始化回调

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

void afterPropertiesSet() throws Exception;

实现代码如下:

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

二、 配置XML实现Bean初始化回调

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

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

同时加入实现代码:

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

三、 使用注解实现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配置文件中指定的初始化方式......");
    }
}
View Code

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();
    }
}
View Code

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" scope="prototype" init-method="init2">
        <property name="message" value="Hello World!" />
    </bean>
    <context:annotation-config/>
</beans>

 六、输出如下

 

本文参考:

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

标签:初始化,Spring,void,PostConstruct,Bean,println,public
来源: https://www.cnblogs.com/nayitian/p/14993907.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有