ApplicationContext,讀取bean、屬性、屬性值,demo:
public class HutuAbstractTopicEventcode {
private String ctxPath = "C:/happyday/demo.xml";
private String moduleName = "happybaby";
public static void main(String[] args) {
HutuAbstractTopicEventcode o = new HutuAbstractTopicEventcode();
// 提取出所有topic/eventcode
Map<String, Set<String>> topicEventcodes = o.abstractTopicEventcode();
// 用於計數
int count = 0;
// 打印.
for(Map.Entry<String, Set<String>> entry : topicEventcodes.entrySet()){
String topic = entry.getKey();
Set<String> eventcodes = entry.getValue();
for(String eventcode : eventcodes){
System.err.println(topic + ", " + eventcode);
count ++;
}
}
System.err.println("共計: " + count);
}
/**
* 從xml里提取出所有topic/eventcode,並轉化為map類型.
*
* @return
*/
@SuppressWarnings("unchecked")
public Map<String/*topic*/, Set<String/*eventcode*/>> abstractTopicEventcode() {
ApplicationContext context = new FileSystemXmlApplicationContext(ctxPath);
// ApplicationContext context = new ClassPathXmlApplicationContext(ctxPath);
TopicMessageConfig newConfigObject = (TopicMessageConfig) context.getBean(moduleName);
Set<String> attributeSet = this.getAttributeNames(newConfigObject);
for (String attributeName : attributeSet) {
System.err.println("屬性名稱" + attributeName);
System.err.println("屬性值:" + this.getAttributeVlaue(newConfigObject, attributeName));
return (Map<String, Set<String>>) this
.getAttributeVlaue(newConfigObject, attributeName);
}
return null;
}
// ~~~ 內部方法 ~~~
/**
* 獲取該bean里的全部屬性名稱.
*
* @param newConfigObject
* @return
*/
private Set<String> getAttributeNames(TopicMessageConfig newConfigObject) {
PropertyDescriptor[] propertyDescriptors = BeanUtils.getBeanInfo(newConfigObject)
.getPropertyDescriptors();
Set<String> attributeNames = new HashSet<String>();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if (!propertyDescriptor.getName().equals("class")) {
if (propertyDescriptor.getReadMethod() != null
&& propertyDescriptor.getWriteMethod() != null) {
attributeNames.add(propertyDescriptor.getName());
}
}
}
return attributeNames;
}
/**
* 獲取該bean中,對應該屬性名的屬性值.
*
* @param configObject
* @param attributeName
* @return
*/
private Object getAttributeVlaue(Object configObject, String attributeName) {
PropertyDescriptor[] propertyDescriptors = BeanUtils.getBeanInfo(configObject)
.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if (propertyDescriptor.getName().equals(attributeName)) {
try {
if (propertyDescriptor.getReadMethod() != null) {
return propertyDescriptor.getReadMethod().invoke(configObject);
}
} catch (InvocationTargetException e) {
this.handleException("get", attributeName, e);
} catch (IllegalAccessException e) {
this.handleException("get", attributeName, e);
}
}
}
return null;
}
/**
* 處理異常.
*
* @param opName
* @param name
* @param e
*/
private void handleException(String opName, String name, Exception e) {
StringBuilder buf = new StringBuilder();
buf.append(opName).append("屬性失敗,moduleName:").append(this.moduleName);
buf.append(", propertyName:").append(name);
LOGGER.error(buf.toString(), e);
throw new RuntimeException(buf.toString(), e);
}
}
