请考虑以下代码:
public class UserJMXBeanExpose{
private UserJMXBeanImpl userJMXBean;
public UserJMXBeanExpose(UserJMXBeanImpl userJMXBean) {
this.userJMXBean = userJMXBean;
}
public void init() throws MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException, MalformedObjectNameException {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("MineStar:type=UserJMXBeanExpose,name=Statistics");
MineStarMBean impl = new MineStarMBean();
impl.addProperty(new PropertyAdapter("User", "User List", "java.lang.String") {
public Object getValue() {
if (userJMXBean == null) return 0;
return userJMXBean.getUserAttributes();
}
});
server.registerMBean(impl, objectName);
}
}
这段代码:
public class UserJMXBeanImpl implements UserJMXBean {
private List<User> userList = new ArrayList<>();
public void addUser(User user){
userList.add(user);
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public String[] getUserAttributes(){
for(User user : this.getUserList()){
return new String[]{user.getUserId(),user.getRoles()};
}
return null;
}
}
当用户登录类userjmxbeanimpl存储数据时。但是,我需要在添加新用户后立即激活userjmxbeanexpose类,以便它可以公开从impl类中获取的数据。我怎样才能做到这一点? 弹簧配置如下:
<bean id="userJMXBeans" class="minestar.platform.domain.user.UserJMXBeanExpose" init-method="init" scope="singleton"
lazy-init="false">
<constructor-arg ref="userJMXBean"/>
</bean>
答案 0 :(得分:0)
我不确定它代表什么逻辑,但是根据你提供的代码来判断...你需要做的唯一事情就是工作:
public void addUser(User user){
userList.add(user);
new UserJMXBeanExpose(this).init();
}
考虑到Observer模式似乎在这里工作之后,很多样板代码基本上都会做同样的事情并没有带来任何好处。除非我误解了对activate UserJMXBeanExpose
的含义。