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方法