简体   繁体   English

布尔变量必须在diff spring bean中共享

[英]Boolean variable has to be shared in diff spring bean

I have two beans 我有两个豆

<bean id="eventService" class="xxx.xxxx.xxxxx.EventServiceImpl">
</bean>

<bean id="UpdateService" class="xxx.xxxx.xxxxx.UpdateServiceImpl">
</bean>

A Boolean variable has to be shared... means updating Boolean in a bean should be available for other bean to know the status 必须共享一个布尔变量...意味着在一个bean中更新Boolean应该可以让其他bean知道状态

appreciate your idea guys 感谢您的想法

The fact that you have multiple objects interested in a the same flag sounds very much like an event. 您有多个对同一个标志感兴趣的对象这一事实听起来很像一个事件。 Have a look at Spring's support from broadcasting and listening to events (including custom events). 看看Spring在广播和收听事件(包括自定义事件)方面的支持。

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#context-functionality-events http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#context-functionality-events

What you could do is have one managed object be the "owner" of that flag, and broadcast state changes to anyone else that's interested via an event. 您可以做的是让一个托管对象成为该标志的“所有者”,并通过事件将状态更改广播给其他感兴趣的任何人。

I'm afraid you will need some value holder bean, something like this: 恐怕您将需要一些值持有人bean,如下所示:

public class ValueHolder{
    private boolean flag;
    public boolean isFlag(){return flag;}
    public void setFlag(boolean flag){this.flag=flag;}
}

Wire this as a Spring Bean and inject it into your service beans. 将其连接为Spring Bean,并将其注入到服务bean中。

Just for completeness: or you could use a static field, but that's even uglier. 只是为了完整性:或者您可以使用静态字段,但这更加丑陋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM