java注解疑问 注解能不能实现类似下面这样的效果我给一个类的方法注解,当这个方法执行前输出123,方法执行后输出345, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 sping注解可以实现。 就是AOP的应用 import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@interface Ticmy { public String before() default "123"; public String after() default "345";}interface Service { public void mymethod(); public void mymethod2();}class ServiceImpl implements Service { @Ticmy public void mymethod() { System.out.println("in mymethod"); } @Ticmy(before="before mymethod2", after="after mymethod2") public void mymethod2() { System.out.println("in mymethod2"); } private ServiceImpl() {/*直接new注解没有效果*/} public static Service getInstance() { //也可以使用其它方式 return (Service)Proxy.newProxyInstance(Service.class.getClassLoader(), new Class[]{Service.class}, new TicmyInvocationHandler(new ServiceImpl())); } static class TicmyInvocationHandler implements InvocationHandler { private Object delegation; public TicmyInvocationHandler(Object delegation) { this.delegation = delegation; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //这里只判断接口上的方法是否有注解 Method methodInImpl = delegation.getClass().getMethod(method.getName(), method.getParameterTypes()); Ticmy ticmy = methodInImpl.getAnnotation(Ticmy.class); Object ret; if(ticmy != null) { //如果有注解 System.out.println(ticmy.before()); ret = method.invoke(delegation, args); System.out.println(ticmy.after()); } else { ret = method.invoke(delegation, args); } return ret; } }}public class MyAnnotation { public static void main(String[] args) { Service instance = ServiceImpl.getInstance(); instance.mymethod(); System.out.println("--------"); instance.mymethod2(); }} public class a和class a 类型安全问题请教,路过的大侠请看过来. 关于JAVA本身的类库的问题``` 请教 ibm开发者的网址? 请问JTable 表格中 能加按纽吗 在线等!!! 老问题,谁有这个程序,朋友要用的 Java里基于TCP/IP的Socket通信中一帧数据最好是多大(内详) Jbuilder 7.0 连接 Oracle 数据库 SOAP的命名空间问题 求Java大神教导怎么做学生成绩管理系统 Headfirst java 562面的困惑 classpath 设置疑问
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Ticmy {
public String before() default "123";
public String after() default "345";
}interface Service {
public void mymethod();
public void mymethod2();
}
class ServiceImpl implements Service {
@Ticmy
public void mymethod() {
System.out.println("in mymethod");
}
@Ticmy(before="before mymethod2", after="after mymethod2")
public void mymethod2() {
System.out.println("in mymethod2");
}
private ServiceImpl() {/*直接new注解没有效果*/}
public static Service getInstance() {
//也可以使用其它方式
return (Service)Proxy.newProxyInstance(Service.class.getClassLoader(),
new Class[]{Service.class}, new TicmyInvocationHandler(new ServiceImpl()));
}
static class TicmyInvocationHandler implements InvocationHandler {
private Object delegation;
public TicmyInvocationHandler(Object delegation) {
this.delegation = delegation;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//这里只判断接口上的方法是否有注解
Method methodInImpl = delegation.getClass().getMethod(method.getName(), method.getParameterTypes());
Ticmy ticmy = methodInImpl.getAnnotation(Ticmy.class);
Object ret;
if(ticmy != null) {
//如果有注解
System.out.println(ticmy.before());
ret = method.invoke(delegation, args);
System.out.println(ticmy.after());
} else {
ret = method.invoke(delegation, args);
}
return ret;
}
}
}public class MyAnnotation {
public static void main(String[] args) {
Service instance = ServiceImpl.getInstance();
instance.mymethod();
System.out.println("--------");
instance.mymethod2();
}
}