使用Spring的JMX annotation让POJO对象输出到JMX

自JDK5.0 引入注解(Annotation)后,让Java的开发简化了很多,让开发者几 乎摆脱复杂的

配置文件的烦恼。本文将介绍Spring提供的一套相当于Commons Attribut属 性类的注解和一个策略接口 JmxAttributeSource 的实现类 AnnotationsJmxAttributeSource, 这个类允许 MBeanInfoAssembler 来读这些 注解。本文就给大家展示一下,使用Spring的JMX annotation,如何简单快速让 POJO对象输出到JMX.

里面的出现的类名的功能,在下面介绍,先来看一个例子:

先编写一个POJO对象

1 import java.util.ArrayList;
2 import java.util.List;
3
4 import org.springframework.jmx.export.annotation.ManagedAttribute;
5 import org.springframework.jmx.export.annotation.ManagedOperation;
6 import org.springframework.jmx.export.annotation.ManagedOperationParameter;
7 import org.springframework.jmx.export.annotation.ManagedOperationParameters;
8 import org.springframework.jmx.export.annotation.ManagedResource;
9
10 //实例标记为由JMX管理的资源
11 @ManagedResource(objectName="bean:name=testJmxBean", description="My Managed Bean", log=true,
12 logFile="jmx.log", currencyTimeLimit=15, persistPolicy="OnUpdate", persistPeriod=200,
13 persistLocation="foo", persistName="bar")
14 public class AnnotationTestBean {
15
16 private String name;
17 private int age;
18
19 private List<String> values;
20
21 //把getter或setter标记为JMX的属性
22 @ManagedAttribute(description="The Age Attribute", currencyTimeLimit=1)
23 public int getAge() {
24 return age;
25 }
26
27 public void setAge(int age) {
28 this.age = age;
29 }
30
31 @ManagedAttribute(description="The values Attribute", currencyTimeLimit=1)
32 public List<String> getValues() {
33 values = new ArrayList<String>(2);
34 values.add("hello");
35 values.add("world");
36 return values;
37 }
38
39 @ManagedAttribute(description="The Name Attribute",
40 currencyTimeLimit=20,
41 defaultValue="bar",
42 persistPolicy="OnUpdate")
43 public void setName(String name) {
44 this.name = name;
45 System.out.println("set: " + name);
46 }
47
48 @ManagedAttribute(defaultValue="foo", persistPeriod=300)
49 public String getName() {
50 System.out.println("get: " + name);
51 return name;
52 }
53
54 //把方法标记为JMX的操作
55 @ManagedOperation(description="Add two numbers")
56 @ManagedOperationParameters({
57 @ManagedOperationParameter(name = "x", description = "The first number"),
58 @ManagedOperationParameter(name = "y", description = "The second number")})
59 public int add(int x, int y) {
60 return x + y;
61 }
62
63 public void dontExposeMe() {
64 throw new RuntimeException();
65 }
66 }
67

时间: 2024-08-30 04:21:38

使用Spring的JMX annotation让POJO对象输出到JMX的相关文章

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat

springaop事务-Spring 中的Annotation 关于@Pointcut 的问题

问题描述 Spring 中的Annotation 关于@Pointcut 的问题 package com.bjsxt.aop; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; impo

安卓开发需不需要封装pojo对象

问题描述 安卓开发需不需要封装pojo对象?公司的项目中没有封装pojo对象,而是为了和服务器进行json通信,都做成一个类中的各种json字符串,我以前搞j2ee的,习惯了面向对象,习惯将需要的都封装成类,用起来觉得happy,现在安卓这么做是有自己的道理,还是公司的项目不规范? 解决方案 JSON用来做数据交换,当然简单的应用无所谓封装不封装.倘若接受到JSON后,还需要做一列数据处理,那么封装成对象必然好.解决方案二:实体类若没有在多个操作数据库的方法中使用(WEB开发就是service层

Java的Spring框架中DAO数据访问对象的使用示例_java

Spring DAO之JDBC  Spring提供的DAO(数据访问对象)支持主要的目的是便于以标准的方式使用不同的数据访问技术, 如JDBC,Hibernate或者JDO等.它不仅可以让你方便地在这些持久化技术间切换, 而且让你在编码的时候不用考虑处理各种技术中特定的异常. 为了便于以一种一致的方式使用各种数据访问技术,如JDBC.JDO和Hibernate, Spring提供了一套抽象DAO类供你扩展.这些抽象类提供了一些方法,通过它们你可以 获得与你当前使用的数据访问技术相关的数据源和其他

输出对象-jfinal中如何将这个对象输出

问题描述 jfinal中如何将这个对象输出 jfinal中如何对象输出对象 BufferedImage image = new BufferedImage(width height BufferedImage.TYPE_INT_RGB); //画笔 Graphics graphics = image.getGraphics(); //这个是验证码数字 char [] rands = generatedCheckCode(); //这个是验证码数字和画笔结合起来的验证码 drawRands(gra

java中的合并流问题,不能合并三个输入流对象输出

问题描述 java中的合并流问题,不能合并三个输入流对象输出 解决方案 这个程序运行没有问题,需求是将1,2,3三个文本中的内容全部写到4文本中,但是现在只能写出1文本中的内容,2和3都不能写出,

Spring MVC 使用POJO对象绑定请求参数值

index.jsp前台页面加上这些,把值输入之后,通过post提交到后台. <form action="springmvc/testPOJO" method="post"> username: <input type="text" name="username"/> <br/> password: <input type="password" name="

Spring jdbc getJdbcTemplate() queryForObject如何返回对象

问题描述 这是我的代码 希望您花几分钟不到的时间帮我看一下 我搞了1天了 楞是不明白一运行就报异常捕获在持久层:Incorrect column count: expected 1, actual 3这个错误 结果网上搜不到这个异常是个什么 public User IsQueryOne(String id) throws Exception {try{User user= (User) this.getJdbcTemplate().queryForObject("select * from te

annotation hibernate pojo

问题描述 我有2张表source表user表sidintprimarykey,uidintprimarykey,namevarchar(12),namevarchar(12),u_idintforeignkeyreferencesusersource多对一user帮我用annotation写上面的pojo类要双向关联 解决方案 解决方案二:User表:importjava.util.HashSet;importjava.util.Set;importjavax.persistence.Casca