我能夠測試在單例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方法。 –
它仍然是相同的代理(如答案解釋)。如果您想比較生成的對象,則需要展開代理對象並獲取對象的基礎實際實例。\ –