spring笔记-scope

1.内置scope

@Component
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class SingletonTestService {
    public String getHelloMessage() {
        return "Hello " + this.getClass().getName();
    }
}

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ProtoTypeTestService {
    public String getHelloMessage() {
        return "Hello " + this.getClass().getName();
    }
}
@Component
@RequestScope
public class RequestTestService {
    public String getHelloMessage() {
        return "Hello " + this.getClass().getName();
    }
}
@Component
@SessionScope
public class SessionTestService {
    public String getHelloMessage() {
        return "Hello " + this.getClass().getName();
    }
}
@Component
@ApplicationScope
public class ApplicationTestService {
    public String getHelloMessage() {
        return "Hello " + this.getClass().getName();
    }
}

测试代码

@Controller
public class SampleController implements ApplicationContextAware {


    private ApplicationContext applicationContext;

    @GetMapping("/hello")
    @ResponseBody
    public String helloWorld() {

        test(SingletonTestService.class);
        test(ProtoTypeTestService.class);
        test(RequestTestService.class);
        test(SessionTestService.class);
        test(ApplicationTestService.class);
        return "";
    }


    <T> boolean test(Class<T> cls)
    {
        T obj1=applicationContext.getBean(cls);
        T obj2=applicationContext.getBean(cls);

        boolean bFlag=(obj1==obj2);

        System.out.println("begin");
        System.out.println(cls);
        System.out.println(obj1);
        System.out.println(obj2);
        System.out.println("end");
        return bFlag;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}

结果输出:

第一次:

begin
class sample.jetty.service.SingletonTestService
sample.jetty.service.SingletonTestService@23201692
sample.jetty.service.SingletonTestService@23201692
end
begin
class sample.jetty.service.ProtoTypeTestService
sample.jetty.service.ProtoTypeTestService@6a3b8a3d
sample.jetty.service.ProtoTypeTestService@30eca8a3
end
begin
class sample.jetty.service.RequestTestService
sample.jetty.service.RequestTestService@4dc430a0
sample.jetty.service.RequestTestService@4dc430a0
end
begin
class sample.jetty.service.SessionTestService
sample.jetty.service.SessionTestService@483e60cf
sample.jetty.service.SessionTestService@483e60cf
end
begin
class sample.jetty.service.ApplicationTestService
sample.jetty.service.ApplicationTestService@5fa1592c
sample.jetty.service.ApplicationTestService@5fa1592c
end

第二次

begin
class sample.jetty.service.SingletonTestService
sample.jetty.service.SingletonTestService@23201692
sample.jetty.service.SingletonTestService@23201692
end
begin
class sample.jetty.service.ProtoTypeTestService
sample.jetty.service.ProtoTypeTestService@1a24fae5
sample.jetty.service.ProtoTypeTestService@1e1b6d79
end
begin
class sample.jetty.service.RequestTestService
sample.jetty.service.RequestTestService@5b19f0de
sample.jetty.service.RequestTestService@5b19f0de
end
begin
class sample.jetty.service.SessionTestService
sample.jetty.service.SessionTestService@483e60cf
sample.jetty.service.SessionTestService@483e60cf
end
begin
class sample.jetty.service.ApplicationTestService
sample.jetty.service.ApplicationTestService@5fa1592c
sample.jetty.service.ApplicationTestService@5fa1592c
end

参考:
http://blog.csdn.net/elim168/article/details/75581612

2.自定义scope

1.实现Scope接口
2.声明CustomScopeConfigurer实例,并将自定义Scope加入其中

@Configuration
public class AppConfig {

    @Bean
    public CustomScopeConfigurer configurer()
    {
        CustomScopeConfigurer item=new CustomScopeConfigurer();
        item.addScope("myScope",new MyScope());
        return item;
    }
}

@Component
@Scope("myScope")
public class CustomScopeTestService {
    public String getHelloMessage() {
        return "Hello " + this.getClass().getName();
    }

}

参考:
http://blog.csdn.net/elim168/article/details/75581670

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,076评论 25 708
  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 11,597评论 12 197
  • 今天礼拜二,全国又开始大范围降温了。各位要多加点衣服哦。 昨天晚上,看新闻说罗志祥的女友整容,以前的照片很丑,然后...
    Doris2426阅读 215评论 0 0
  • 景景三页阅读 171评论 0 2
  • 过润扬,观长江,长江天长。 斜阳西,归无锡,锡山无锡。
    竹影灯阅读 207评论 0 0

友情链接更多精彩内容