如何使用org.jboss.varia.property.SystemPropertiesService和org.jboss.util.property.PropertyListener [英] How to use org.jboss.varia.property.SystemPropertiesService and org.jboss.util.property.PropertyListener

查看:85
本文介绍了如何使用org.jboss.varia.property.SystemPropertiesService和org.jboss.util.property.PropertyListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我已经看到了jboss-service.xml,它使用扩展的SystemPropertiesService类来引用自定义属性文件.但是我还不完全了解这种用法​​. 有人可以给我一些帮助,以了解如何使用这两个类吗?谢谢.

I have seen the jboss-service.xml which use an extended SystemPropertiesService class to reference to custom property file. But I didn't totally understood this kind of usage yet. Could someone please give me some help to understand how to use these two class? thanks.

推荐答案

SystemPropertiesService对定义可以从您的应用程序访问的属性非常有用,它通常用于对应用程序进行参数设置,而无需更改代码,甚至无需更改代码应用程序包(假设您将jboss-service.xml放置在de war/ear/jar结构之外).例如,您可以创建具有以下内容的myapp-service.xml文件:

The SystemPropertiesService is very useful to define properties that then can be accessed from your application, it's usually used to parametrize the application without having to change to code, or even the application package (provided you place the jboss-service.xml outside de war / ear / jar structure). For example, you can create a myapp-service.xml file with the following content:

<server>
 <mbean code="org.jboss.varia.property.SystemPropertiesService" name="jboss:type=Service,name=MyAppProperties">
 <!-- Define the properties directly in the service.xml file-->
 <attribute name="Properties">
     myapp.property1=property1Value
     myapp.property2=property2Value
 </attribute>
 <!-- You can also specify a route to another file where you define properties-->
 <attribute name="URLList">
     /home/myuser/txtlist.properties
 </attribute>
 </mbean>
</server>

然后,您可以直接在JBoss中部署此文件,定义的属性将对部署在同一JBoss中的所有应用程序可见,并且您将能够使用静态方法访问它们:

Then you can deploy this file directly in JBoss, the properties defined will be visible to all the applications deployed in the same JBoss and you'll be able to access them with the static method:

String System.getProperty(String propertyName)

因此,如果要从应用程序访问myapp.property1的值,请执行以下操作:

So if you want to access to the value of myapp.property1 from your application you'd do:

String property = System.getProperty("myapp.property");

另一方面,PropertyListener实际上是一个定义侦听器的接口,当与属性发生任何事件时将触发该侦听器. org.jboss.util.property.PropertyAdapter是此接口的抽象实现.要使用它,您必须实现其三种方法(propertyAdded,propertyChanged,propertyRemoved),当分别添加,更改或删除属性时,容器将调用它们.这些方法都有一个PropertyEvent对象作为参数,让您知道受影响的属性.

On the other hand the PropertyListener is really an interface that defines a listener that will be triggered when any event occurs with a property. The org.jboss.util.property.PropertyAdapter is an abstract implementation of this interface. To use it you've to implement its three methods (propertyAdded, propertyChanged, propertyRemoved), that will be called by the container when a property is added, changed or removed respectively. Those methods have a PropertyEvent object as parameter, which let you know the property affected.

当您希望应用程序每次属性更改时都做某事(一个不好的实现是您每隔一段时间检查一次属性更改)时,此接口/类会很有用,这样,当JBoss检测到某个属性具有更改其值,它将调用相应的方法(您应该以所需的行为实现).

This interface/class is useful when you want your application to do something every time a property changes (a bad implementation would be that you check every certain time for a property change), this way, when JBoss detects that a property has changed its value, it will call the respective method (that you should implement with the behaviour you want).

例如,如果要在每次更改时打印新属性值,则可以通过以下方式实现propertyChanged方法:

For example, if you want to print the new property value everytime it's changed you could implement the propertyChanged method this way:

void propertyChanged (PropertyEvent pe){
    // check the property that has changed
    if (pe.getPropertyName().equals("myapp.property1")){
         System.out.println("The value of " + pe.getPropertyName() + " has changed to " + pe.getPropertyValue());
    }
}

查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆