在我們的獨立Spring 3.1應用程序中,我們嚴格地將業務邏輯從監視Swing視圖中分離出來。該視圖通過實現EventListener
接口來獲得其信息。如何在運行或啓動時在Spring中禁用服務?
要禁用UI,只需「移除」UI Bean上的所有@Services
即可,以便實現此EventListner的UI類不會被業務邏輯注入。
但如何做到這一點?
這個例子給我們的類的小Oerview:
@Service
public class UI extends JFrame implements EventListener {
@PostConstruct
public void setup() {
// Do all the Swing stuff
setVisible(true);
}
@Override
public void onBusinessLogicUpdate(final State state) {
// Show the state on the ui
}
}
@Service
public class Businesslogic {
@Autowired
public List<EventListener> eventListeners;
public void startCalculation() {
do {
// calculate ...
for (final EventListener listener : this.eventListeners) {
eventlistener.onBusinessLogicUpdate(currentState);
}
}
while(/* do some times */);
}
}
public class Starter {
public static void main(final String[] args) {
final ApplicationContext context = // ...;
if(uiShouldBedisabled(args)) {
// remove the UI Service Bean
}
context.getBean(Businesslogic.class).startCalculation();
}
}