我在spring中注册了一个userDAO
<bean id="userDAO" class="com.abc.dao.impl.UserDAOImpl">
并给这个类的方法添加了aop的事务(如果不加事务,那么下面打印的是UserDAOImpl)
在程序中
  UserDAO userDAO = (UserDAO) userDAO.getBean("userDAO");
  System.out.println(userDAO.getClass());
结果打印的是 $Proxy1,但是我想得到的是UserDAOImpl这个类,因为这个类上我给加了annotation,我想读其中的值
高手帮帮忙?

解决方案 »

  1.   

    你声明了什么Annotation呢?如果该Annotation类声明了@Inherited则正常来说$proxy1也应该可以获得这个Annotation的
      

  2.   

    @Target({ElementType.METHOD,ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    public @interface Auth {
    String name();
    enum TYPE{MENU,OPER,DATA} ;
    TYPE type() default TYPE.OPER;
    String fields() default "";
    int order() default 0;
    }
    我是这么定义的,
    @Auth(name = "权限相关的操作")
    public class UserDAOImpl implements UserDAO测试
    可是还是没得到
      

  3.   

    无论CGLIB还是JDK代理,代理之后会动态修改字节码,将你定义的操作插入,代理后的类与原本的类已经不同了,肯定是要对代理后的类也就是$Proxy1进行操作了,理论上说设置成RetentionPolicy.RUNTIME应该能得到,你将动态生成的类反编绎一下,看看Annotation还在不在了
      

  4.   

    JDK的动态代理Handler中可以通过参数Joinpoint来过的目标对象
    但是AOP的事务处理spring已经实现了,是否可以复写它的类?
      

  5.   


    Class clazz = null;
    Object proxy = SpringUtils.getBean(bean);
    if (AopUtils.isJdkDynamicProxy(proxy)) {
    InvocationHandler invo = Proxy.getInvocationHandler(proxy);
    AdvisedSupport advised = (AdvisedSupport) new DirectFieldAccessor(
    proxy).getPropertyValue("advised");
    clazz = advised.getTargetClass();
    } else
    clazz = AopUtils.getTargetClass(proxy);
      

  6.   

    package proxy.dao;
    public interface IsayHello {

    public void sayHello();
    }package proxy.entity;import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;public class InterfaceHandlder implements InvocationHandler {
    public Object targer;
    /**
     * Method InterfaceHandlder
     *
     *
     */
    public InterfaceHandlder() {
    // TODO: Add your code here
    }
    public InterfaceHandlder(Object t){

    this.targer=t;

    } /**
     * Method invoke
     *
     *
     * @param proxy
     * @param method
     * @param args
     *
     @throws Throwable
     *
     * @return
     *
     */
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // TODO: Add your code here

    System.out.println("代理对象的方法开始执行......");
    Object ob=method.invoke(this.targer,args);
    System.out.println("代理对象的方法执行结束");


    return ob;
    }
    }
    package proxy.test;import proxy.entity.*;
    import java.lang.reflect.*;
    import proxy.dao.*;
    public class ProxyTest {

    /**
     * Method ProxyTest
     *
     *
     */
    public ProxyTest() {
    // TODO: Add your code here
    } /**
     * Method main
     *
     *
     * @param args
     *
     */
    public static void main(String[] args) {
    // TODO: Add your code here
    IsayHello ih=new Targer();

    InterfaceHandlder ihHandler=new InterfaceHandlder(ih);
    Class [] interfaces=ih.getClass().getInterfaces();
      

    Object proxy=Proxy.newProxyInstance(Object.Class.getClassloader(),interfaces,ihHandler);
    IsayHello ihProxy=null;


    ihProxy=(IsayHello)proxy;
    ihProxy.sayHello();




    }
    }