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 "";
    }


     boolean test(Class 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

你可能感兴趣的:(spring笔记-scope)