2017-03-01 27 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