spring创建对象执行步骤

spring生命周期

spring生命周期

注:ApplicationContext 对象,spring上下文对象,根据此对象可以获取spring中的对象。

package com.example.demo.service;

import com.sun.media.sound.SoftTuning;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class BeanUtils implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanPostProcessor, InitializingBean, DisposableBean {

    private StudentService studentService;

    private BeanFactory beanFactory;

    private ApplicationContext applicationContext;

    public BeanUtils() {
        System.out.println("执行了构造方法");
    }

    @Autowired
    public void setStudentService(StudentService studentService) {
        System.out.println("执行了依赖注入");
        this.studentService = studentService;
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("执行setbeanName方法:"+name);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("执行beanFactory方法:"+beanFactory);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        System.out.println("获取applicationContest对象");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("执行postProcessBeforeInitialization方法");
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("执行postProcessAfterInitialization方法");
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行afterPropertiesSet方法");
    }

    @PostConstruct
    public void set(){
        System.out.println("执行了PostConstruct注解方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("执行了destroy注解方法");
    }
}

执行了构造方法=
执行了依赖注入
beanName:beanUtils
beanFactory:xxxx
获取applicationContest对象
执行了PostConstruct注解方法
执行afterPropertiesSet方法
执行postProcessBeforeInitialization方法
执行postProcessAfterInitialization方法
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值