示例:
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) //原型模式
public class C {
}
@Component
public abstract class D {
public void hello(){
C c = getC();
System.out.println(c);
}
@Lookup
public abstract C getC();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext
= new AnnotationConfigApplicationContext(ConfigBean.class);
applicationContext.getBean(D.class).hello();
applicationContext.getBean(D.class).hello();
applicationContext.getBean(D.class).hello();
applicationContext.getBean(D.class).hello();
applicationContext.getBean(D.class).hello();
}
结果:
com.lucky.srm.spring.C@5fd9b663
com.lucky.srm.spring.C@214894fc
com.lucky.srm.spring.C@10567255
com.lucky.srm.spring.C@e362c57
com.lucky.srm.spring.C@1c4ee95c
总结:
场景:如果想在D类,每次都能够获取到最新的C类
1.C使用原型模式 2.D使用抽象方法设置C类同时用@Lookup 注解到方法
好像有点蛋疼的注解,到目前没用过。