java代码中init method和destroy method的三种使用方式

来源:互联网 发布:碎屏幕手机壁纸软件 编辑:程序博客网 时间:2024/05/19 03:45



初始化一个对象(bean)后,立即初始化加载一些数据,在销毁一个对象之前进行垃圾回收等等。


三种方式来实现init method和destroy method:


1.@Bean注解方式:(其中在配置类中用@Configuration注解,且指定扫描的包@ComponentScan("xxxxx"),在              类 中@Bean注解的后面声明initMethod和destroy方法。

       @Bean(initMethod="init" , destroyMethod="destroy")

              用@Configuration注解类,等价与XML中 配置beans;用@Bean标注方法,等价于XML中 配置bean。

             @ComponentScan则是指定需要spring来扫描的包,相当于xml中的context:component-scan属性。                           【@ComponentScan("xxxxx")

            

2.JSR-250注解的方式(需要导入jsr250-api的jar包):

        一个拥有构造方法在内的三个方法的java类中在init和destroy方法上加入了两个注解,@PostConstruct和上边@Bean后的initMethod相同,而@PreDestroy则是和destroyMethod作用相同。

还是在配置类中用@Configuration注解,且指定扫描的包@ComponentScan("xxxxx"),在类中@Bean注解的后面就声不必声明initMethod和destroy方法,只写@Bean就可以了。


3.xml的配置方式:创建的java类与第一种完全一样,而这里是配置文件spring.xml:

             bean里边有id,calss和init以及destroy方法,class指定这个bean对应的类。

       

<?xml version="1.0" encoding="UTF-8"?>  <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.xsd">  <bean id="initOrDestroyTest" class="springTest2.Test3" init-method="init" destroy-method="destroy"></bean></beans>

        在测试的时候使用的是ClassPathxXmlApplicationContext直接加载配置文件,不是Java类

 ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("spring.xml");

而不是AnnotationConfigApplicationContext。


在实际的web应用使用时,可以在web.xml中使用类似下边的配置来加载bean,实现init method:

<servlet-name>dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>      <servlet-name>dispatcher</servlet-name>      <url-pattern>/</url-pattern>    </servlet-mapping> 

这里边没有调用destroy method,原因是spring本身代码就需要我们手动调用销毁bean的方法,像前边的几个例子中的context.close就是。
如果不手动调用这个方法,bean就不会被销毁,也就不会去调用destroy method,这也就是为何这里在web.xml中配置后,启动tomcat 只打印了构造函数和init方法中的内容。





Spring 在容器初始化时将自动扫描 base-package 指定的包及其子包下的所有 class 文件,@ComponentScan("xxxxx"),这里扫描xxxxx包下的所有有@Configuration的类。

  用@Configuration注解类,等价与XML中 配置beans;用@Bean标注方法,等价于XML中 配置bean。

  用@ComponentScan则是指定需要spring来扫描的包,相当于xml中的context:component-scan属性。                        【@ComponentScan("xxxxx")】


整体项目结构:








运行MainTest2控制台输出:



运行MainTest控制台输出:




其中去掉ConfigTest类的@Configuration后,运行MainTest2控制台输出:





当把 ConfigTest2中的@ComponentScan注解改为:@ComponentScan("springTest2.ConfigTest2")后,

运行MainTest2控制台输出




ConfigTest2中的@ComponentScan注解为:@ComponentScan("springTest2"),它会把springTest2包下的所有注解了@Configuration的配置类都扫描了,执行顺序:先执行其它配置类中的方法(即先加载其它类的 无参构造函数,初始化方法。再执行本配置类无参构造函数,初始化方法。销毁方法也是先执行其它配置类的,再执行本配置类的)。


我目前没有搞懂它这个执行顺序是依照的什么执行的,希望看到我这个博客的人,可以留言指导指导我搞清这个问题,非常感谢!!!


























































































































































































































   

阅读全文
0 0
原创粉丝点击