关于实例化对象时自定义操作方法前后顺序

话不多说,线上代码:

启动类

package com.skyarthur.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

controller类

/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.skyarthur.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.google.common.base.Strings;
import com.skyarthur.springboot.controller.bean.Person;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/data")
public class DemoController {

    @RequestMapping("/demo")
    @ResponseBody
    public String demo(String name) {
        return Strings.isNullOrEmpty(name) ? "demo" : name;
    }

    @RequestMapping("/person")
    @ResponseBody
    public Person getPersonInfo() {
        return new Person("skyarthur", 30);
    }

    @RequestMapping("/model")
    @ResponseBody
    public String getModel() {
        Person person = new Person("skyarthur", 30);
        return "request model method success";
    }

    @RequestMapping("/json")
    @ResponseBody
    public String getJsonView() {
        Person person = new Person("skyarthur", 30);
        return "request json method success";
    }

}

测试对象bean

/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.skyarthur.springboot.common.bean;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;

@Slf4j
public class InitSequenceBean implements InitializingBean {

    public InitSequenceBean() {
        log.info("InitSequenceBean: construct");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("InitSequenceBean: afterPropertiesSet");
    }

    @PostConstruct
    public void postConstruct() {
        log.info("InitSequenceBean: postConstruct");
    }

    public void initMethod() {
        log.info("InitSequenceBean: initMethod");
    }
}

炮灰类 —为代码完整性,无意义

/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.skyarthur.springboot.controller.bean;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Person {
    private String name;
    private int age;
}

配置类

/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.skyarthur.springboot.common.config;

import lombok.extern.slf4j.Slf4j;
import org.jboss.logging.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.skyarthur.springboot.common.bean.InitSequenceBean;

@Slf4j
@Configuration
public class SystemConfig {

    @Bean(initMethod = "initMethod", name = "initSequenceBean")
    public InitSequenceBean initSequenceBean() {
    	log.info("SystemConfig: initSequenceBean");
        return new InitSequenceBean();
    }
}

yml 文件内容空

测试结果

2019-11-22 11:02:33.541  INFO 17016 --- [           main] c.s.s.common.config.SystemConfig         : SystemConfig: initSequenceBean
2019-11-22 11:02:33.541  INFO 17016 --- [           main] c.s.s.common.bean.InitSequenceBean       : InitSequenceBean: construct
2019-11-22 11:02:33.543  INFO 17016 --- [           main] c.s.s.common.bean.InitSequenceBean       : InitSequenceBean: postConstruct
2019-11-22 11:02:33.543  INFO 17016 --- [           main] c.s.s.common.bean.InitSequenceBean       : InitSequenceBean: afterPropertiesSet
2019-11-22 11:02:33.543  INFO 17016 --- [           main] c.s.s.common.bean.InitSequenceBean       : InitSequenceBean: initMethod

结论

construct > @PostConstruct > InitializingBean.afterPropertiesSet > initMethod

你可能感兴趣的:(java)