Spring 自动装配...将源类传递给自动装配类

2023-12-01

陷入了一个奇怪的要求。我需要将唯一的错误 ID 附加到 log4j 消息并将该消息 ID 返回到接口。所以,我虽然让我们创建一个 Spring 服务,如下所示

public class LoggingService {

   protected static Logger logger = LoggerFactory.getLogger(LoggingService.class);



   public String debug(String debug_msg)
   {
       String uniqueMsgId = generateUniqueId();
       logger.debug(concatIdWithMsg(uniqueMsgId, debug_msg));
       return uniqueMsgId;
   }

  }

并将其自动连接到我需要的任何地方。

 public class LoginLogoutController {

    @Autowired
    LoggingService logger;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String getLoginPage()
    {
       logger.debug("Login page requested");
    }
    }

虽然它工作正常,但 logger msg 中的源类是 LoggingService,这是显而易见的。我想要的是传递 LoggingService 自动装配的类,以便记录器消息显示问题的原始来源。我尝试以某种方式改变服务 但不知道如何传递源类

 public class LoggingService<T> {

       protected static Logger logger = null;

       Class<T> sourceClass;

       public void construct(Class<T> sourceClass)
       {
           this.sourceClass = sourceClass;
           logger = LoggerFactory.getLogger(sourceClass);
       }

       public String debug(String debug_msg)
       {
           String uniqueMsgId = generateUniqueId();
           logger.debug(concatIdWithMsg(uniqueMsgId, debug_msg));
           return null;
       }
    }

我使用这种机制来注入记录器。

创建此注释..

/**
* Indicates InjectLogger of appropriate type to
* be supplied at runtime to the annotated field.
*
* The injected logger is an appropriate implementation
* of org.slf4j.Logger.
*/
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target(FIELD)
@Documented
public @interface InjectLogger {
}

现在让我们定义一个实际执行注入记录器实现工作的类。

/**
 * Auto injects the underlying implementation of logger into the bean with field
 * having annotation <code>InjectLogger</code>.
 * 
 */
import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;

import static org.springframework.util.ReflectionUtils.FieldCallback;

public class LoggerInjector implements BeanPostProcessor {

 public Object postProcessAfterInitialization(Object bean, String beanName)
   throws BeansException {
  return bean;
 }

 public Object postProcessBeforeInitialization(final Object bean,
   String beanName) throws BeansException {
  ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
   public void doWith(Field field) throws IllegalArgumentException,
     IllegalAccessException {
    // make the field accessible if defined private
    ReflectionUtils.makeAccessible(field);
    if (field.getAnnotation(InjectLogger.class) != null) {
     Logger log = LoggerFactory.getLogger(bean.getClass());
     field.set(bean, log);
    }
   }
  });
  return bean;
 }
}

使用它更加简单。只需将上面创建的 Logger 注解添加到所需类中的 Log 字段即可。

import org.slf4j.Logger;

public class Demo {

 @InjectLogger
 private Logger log;

 public void doSomething() {
  log.info("message");
  log.error("Lets see how the error message looks...");
 }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring 自动装配...将源类传递给自动装配类 的相关文章

随机推荐