2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
異常說(shuō)明了一切 - attribute1 bean 是在應(yīng)用程序初始化期間創(chuàng)建的(通過(guò)會(huì)話作用域 bean),但沒(méi)有與請(qǐng)求綁定的線程。您還應(yīng)該代理您的 attribute1 bean,因?yàn)槟鷮⑵渥⑷氲絾卫▽傩?2 服務(wù)。)

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
基于 Alexander.Furer 給出的見(jiàn)解。我創(chuàng)建了自己的作用域,并管理它來(lái)調(diào)用 bean 提供者,以便在Attribute1方法的每次訪問(wèn)中都擁有新鮮的 bean。
為此,我擴(kuò)展了以下范圍:
// Register scope as "runtime"
public class RuntimeScope implements Scope {
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
return objectFactory.getObject();
}
...
}
新Attribute1服務(wù):
@Service
public class Attribute1Service {
@Resource
private BeanSession beanSession;
public void setDefaultValue() {
Configuration configuration = beanSession.getRootState();
configuration.getAttribute1().setValue("VALUE 1");
}
@Bean
@Scope(value = "runtime", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Attribute1 attribute1() {
Configuration configuration = beanSession.getRootState();
return configuration.getAttribute1();
}
}
消費(fèi)者Attribute2服務(wù):
@Service
public class Attribute2Service {
@Resource
private BeanSession beanSession;
@Resource
private Processor processor;
@Resource
private Attribute1 attribute1;
public void defineAttribute2() {
Configuration configuration = beanSession.getRootState();
String value = processor.getValue(configuration, attribute1.getValue()); // Will call Attribute1 service to require the fresh bean
configuration.getAttribute2().setValue(value);
}
public void defineAttribute3() {
Configuration configuration = beanSession.getRootState();
String value = processor.getValue(configuration, attribute1.getValue()); // Will call Attribute1 service to require the fresh bean
configuration.getAttribute3().setValue(value);
}
}
我沒(méi)有看到的問(wèn)題是 Attribute1 應(yīng)該是處理 bean 實(shí)例化的代理。因此,通過(guò)創(chuàng)建我自己的范圍,我可以保證訪問(wèn) attribute1(由Attribute2Servicewith生成attribute1.getValue())方法將創(chuàng)建一個(gè)新的 bean(由 提供Attribute1Service)。
添加回答
舉報(bào)