In the spring the container three ways to achieve initialization methods and methods of destruction as bean

  Container-managed bean's life cycle, we can customize the initialization and destruction methods; in the bean container to the current life cycle of time to call the initialization and destruction methods our custom.

  Here are three ways to initialize and destroy the bean demonstration.

  The first, using the initMethod @Bean own annotations () and destroyMethod ().

  The second, and achieve InitializingBean DisposableBean interface afterPropertiesSet () for and destroy () method.

  Third, you can use JSR250, @ PostConstruct (created in the bean and property assignment is completed; to perform initialization method) and @PreDestroy (let us clean up work before the container destroyed bean)

public class Car {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Car() {
        System.out.println("Car Constructor.....");
    }
    public void  init()
    {
        System.out.println("Car init .....");
    }
    public void destory()
    {
        System.out.println("Car destory .....");
    }
}
public class Cat implements InitializingBean, DisposableBean {
    public Cat() {
        System.out.println("Cat 构造方法 。。。。。。");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("Cat 销毁方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Cat 初始化方法 。。。。。。");
    }
}@Configuration
 
 
class Dog {public 

public Dog () {
System.out.println ( "Dog Construct ......");
}

// After the object is created and assigned call
@PostConstruct
public void the init ()
{
System.out.println ( "Dog ..... @PostConstruct the init");
}
@PreDestroy
public void Destory ()
{
System.out.println ( "Dog Destory @PreDestroy .....");
}
. @Import (Dog class )
@Configuration
public class Config1 {

    @Bean(initMethod = "init",destroyMethod = "destory")
    public Car car (){
        return new Car();
    }
    @Bean
    public Cat cat(){
        return new  Cat();
    }
}
public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Config1.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String n:beanDefinitionNames)
        {
            System.out.println(n);
        }
        context.close();
    }
}

operation result:

dog construct......
dog init @PostConstruct .....
13:41:29.489 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'car'
Car Constructor.....
Car init .....
13:41:29.522 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'cat'
Cat 构造方法 。。。。。。
Cat 初始化方法 。。。。。。
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config1
com.yuan.beancycle.model.Dog
car
cat
13:41:29.577 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@544a2ea6, started on Mon Aug 05 13:41:29 CST 2019
Disconnected from the target VM, address: '127.0.0.1:64811', transport: 'socket'
Cat 销毁方法
Car destory .....
dog destory  @PreDestroy .....

 

Guess you like

Origin www.cnblogs.com/mingyuan1031/p/11302488.html