Fasulyeleri inmek için diğer yaklaşımları sıralayacağım, yaklaşımı Standart Yaklaşım ve Bahar Önyükleme Yaklaşımında gruplandırdım.
örnektir: MyBean sınıfı yukarıda listelenen her bir yaklaşım için çağrılır farklı yöntem vardır, her yöntem, bir baskı yöntemi arayacak ve bu yöntem doğrulamak amacıyla bir Thread.sleep var Her dinleyicinin çağrıldığı sıra. İşte
import javax.annotation.PostConstruct;
public class MyBean {
private String myVar="";
public MyBean(){
}
@PostConstruct
public void postConstructInit(){
this.myVar="Post init called";
print();
}
public void beanInit(){
this.myVar="Bean init called";
print();
}
public void contextInit(){
this.myVar="Context init called";
print();
}
public void runnerInit(){
this.myVar="Runner init called";
print();
}
public void bootListenerInit(){
this.myVar="Boot init called";
print();
}
public void print(){
System.out.println(this.myVar);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
ContextRefreshedEvent
dinlemek ve idare edecek ContextRefreshListener
sınıftır. public class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
contextRefreshedEvent.getApplicationContext().getBean(MyBean.class).contextInit();
}
}
Ve Bahar Uygulama geliyor ApplicationReadyEvent
alacaktır BootListener
olduğunu.
public class MyBootListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
applicationReadyEvent.getApplicationContext().getBean(MyBean.class).bootListenerInit();
}
}
Ve nihayet Bahar Önyükleme Uygulaması
@SpringBootApplication
public class StackoverflowBootApplication {
public static void main(String[] args) {
SpringApplication.run(StackoverflowBootApplication.class, args);
}
@Bean(name = "myBean", initMethod = "beanInit")
public MyBean getMyBean(){
return new MyBean();
}
@Bean
public ContextRefreshListener getContextRefreshedListener(){return new ContextRefreshListener();}
@Bean
public MyBootListener getBootListener(){return new MyBootListener();}
@Bean
public CommandLineRunner getRunner(ApplicationContext ctx){
return (args) -> {
ctx.getBean(MyBean.class).runnerInit();
};
}
}
çıktısı:
Post init called
Bean init called
Context init called
Runner init called
Boot init called
Post init called
çıkış
@PostConstruct
public void init(){
this.initByPostconstruct="Post init called";
Bean init called
fro geliyor
geliyor CommandLineRunner
@Bean
public CommandLineRunner getRunner(ApplicationContext ctx){
return (args) -> {
ctx.getBean(MyBean.class).runnerInit();
};
}
Boot init called
ApplicationReadyEvent
gelen gelen @Bean(name = "myBean", initMethod = "beanInit")
public MyBean getMyBean(){
return new MyBean();
}
}
Context init called
ContextRefreshedEvent
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
contextRefreshedEvent.getApplicationContext().getBean(MyBean.class).contextInit();
}
Runner init called
gelen initMethod
değerini m gelir
Listelenen tüm senaryolar Spring
tarafından tetiklendi, doğrudan olaylardan herhangi birini çağırdım, hepsi Spring Framework
tarafından çağrıldı.
@PostConstruct ile Dene –
@DanielC. PostConstruct ek açıklaması eklediğimde bu hatayı aldım: java.lang.IllegalStateException: Lifecycle yöntemi notu, no-arg yöntemini gerektirir: –
İstediğiniz sonuçları elde etmenize yardımcı olan bazı bağlantılar vardır. Umarım yardımcı olur. [link1] (http://learningviacode.blogspot.com/2012/07/spring-and-events-and-listeners.html) [bağlantı2] (http://www.naturalprogrammer.com/run-code-spring- boot-application-startup /) [link3] (https://stackoverflow.com/questions/38206676/contextstartedevent-not-broadcasted-on-spring-boot) – Tehmina