关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

  1. 通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
  2. 通过 在xml中定义init-method 和  destory-method方法
  3. 通过bean实现InitializingBean和 DisposableBean接口

 

1@PostConstruct 和 @PreDestroy 方法

1)实现类:

public classPersonService { 

    private String  message; 

 

    public String getMessage() { 

        return message; 

    } 

    public void setMessage(String message){ 

        this.message = message; 

    } 

     

    @PostConstruct 

    public void init(){ 

        System.out.println("I'm  init method  using  @PostConstrut...."+message); 

    } 

     

    @PreDestroy 

    public void dostory(){ 

        System.out.println("I'm  destory method  using @PreDestroy....."+message); 

    } 

2)配置:

<?xmlversion="1.0" encoding="UTF-8"?> 

<beansxmlns="http://www.springframework.org/schema/beans" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:context="http://www.springframework.org/schema/context" 

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

 

<context:annotation-config/> 

 

<beanid="personService"class="com.myapp.core.annotation.init.PersonService"> 

  <property name="message"value="123"></property> 

</bean> 

 

</beans>

3)测试:

public static voidmain(String[] args) { 

        ApplicationContext  context = newClassPathXmlApplicationContext("resource/annotation.xml"); 

        PersonService   personService  = (PersonService)context.getBean("personService"); 

         

        personService.dostory(); 

输出:

I'm  init method  using  @PostConstrut....123

I'm  destory method  using @PreDestroy.....123

 

2在xml中定义init-method 和  destory-method方法

1)实现类:

public classPersonService  { 

    private String  message; 

 

    public String getMessage() { 

        return message; 

    } 

    public void setMessage(String message){ 

        this.message = message; 

    } 

 

    public void init(){ 

       System.out.println("init"); 

    } 

   

    public void cleanUp(){ 

       System.out.println("cleanUp"); 

    } 

}

2)配置:

<beanid="personService"class="com.myapp.core.beanscope.PersonService"scope="singleton" init-method="init" destroy-method="cleanUp"> </bean> 

3)测试:

public static voidmain(String[] args) { 

       

    AbstractApplicationContext  context =new ClassPathXmlApplicationContext("SpringBeans.xml"); 

    PersonService  person =(PersonService)context.getBean("personService"); 

    person.setMessage("hello  spring"); 

     

    PersonService  person_new =(PersonService)context.getBean("personService"); 

     

   System.out.println(person.getMessage()); 

   System.out.println(person_new.getMessage()); 

   

context.registerShutdownHook(); 

输出:

init

hello  spring

hello  spring

cleanUp

注:context.registerShutdownHook();是一个钩子方法,当jvm关闭退出的时候会调用这个钩子方法,在设计模式之模板模式中 通过在抽象类中定义这样的钩子方法由实现类进行实现,这里的实现类是AbstractApplicationContext,这是spring 容器优雅关闭的方法

 

3实现InitializingBean和 DisposableBean接口

1定义相应类实现InitializingBean ,DisposableBean接口

public classPersonService  implementsInitializingBean,DisposableBean{ 

    private String  message; 

 

    public String getMessage() { 

        return message; 

    } 

    public void setMessage(String message){ 

        this.message = message; 

    } 

 

    @Override 

    public void destroy() throws Exception{ 

        // TODO Auto-generated method stub 

        System.out.println("I'm  init method  using implementsInitializingBean interface...."+message); 

         

    } 

 

    @Override 

    public void afterPropertiesSet() throwsException { 

        // TODO Auto-generated method stub 

        System.out.println("I'm  init method  using implementsDisposableBean interface...."+message);  

    } 

}

2)配置:

<?xmlversion="1.0" encoding="UTF-8"?> 

<beansxmlns="http://www.springframework.org/schema/beans" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:context="http://www.springframework.org/schema/context" 

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

 

<beanid="personService"class="com.myapp.core.annotation.init.PersonService"> 

          <property name="message"value="123"></property> 

</bean> 

</beans>

3)测试:

public static voidmain(String[] args) { 

         

        AbstractApplicationContext  context = newClassPathXmlApplicationContext("resource/annotation.xml"); 

        PersonService   personService  = (PersonService)context.getBean("personService"); 

         

        context.registerShutdownHook(); 

}

输出:

I'm  init method  using implementsDisposableBean interface....123 

三月 16, 2013 5:06:34下午 org.springframework.context.support.AbstractApplicationContext doClose 

INFO: Closingorg.springframework.context.support.ClassPathXmlApplicationContext@205756:startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy 

I'm  init method  using implementsInitializingBean interface....123  

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐