基于注解code注射液简单的技术

基于注解code注射液简单的技术

本文介绍了Java的:基于注解code注射液简单的技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让这个code的工作?

Is there a way to make this code work?

LogonControl.java

LogonControl.java

@Audit(AuditType.LOGON)
public void login(String username, String password) {
 // do login
}

AuditHandler.java

AuditHandler.java

public void audit(AuditType auditType) {
 // persist audit
}

残局是,即每次登录()被调用,审计()也称为,用适当audittype

Endgame being, that each time login() is called, audit() is also called, with the appropriate audittype.

我想象AOP可能是解决这一点,但我想它要尽可能(AspectJ的教程我已经看了通常有非常令人费解的注释)。

I imagine AOP is probably the solution to this, but I would like it to be as simple as possible (the AspectJ tutorials I've looked at normally have very convoluted annotations).

请注意:我不希望有predefine将调用审核,我在写这一个可扩展的框架方法,和其他人可能需要使用它

Note: I don't want to have to predefine the methods that will call audit, I'm writing this for an extensible framework, and others may need to use it.

推荐答案

使用反射很容易只是标注有@Audit的方法,就像JUnit测试参赛者:

Using reflection is easy just annotate a method with @Audit, just like test runners in JUnit:

public interface Login {

    void login(String name, String password);
 }

public class LoginImpl implements Login {

    @Audit(handler = LoginHandler.class)
    public void login(String name, String password) {
        System.out.println("login");
    }

}

@Audit定义为:

@Audit is defined as:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Audit {

   Class<? extends Handler> handler();
}

,其中处理器是:

where Handler is:

interface Handler {

    void handle();
}

class LoginHandler implements Handler {

    public void handle() {
        System.out.println("HANDLER CALLED!");
    }
}

和现在真正的code:

and now the real code:

public class LoginFactory {

    private static class AuditInvocationHandler implements InvocationHandler {

        private final Login realLogin;

        public AuditInvocationHandler(Login realLogin) {
            this.realLogin = realLogin;
        }

        public Object invoke(Object proxy, Method method, Object[] args)
                      throws Throwable {
            Method realMethod = realLogin.getClass().getMethod(
                                        method.getName(),
                                        method.getParameterTypes());
            Audit audit = realMethod.getAnnotation(Audit.class);

            if (audit != null) {
                audit.handler().newInstance().handle();
            }

            return method.invoke(realLogin, args);
        }
    }

    public static Login createLogin() {
        return (Login) Proxy.newProxyInstance(
                LoginFactory.class.getClassLoader(),
                new Class[]{Login.class},
                new AuditInvocationHandler(new LoginImpl()));
    }
}

@Test

    Login login = LoginFactory.createLogin();
    login.login("user", "secret");
    login.logout();

输出:


HANDLER CALLED!
login
logout

这篇关于Java的:基于注解code注射液简单的技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:31