我能够测试在单例bean中自动装配原型bean的结果是只创建一个原型bean。使用AOP作用域代理自动装入单例bean中的原型bean
作为一个解决方案,我读了我可以定义原型bean的AOP作用域代理或使用Spring的查找方法注入。
这是我曾尝试 -
PrototypeBean.java
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.INTERFACES)
public class PrototypeBean implements Prototype {
private String welcomeMessage;
public String getWelcomeMessage() {
return welcomeMessage;
}
public void setWelcomeMessage(final String welcomeMessage) {
this.welcomeMessage = welcomeMessage;
}
}
SingletonBean.java
@Component
public class SingletonBean implements Singleton{
@Autowired private Prototype prototype;
public Prototype getPrototype() {
return prototype;
}
public void greet() {
System.out.println(prototype.getWelcomeMessage());
}
}
测试类
public class AutowiredDependenciesDemo {
@Autowired private Singleton autowiredSingleton;
@Autowired ConfigurableApplicationContext context;
@Test
public void testPrototypeBeanWithAopScopedProxy(){
Assert.assertNotNull(autowiredSingleton);
Prototype prototypeBean = (Prototype) ((SingletonBean) autowiredSingleton).getPrototype();
prototypeBean.setWelcomeMessage("hello world");
autowiredSingleton.greet();
Singleton contextSingleton = (Singleton) context.getBean("singletonBean");
Assert.assertSame(autowiredSingleton, contextSingleton);
Prototype anotherPrototypeBean = (Prototype) ((SingletonBean)contextSingleton).getPrototype();
anotherPrototypeBean.setWelcomeMessage("hello india");
contextSingleton.greet();
autowiredSingleton.greet();
// i expected both the prototype instances to be different. in the debugger, it does show two different 'proxied' instances. however the test fails.
Assert.assertNotSame(prototypeBean, anotherPrototypeBean);
}
我在这里失去了一些东西?此外,对greet()方法的调用返回null。
是的,我也看到了类似的情况。但是,我试图调用Proxied bean上的特定方法。在我的测试类中,我调用了'prototypeBean.setWelcomeMessage(“hello world”);''和'anotherPrototypeBean.setWelcomeMessage(“hello india”);''我期望这会让Spring创建两个Prototype bean每PrototypeBeanImpl内存地址),但后来的Assert语句并没有证实我的观察结果。 – user3842182
每次调用此代理将创建一个Prototype Scoped bean的新实例。如果你想获得这样的Prototype Scoped bean的实例并调用它的多个方法,那么你应该使用Lookup Method方法。 –
它仍然是相同的代理(如答案解释)。如果您想比较生成的对象,则需要展开代理对象并获取对象的基础实际实例。\ –