使用Java注释来注入依赖关系logger [英] Using java annotation to inject logger dependency
问题描述
我使用的春天方面-J注释支持,允许一个 @Loggable
注释。这允许在基于该结构的类自动记录。
I am using spring with aspect-j annotation support to allow for an @Loggable
annotation. This allows automatic logging on a class based on the configuration.
我想知道如果我能以某种方式使用此批注暴露一个SLF4J 日志
变量到类直接使用,所以我没有做一些事来效果:
I am wondering if I can somehow use this annotation to expose an slf4j Logger
variable into the class for direct use, so that I don't have to do something to the effect of:
Logger logger = LoggerFactory.getLogger(MyClass.class);
这将是很好,如果上面是隐式可由于注释和我可以去这样做 logger.debug(...);
无宣言。我不知道这甚至有可能。
It would be nice if the above was implicitly available due to the annotation and I could just go about doing logger.debug("...");
without the declaration. I'm not sure if this is even possible.
推荐答案
您可以使用的BeanPostProcessor
接口,这是由所谓的ApplicationContext
所有创建的bean,所以你必须填写相应属性的机会。
You can use the BeanPostProcessor
interface, which is called by the ApplicationContext
for all created beans, so you have the chance to fill the appropriate properties.
我创建了一个简单的实现,其中这是否:
I created a simple implementation, which does that:
import java.lang.reflect.Field;
import java.util.List;
import net.vidageek.mirror.dsl.Mirror;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class LoggerPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
List<Field> fields = new Mirror().on(bean.getClass()).reflectAll().fields();
for (Field field : fields) {
if (Logger.class.isAssignableFrom(field.getType()) && new Mirror().on(field).reflect().annotation(InjectLogger.class) != null) {
new Mirror().on(bean).set().field(field).withValue(LoggerFactory.getLogger(bean.getClass()));
}
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
您不必做任何复杂的注册步骤,因为的ApplicationContext
能够识别的的BeanPostProcessor
实例并自动注册。
You don't have to do any complex registration step, since the ApplicationContext
is capable of recognizing BeanPostProcessor
instances and automatically register them.
的 @InjectLogger
注释是:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectLogger {
}
然后你就可以轻松地使用注释:
And then you can easily use the annotation:
public static @InjectLogger Logger LOGGER;
...
LOGGER.info("Testing message");
我用镜库中查找注释字段,但很明显,你可以为了执行手动查找避免这种额外的依赖关系。
I used the Mirror library to find the annotated fields, but obviously you may perform a manual lookup in order to avoid this additional dependency.
它实际上是一个不错的主意,以避免重复code,以及来自复制和粘贴从其他类一样,当我们忘记了日志
定义,即使是小问题更改类
参数,从而导致错误日志。
It's actually a nice idea to avoid repeated code, and even small issues that come from copying and paste the Logger
definitions from other classes, like when we forget to change the class
parameter, which leads to wrong logs.
这篇关于使用Java注释来注入依赖关系logger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!