Guice Guice tarafından inşa edilmedi örneklerinde AOP'yi izin vermez: Guice AOP Limitations
"örnekleri @ bir tarafından Guice tarafından oluşturulan enjekte-açıklamalı yapılması veya argüman yapıcı olması gerekir"
Bu demektir Bir sağlayıcı ile oluşturulan örnekler AOP için aday olmayacaktır.
Kapak tarafında, Sağlayıcınız belirtilen koşullar altında Guice tarafından başlatıldığı sürece, Sağlayıcınız AOP için bir aday olabilir.
AOP Ek Açıklama: Burada
bu gösterir bir örnek
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
@interface AOPExample {}
Sağlayıcı:
public class ExampleProvider implements Provider<Example> {
@AOPExample
public Example get() {
System.out.println("Building...");
return new Example();
}
}
Hedef Örnek:
public class Example {
@AOPExample
public void tryMe() {
System.out.println("example working...");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Modül:
public class ExampleModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), new LoggingAOP());
bind(Example.class).toProvider(ExampleProvider.class);
}
}
Testi Kodu:
public class Test {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TestModule());
ExampleProvider exampleProvider = injector.getInstance(ExampleProvider.class);
Example example = exampleProvider.get();
example.tryMe();
Example directExample = injector.getInstance(Example.class);
directExample.tryMe();
}
}
Testi Çıkışı: o
start
Building...
end took: 3
example working...
start
Building...
end took: 0
example working...
Bildirimi "örnek çalışma ..." zamanlayıcı kodu ile çevrili değildir. Ancak Provider.get ("Building ...").
Eğer sorunuz şudur: engelleyici (yeni INTERCEPTOR()) bir Guice Sağlayıcı üzerinden sağlanabiliyorsa, cevap hayır demektir. Bu işleve en yakın olanı, modül yapılandırma yöntemindeki requestInjection() öğesini çağırmaktır. Bu, Interceptor'unuzu uygun kod ile enjekte edecektir. Interceptor'unuzdan, başlangıç sırasında yavaş olmanıza neden olacak herhangi bir yükü önlemek için Sağlayıcıları kullanabilirsiniz.
İşte demek istediğim:
Modül:
public class TestModule extends AbstractModule {
@Override
protected void configure() {
bind(String.class).toInstance("One");
bind(String.class).annotatedWith(Names.named("two")).toInstance("Two");
LoggingAOP loggingAOP = new LoggingAOP();
bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), loggingAOP);
requestInjection(loggingAOP);
bind(Example.class).toProvider(ExampleProvider.class);
}
}
keseni: bu sorunuza yanıt
public class LoggingAOP implements MethodInterceptor {
@Inject
private Provider<SomethingThatTakesALongTimeToInit> provider;
public Object invoke(MethodInvocation invocation) throws Throwable {
provider.get()...
System.out.println("start");
long start = System.currentTimeMillis();
Object value = invocation.proceed();
System.out.println("end took: " + (System.currentTimeMillis() - start));
return value;
}
}
Umut.
Neyi engellemeye çalışıyorsunuz? get() yöntemi veya belki bazı ayarlayıcı yöntemleri? –
İdeal olarak, herhangi bir yöntemde, yöntem çağrılarını ve yürütme sürelerini kestiren bir performans izleme sistemine sahibiz. Tembelli bir şekilde başlatıcıyı başlatmak istiyorum. –