spring容器初始化bean和销毁bean之前进行一些操作的方法

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

        第一种,通过在xml中定义init-method和destory-method方法

        第二种,通过bean实现InitializingBean和 DisposableBean接口

        第三种,通过Spring @PostConstruct和@PreDestroy方法

一.通过在xml中定义init-method和destory-method方法

        在xml中配置init-method和 destory-method方法,只是定义spring容器在初始化bean和容器销毁之前的所做的操作。

1.xml中配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-4.1.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                    ">

    <bean id="testService" class="com.bijian.study.service.impl.TestService" init-method="afterPropertiesSet" destroy-method="destroy">  
          <property name="message" value="123"></property>  
	</bean>
	
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">   
        <property name="viewClass"  
            value="org.springframework.web.servlet.view.JstlView" />   
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />   
    </bean>
</beans>

2.定义TestService类

package com.bijian.study.service.impl;

public class TestService {

    private String  message;  
  
    public String getMessage() {  
        return message;  
    }  
  
    public void setMessage(String message) {  
        this.message = message;  
    }
    
    public void afterPropertiesSet() throws Exception {
        System.out.println("I'm  init  method  using  @PostConstrut...."+message);
    }  

    public void destroy() throws Exception {
        System.out.println("I'm  destory method  using  @PreDestroy....."+message);
    }
}  

3.相应的测试类

package com.bijian.study.test;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bijian.study.service.impl.TestService;

public class MainTest {

    public static void main(String[] args) {
        
        try {
            AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:/develop/eclipse/workspace/SpringMVC/WebContent/WEB-INF/springMVC-servlet.xml");  
            
            TestService testService  =  (TestService)context.getBean("testService");
            
            System.out.println(testService.getMessage());
            //testService.destroy();
            context.registerShutdownHook();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果:

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

        可以看出init-method方法和destroy-method方法都已经执行了。

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

二.通过bean实现InitializingBean和 DisposableBean接口

1.xml中配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-4.1.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                    ">

    <bean id="testService" class="com.bijian.study.service.impl.TestService">  
          <property name="message" value="123"></property>  
	</bean>
	
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">   
        <property name="viewClass"  
            value="org.springframework.web.servlet.view.JstlView" />   
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />   
    </bean>
</beans>

2.定义TestService类

package com.bijian.study.service.impl;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class TestService implements InitializingBean,DisposableBean{  
    
    private String  message;  
  
    public String getMessage() {  
        return message;  
    }  
  
    public void setMessage(String message) {  
        this.message = message;  
    }  

    public void afterPropertiesSet() throws Exception {
        System.out.println("I'm  init  method  using  @PostConstrut...."+message);
    }  

    public void destroy() throws Exception {
        System.out.println("I'm  destory method  using  @PreDestroy....."+message);
    }
}  

3.测试类同上,运行测试类。

运行结果:

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

三.通过Spring @PostConstruct和@PreDestroy方法

1.xml中配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-4.1.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                    ">
    
	<context:component-scan base-package="com.bijian.study"></context:component-scan>   

    <bean id="testService" class="com.bijian.study.service.impl.TestService">  
          <property name="message" value="123"></property>  
	</bean>
	
	<context:annotation-config />
	
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">   
        <property name="viewClass"  
            value="org.springframework.web.servlet.view.JstlView" />   
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />   
    </bean>
</beans>

    其中<context:annotation-config />告诉spring 容器采用注解配置,扫描注解配置。

2.定义TestService类

package com.bijian.study.service.impl;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class TestService {  
    
    private String  message;  
  
    public String getMessage() {  
        return message;  
    }  
  
    public void setMessage(String message) {  
        this.message = message;  
    }
    
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        System.out.println("I'm  init  method  using  @PostConstrut...."+message);
    }  
    
    @PreDestroy
    public void destroy() throws Exception {
        System.out.println("I'm  destory method  using  @PreDestroy....."+message);
    }
}  

3.测试类同上,运行测试类。

运行结果:

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

        其中也可以通过申明加载org.springframework.context.annotation.CommonAnnotationBeanPostProcessor类来告诉Spring容器采用的 常用解配置的方式,只需要修改配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-4.1.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                    ">
	
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">   
        <property name="viewClass"  
            value="org.springframework.web.servlet.view.JstlView" />   
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />   
    </bean>
     
	<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
	<bean id="testService" class="com.bijian.study.service.impl.TestService">  
		<property name="message" value="123"></property>
	</bean>
</beans>

        最后,Srping的bean可以通过注解配置方法注入。如下所示:

1.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-4.1.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                    ">
    
	<context:component-scan base-package="com.bijian.study"></context:component-scan>   

    <bean id="testService" class="com.bijian.study.service.impl.TestService">  
          <property name="message" value="123"></property>  
	</bean>
	
	<context:annotation-config />

    <mvc:annotation-driven></mvc:annotation-driven>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">   
        <property name="viewClass"  
            value="org.springframework.web.servlet.view.JstlView" />   
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />   
    </bean>
</beans>

2.定义TestService类

package com.bijian.study.service.impl;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Service;

@Service("testService")
public class TestService { 
    
    private String  message;  
  
    public String getMessage() {  
        return message;  
    }
  
    public void setMessage(String message) {  
        this.message = message;  
    }
    
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        System.out.println("I'm  init  method  using  @PostConstrut...."+message);
    }  
    
    @PreDestroy
    public void destroy() throws Exception {
        System.out.println("I'm  destory method  using  @PreDestroy....."+message);
    }
}

参考文章:

http://blog.csdn.net/topwqp/article/details/8681467

http://blog.csdn.net/topwqp/article/details/8681497

猜你喜欢

转载自bijian1013.iteye.com/blog/2374256
今日推荐