2017-08-18 6 views
5

मेरे आवेदन शुरू होने के बाद मुझे वसंत संदर्भ में बीन्स शुरू करने की आवश्यकता है; वर्तमान में, मैं सेम एक कक्षा में एनोटेशन @Configuration के साथ इस तरह प्रारंभ:आवेदन शुरू होने के बाद वसंत संदर्भ में सेम शुरू करने का सबसे अच्छा तरीका?

@Configuration 
public class AppConfig { 
    @Inject 
    @Bean 
    public BeanA init(param1, param2, etc...) { 
     --- Code to construct bean A --- 
    } 

    @Inject 
    @Bean 
    public BeanB init(param1, param2, etc...) { 
     --- Code to construct bean B --- 
    } 
} 

लेकिन कुछ सेम मैं आवेदन स्टार्टअप के बाद प्रारंभ करने की आवश्यकता तो मेरे दृष्टिकोण एक वर्ग ApplicationReadyEvent में घटना को सुनने बनाने है वसंत और उस वर्ग में सेम शुरू करने के लिए कोड डाल दिया।

@Configuration 
class ApplicationStartingListener implements ApplicationListener<ApplicationReadyEvent>{ 
    ---- Code to init bean here ---- 

    @Override 
    public void onApplicationEvent(ApplicationReadyEvent event) { 
     --- If I put init bean code in here, is it correct? ---- 
    } 
} 

क्या यह सबसे अच्छा तरीका है? या कुछ और बेहतर समाधान हैं?

+0

@PostConstruct –

+0

@DanielC के साथ प्रयास करें। java.lang.IllegalStateException: जीवनचक्र विधि एनोटेशन नो आर्ग विधि की आवश्यकता है: मैं जब मैं PostConstruct टिप्पणी जोड़ने यह त्रुटि आई –

+0

कुछ लिंक जो आप अपने वांछित परिणाम प्राप्त करने में मदद करता हैं। मुझे आशा है कि इससे मदद मिलेगी। [लिंक 1] (http://learningviacode.blogspot.com/2012/07/spring-and-events-and-listeners.html) [link2] (http://www.naturalprogrammer.com/run-code-spring- बूट-एप्लिकेशन-स्टार्टअप /) [लिंक 3] (https://stackoverflow.com/questions/38206676/contextstartedevent-not-broadcasted-on-spring-boot) – Tehmina

उत्तर

3

मैं बीन्स में प्रवेश करने के लिए अन्य दृष्टिकोणों की गणना करूंगा, मैंने मानक दृष्टिकोण और वसंत बूट दृष्टिकोण में दृष्टिकोण को समूहीकृत किया।

स्टैंडर्ड दृष्टिकोण

  1. @PostConstruct: यह सिर्फ एक एनोटेशन है कि एक विधि से चलाता है सेम के बाद यह इनपुट पैरामीटर की अनुमति नहीं है, बनाने है।
  2. @Bean(init-method="somInitMehotd"): यह दृष्टिकोण स्प्रिंग बीन लाइफसाइकिल से पूरी तरह से संबंधित है और इसे बीन निर्माण के बाद बुलाया जाता है, यदि आप @PostConstruct एनोटेशन के साथ किसी अन्य विधि का उपयोग कर रहे हैं, तो @PostConstruct को पहले कॉल किया जाएगा। यह दृष्टिकोण इनपुट पैरामीटर की अनुमति नहीं देता है।
  3. ApplicationListener: यह इंटरफ़ेस संदर्भ लाइफसाइक्ल से संबंधित मानक घटनाओं को सुनने की अनुमति देता है, यह भी अनुकूलित घटनाओं को सुन सकता है। उदाहरण के लिए: एक वर्ग MyAppListener बना सकते हैं और इस मामले में ApplicationListener<ContextRefreshedEvent> लागू करता MyAppListener एक onApplicationEvent विधि है कि एक ContextRefreshedEvent

स्प्रिंग बूट दृष्टिकोण प्राप्त करता

  1. को लागू करेगा धावक: दो बहुत ही उपयोगी इंटरफेस CommandLineRunner और ApplicationRunner दोनों ही अनुप्रयोगकॉन्टेक्स्ट बनने के बाद चलाए जाएंगे, उनमें से दोनों इनपुट पैरामीटर के रूप में बीन्स इंजेक्ट करने की अनुमति देते हैं।

  2. वसंत बूट श्रोताओं: वसंत आवेदन आवेदन संदर्भ से आने वाले मानकों की घटनाओं की तुलना में कुछ अतिरिक्त घटनाएं देता है। घटना में से एक ApplicationReadyEvent है और यह अनुरोध तब प्राप्त होता है जब आवेदन अनुरोध प्राप्त करने के लिए तैयार हो। इन घटनाओं को सुनने के लिए सामान्य रूप से ApplicationReadyEvent का उपयोग करके ApplicationListener लागू करता है।

यहाँ उदाहरण है:

MyBean वर्ग विभिन्न तरीकों कि ऊपर सूचीबद्ध प्रत्येक दृष्टिकोण के लिए बुलाया जाएगा है, हर विधि एक प्रिंट विधि कॉल होगा और कहा कि विधि आदेश मान्य करने के लिए में एक Thread.Sleep है आदेश है कि हर श्रोता कहा जाता है।

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(); 
     } 
    } 
} 

यहाँ ContextRefreshListener वर्ग कि ContextRefreshedEvent सुनने और इसे संभाल नहीं है।

public class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> { 
    @Override 
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { 
     contextRefreshedEvent.getApplicationContext().getBean(MyBean.class).contextInit(); 

    } 
} 

और यह BootListener कि ApplicationReadyEvent कि वसंत आवेदन से आता है प्राप्त होगा।

public class MyBootListener implements ApplicationListener<ApplicationReadyEvent> { 
    @Override 
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { 
     applicationReadyEvent.getApplicationContext().getBean(MyBean.class).bootListenerInit(); 
    } 
} 

और अंत में वसंत बूट आवेदन

@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(); 
     }; 
    } 

} 

उत्पादन होता है:

Post init called 
Bean init called 
Context init called 
Runner init called 
Boot init called 

Post init called उत्पादन

से आता है
@PostConstruct 
public void init(){ 
    this.initByPostconstruct="Post init called"; 

Bean init called इधर-उधर की बात आती है m initMethod मूल्य

@Bean(name = "myBean", initMethod = "beanInit") 
public MyBean getMyBean(){ 
    return new MyBean(); 
} 
} 

Context init calledContextRefreshedEvent

public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { 
    contextRefreshedEvent.getApplicationContext().getBean(MyBean.class).contextInit(); 

} 

Runner init called से आता है आता है CommandLineRunner

@Bean 
public CommandLineRunner getRunner(ApplicationContext ctx){ 
    return (args) -> { 
     ctx.getBean(MyBean.class).runnerInit(); 
    }; 
} 

Boot init called से ApplicationReadyEvent

से आता है

सभी सूचीबद्ध परिदृश्य Spring द्वारा ट्रिगर किए गए थे, मैंने सीधे किसी भी घटना को कॉल नहीं किया था, उन सभी को Spring Framework द्वारा बुलाया गया था।

+0

धन्यवाद। –

संबंधित मुद्दे