Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot springbootbean生命周期管理_Spring Boot - Fatal编程技术网

Spring boot springbootbean生命周期管理

Spring boot springbootbean生命周期管理,spring-boot,Spring Boot,我正在学习springboot应用程序中ApplicationContext管理的bean的初始化和销毁回调。我有一个bean,它实现了InitializingBeans和DisposableBeans接口。但是,我没有看到在删除实现时调用init方法。我缺少什么 @Component public class LifeCycleBean implements InitializingBean, DisposableBeans{ private String name; p

我正在学习springboot应用程序中ApplicationContext管理的bean的初始化和销毁回调。我有一个bean,它实现了InitializingBeans和DisposableBeans接口。但是,我没有看到在删除实现时调用init方法。我缺少什么

@Component    
public class LifeCycleBean implements InitializingBean, DisposableBeans{    
private String name;   
public String getName() {
return name;
}      
public void setName(String name) {
this.name = name;
}
public LifeCycleBean() {
// TODO Auto-generated constructor stub
System.out.println("Learning lifecycle - COnstructor invoked"+name);
}
@Override
public void destroy() throws Exception {
System.out.println("Learning lifecycle - Calling Destroy Method");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("Learning lifecycle - afterPropertiesSet 
invoked"+name);
}
//This never got executed
public void init() {
System.out.println("Learning lifecycle - initMethod invoked"+name);
}
@PostConstruct
public void postConstructMethod() {
System.out.println("Learning lifecycle - postConstructMethod 
invoked"+name);
}

@PreDestroy
public void preDestroyMethod() {
System.out.println("Learning lifecycle - preDestroyMethod invoked"+name);
}
}
SpringBoot应用程序

@SpringBootApplication
public class LifeCycleApplication {

public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(LifeCycleApplication.class, args);
System.out.println("going to get bean definition names");
ctx.getBeanDefinitionNames();
LifeCycleBean bean = ctx.getBean(LifeCycleBean.class);
System.out.println("before setting name");
bean.setName("bean");
System.out.println("after setting name");
}
}

如何以及何时看到springboot应用程序中调用的init方法

你为什么要这么做?它是某个界面的一部分吗?它是有意义的!。。但我只是试图将spring初始化的传统XML方式中的init方法与spring引导应用程序的关系联系起来。可以为spring引导应用程序显式指定init方法吗?或者我应该只依赖@PostConstruct注释?基本上,没有xml配置,@PostConstruct是init方法的替代品,尽管它们略有不同。Gabriel-我确实同意这些问题与提供的链接类似,但我想了解“细微差别”你指的是什么。即使是这篇文章也没有对这一部分做太多的阐述,或者我无法理解其中微妙的差别。你能解释一下吗?为什么?它是某个界面的一部分吗?它是有意义的!。。但我只是试图将spring初始化的传统XML方式中的init方法与spring引导应用程序的关系联系起来。可以为spring引导应用程序显式指定init方法吗?或者我应该只依赖@PostConstruct注释?基本上,没有xml配置,@PostConstruct是init方法的替代品,尽管它们略有不同。Gabriel-我确实同意这些问题与提供的链接类似,但我想了解“细微差别”你指的是什么。即使是这篇文章也没有对这一部分做太多的阐述,或者我无法理解其中微妙的差别。你能解释一下吗?