Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to initialize method using Spring-beans

    • 0
    • 2
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 347
    Comment on it

    Hello Guy's
    Bellow code will help you to initialize method using Spring-beans.Here, I am using init and destroy method as attribute in bean configuration for bean to perform assigned actions upon initialization and destruction.
    Follow the bellow steps to develop your own project :

    Step :1
    Create FindnerdService class and put bellow code .

    package com.bhagwan.services;
     
    public class FindnerdService
    {
            String propertyMessage;
     
            public String getMessage() {
              return propertyMessage;
            }
     
            public void setMessage(String propertyMessage) {
              this.propertyMessage = propertyMessage;
            }
     
            public void initIt() throws Exception {
              System.out.println("Init method after properties are set : " + propertyMessage);
            }
     
            public void cleanUp() throws Exception {
              System.out.println("Spring Container is destroy!");
            }
     
    }
    

    Step :2
    Create Spring-Service.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
            <bean id="springService" class="com.bhagwan.services.FindnerdService" 
                    init-method="initIt" destroy-method="cleanUp">
     
                    <property name="propertyMessage" value="property message" />
            </bean>
     
    </beans>
    

    Step :3
    Create App class and put bellow code

    package com.bhagwan.common;
     
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    import com.bhagwan.services.FindnerdService;
     
    public class App 
    {
        public static void main( String[] args )
        {
            ConfigurableApplicationContext context = 
                    new ClassPathXmlApplicationContext(new String[] {"Spring-Service.xml"});
     
            FindnerdService findnerd = (FindnerdService)context.getBean("springService");
     
            System.out.println(findnerd);
     
            context.close();
        }
    }
    

    Note : Please run App class and enjoy

 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: