2017-03-01 69 views
1

我正在尝试使用字节Buddy与更大的应用程序。我现在的想法是只用@Advice在方法输入/退出时记录一些内容。我的代理正确附加到应用程序并构建。在日志中,我也可以看到尖角类的转换也完成了。当我在需要RestEndpoint和方法被称为发送请求的问题是,我得到错误:类未找到异常与字节好友Java代理程序

javax.servlet.ServletException: A MultiException has 1 exceptions. They are: 
1. java.lang.NoClassDefFoundError: com/agent/MyAdviser 

at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:391) 
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) 

我的经纪人:

LOG.info("Before Agent Builder build !!!"); 
      new AgentBuilder.Default() 
        .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager()) 
        .type(is(MyClassToCatch.class)) 
        .transform(
         new AgentBuilder.Transformer.ForAdvice() 
         .include(MyAgent.class.getClassLoader()) 
         .advice(ElementMatchers.any(), MyAdviser.class.getName()) 
       ) 
       .installOn(inst); 

而且MyAdviser.class是:

public class MyAdviser { 

private static final Logger LOG = LoggerFactory.getLogger(MyAdviser.class); 

@Advice.OnMethodEnter 
public static void onEnterExit() { 
    LOG.info("INTERCEPTED BBB <<<>>> BBB"); 
} 

是不知怎的,这个问题与类加载器相关? BR,

拉斐尔的解决方案帮助。

编辑:我试过也拦截的方法,并调用它没有任何变化,但我已经有这样的错误结束:

com.agent.DiscoveryAgent - On Error of : MyClassToCatch  None of [net.bytebuddy.implement[email protected]7815f1c, net.bytebuddy.implement[email protected]a193f70f, net.bytebuddy.implement[email protected]d6fdc355, net.bytebuddy.implement[email protected]66e2275b, net.bytebuddy.implement[email protected]19cb065f, net.bytebuddy.implement[email protected]4fc13971, net.bytebuddy.implement[email protected]aea74e0e, net.bytebuddy.implement[email protected]2ac04890, net.bytebuddy.implement[email protected]f5eef57c, net.bytebuddy.implement[email protected]e1b04a0f, net.bytebuddy.implement[email protected]f0de1c86] allows for delegation from public javax.ws.rs.core.Response MyClassToCatch.someMethod() 

回答

0

在咨询类的代码被复制到目标类中。这意味着它需要能够解决MyAdviser类。如果这是不可能的,你必须:

  1. 定义Logger如通过defineField在转化类中的字段和@FieldValue注解。
  2. 从通知内动态读取记录器,并引用另一个可视为日志根的类。
+0

嗨拉斐尔!感谢您的帮助,我终于做到了,并按预期注销了一些东西。 – Macko