spring @postConstruct init-method constructor和afterSetProperties() 执行顺序

1 篇文章 0 订阅

示例代码:

package com.spring.initbean;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * created by anyanwen on 2017/7/20.
 */
public class InitSequenceBean implements InitializingBean {

    private boolean properties;

    public InitSequenceBean() {
        System.out.println("InitSequenceBean constructor");
    }

    public void setProperties(boolean properties) {
        System.out.println("InitSequenceBean set properties");
        this.properties = properties;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitSequenceBean afterPropertiesSet");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("InitSequenceBean postConstruct");
    }

    public void initMethod() {
        System.out.println("InitSequenceBean initMethod");
    }

    public boolean isProperties() {
        return properties;
    }

    public static void main(String[] args) {
    }
}

spring配置文件

<?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"
       xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:crane="http://code.dianping.com/schema/crane"
       xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com"/>

    <bean id="initSequenceBean" class="com.spring.initbean.InitSequenceBean"
        init-method="initMethod">
        <property name="properties" value="true"></property>
    </bean></import>

</beans>

测试类

package com.spring.initbean;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * created by anyanwen on 2017/7/20.
 */
public class InitSequenceBeanTest {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
    }
}

输出:

InitSequenceBean constructor
InitSequenceBean set properties
InitSequenceBean postConstruct
InitSequenceBean afterPropertiesSet
InitSequenceBean initMethod

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值