Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to call a method after bean initialization is complete

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 7.61k
    Comment on it

    Hello Friends, If you Want to call the method after your bean is initialize in spring you can use the following options.

    1. Use the afterProprtiesSet method.

      class MyClass implements InitializingBean {

          public void afterPropertiesSet() throws Exception {
             System.out.println("my bean is initialized");
          }
       }
      

    2:- You can use the annotation @PostConstruct in your class. to enable this you need to define in your application context xml file.

    class MyClass  implements InitializingBean {
    
        @PostConstruct
         public void initMethod() throws Exception {
           System.out.println("my bean is ready");
             }
        }
    

    3:- You can also use the init method attribute in your application context xml file.

    <bean id="myclass" class="com.MyClass" init-method="initMethod">
      </bean>
    

    and make the function name initMethod in your MyClass.java file

    package com;
    class MyClass {
    
      public void initMethod() throws Exception {
         System.out.println("my bean is ready");
      }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: