java 无法在日志附加器中自动连接spring bean

qf9go6mv  于 2023-03-21  发布在  Java
关注(0)|答案(3)|浏览(130)

我正在尝试在我创建的appender下自动连接SQSFIFOJavaClient。我打印出在Sping Boot main class中定义的bean名称,并且我能够看到SQSFIFOJavaClient bean定义。然而,在下面列出的class中没有自动连接bean。SQSAppender和SQSFIFOJavaClient都在不同的包中,但在添加到component-scan的同一个基本包下。你能告诉我我错过了什么吗?

@Component
@DependsOn("SQSFIFOJavaClient")
public class SQSAppender extends AppenderBase<ILoggingEvent> {

    @Autowired
    private SQSFIFOJavaClient sqsClient;

我还尝试添加DependsOn注解,但sqsClient仍然显示为null。

x7rlezfr

x7rlezfr1#

Log4j2将通过调用@PluginFactory annotated方法(按照this answer)创建appender。您的appender可能不是作为Spring bean创建的,最有可能的是调用了new SQSAppender(...),并且没有处理@Autowired
关于如何实现Spring管理的Log4j2 appender的示例可以在org.springframework.amqp.rabbit.log4j2.AmqpAppender中找到。

30byixjq

30byixjq2#

appender是在spring初始化之前创建的。这是我找到的解决方案:

我的自定义添加器.java:

@Plugin(name = "MyCustomAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class MyCustomAppender extends AbstractAppender {
  ...
  private static MyBean myBean;

  public static void setMyBean(MyBean myBean) {
      MyCustomAppender.MyBean = myBean;
  }

  @PluginFactory
  public static MyCustomAppender createAppender(
        @PluginAttribute("name") String name,
        @PluginAttribute("ignoreExceptions") boolean ignoreExceptions,
        @PluginElement("Layout") Layout layout,
        @PluginElement("Filters") Filter filter,
        @PluginElement("Properties") Property[] properties) {
      return new MyCustomAppender(name, filter, layout, ignoreExceptions, properties);
  }

  @Override
  public void append(LogEvent event) {
      if (myBean != null) {
          myBean.doSomething();
      }
  }

MyBean.java:

@Component
public class MyBean {
  ...
  @PostConstruct
  public void init() {
      MyCustomAppender.setMyBean(this);
  }
  ...
}
e5nqia27

e5nqia273#

其他解决方案:

MyServiceApplicationContext.java:

@Service
public class MyServiceApplicationContext implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }
}

我的自定义添加器.java:

@Plugin(name = "MyCustomAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class MyCustomAppender extends AbstractAppender {
  
  private MyBean myBean;

  protected MyCustomAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, Property[] properties) {
    super(name, filter, layout, ignoreExceptions, properties);
}

  @PluginFactory
  public static MyCustomAppender createAppender(
        @PluginAttribute("name") String name,
        @PluginAttribute("ignoreExceptions") boolean ignoreExceptions,
        @PluginElement("Layout") Layout layout,
        @PluginElement("Filters") Filter filter,
        @PluginElement("Properties") Property[] properties) {
      return new MyCustomAppender(name, filter, layout, ignoreExceptions, properties);
  }

  @Override
  public void append(LogEvent event) {
      if (myBean == null) {
        // the appender is called before spring initialization
        ApplicationContext context = MyServiceApplicationContext.getApplicationContext();
        if (context != null) {
            myBean = context.getBean(MyBean.class);
        }
    
    }
      if (myBean != null) {
          myBean.doSomething();
      }
  }

相关问题