package com.wisely.scope;
import org.springframework.stereotype.Service;
@Service//默认scope为singleton
public class DemoSingletonService {
}
package com.wisely.scope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class DemoPrototypeService {
}
package com.wisely.scope;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.scope");
//分别从spring容器里获得两次实例,为singleton时共享一个实例,用prototype是又新建了一个实例
DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class);
DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class);
System.out.println("s1与s2是否相等:"+s1.equals(s2));
System.out.println("p1与p2是否相等:"+p1.equals(p2));
context.close();
}
}
输出
s1与s2是否相等:true
p1与p2是否相等:false
原文:http://wiselyman.iteye.com/blog/2210377