spring-多例对象prototype-生命周期方法问题

多例对象spring容器只负责创建不负责回收

@Lazy(true) //配置延迟加载
@Scope("prototype")//默认是singleton

@Lazy(true)和@Scope("prototype")同时出现时,这样写无效
延迟加载仅针对于单例 作用域对象

//ElementType.TYPE 类, ElementType.METHOD 方法ElementType.CONSTRUCTOR构造方法//ElementType.PARAMETER参数, ElementType.FIELD属性
//@Retention(RetentionPolicy.RUNTIME)运行时有效,运行时可通过反射获取其中的内容
//@Documented注解以文档的形式呈现,使用javadoc指令可形成文档
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {

    /**
     * Whether lazy initialization should occur.
     */
    boolean value() default true;

}

/spring-ioc-v5/src/main/java/utils/OpenDataSource.java

package utils;

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

import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
 * @Component 描述类为一个一般的bean对象
 *
 */
@Component
@Scope("prototype")//默认是singleton
public class OpenDataSource {//key默认为类名,首字母小写
    /**
     * @PostConstruct 注解描述的方法为生命周期方法中的初始化方法
     * 要求jdk1.7
     */
    @PostConstruct
    public void init(){
        System.out.println("OpenDataSource.init()");
    }
    /**
     * @PreDestroy 注解描述的方法为生命周期销毁时要调用的方法
     * 要求jdk1.7
     */
    @PreDestroy
    public void close(){
        System.out.println("OpenDataSource.close()");
    }
}

/spring-ioc-v5/src/main/java/config/AppRootConfig.java

package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
 * 使用此配置类替换spring-configs.xml
 * @Configuration 描述这个类是个配置类
 * @ComponentScan 用于定义要扫描的具体包
 */

@Configuration
@ComponentScan("utils") //由spring扫描本报以及子类中的包
public class AppRootConfig {
}
package test;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import config.AppRootConfig;

public class TestBase {
    protected AnnotationConfigApplicationContext ctx;

    @Before
    public void init(){
        ctx = new AnnotationConfigApplicationContext(AppRootConfig.class);
    }
    @After
    public void close(){
        ctx.close();
    }
}
package test;

import org.junit.Test;

import utils.OpenDataSource;

public class TestOpenDataSource01 extends TestBase{
    @Test
    public void testOpenDataSource(){
        //获取bean对象
        OpenDataSource ds1 = ctx.getBean("openDataSource",OpenDataSource.class);
        OpenDataSource ds2 = ctx.getBean("openDataSource",OpenDataSource.class);
        //测试bean对象是否为空
        System.out.println(ds1==ds2);
    }
}

pom.xml


  4.0.0
  com.gq
  spring-ioc-v5
  0.0.1-SNAPSHOT
    
        
            org.springframework
            spring-context
            4.3.9.RELEASE
        
        
        
            junit
            junit
            4.12
        
        


    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    


运行结果

可以看到并没有调用close()方法,因为类配置了多例

九月 06, 2018 3:54:25 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@368239c8: startup date [Thu Sep 06 15:54:25 CST 2018]; root of context hierarchy
OpenDataSource.init()
OpenDataSource.init()
false
九月 06, 2018 3:54:26 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@368239c8: startup date [Thu Sep 06 15:54:25 CST 2018]; root of context hierarchy

你可能感兴趣的:(spring-多例对象prototype-生命周期方法问题)