@Loggable
public class BookService {
@Inject
@ThirteenDigits
private NumberGenerator numberGenerator;
public Book createBook(String title, BigDecimal price, String description) {
Book book = new Book(title, price, description);
book.setIsbn(numberGenerator.generateNumber());
return book;
}
}
@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {
}
@Interceptor
@Loggable
public class LoggingInterceptor {
@Inject
private Logger logger;
@AroundInvoke
public Object logMethod(InvocationContext ic) throws Exception {
logger.entering(ic.getTarget().getClass().getName(), ic.getMethod().getName());
try {
return ic.proceed();
} finally {
logger.exiting(ic.getTarget().getClass().getName(), ic.getMethod().getName());
}
}
}
public class LoggingProducer {
@Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}