如何在Spring bean创建后初始化log4j2?

时间:2017-03-06 07:45:46

标签: java spring log4j2

我想从我的配置bean中获取一些log4j2属性但问题是Spring在log4j2启动后初始化这个bean。有没有办法在log4j2提供程序之前初始化这个bean?

<NoSql name="elasticsearchTimeAppender">
        //I will set below values from Spring conf. bean
        <Mongo cluster="" host="" port="" index="" type="" flushInterval=""/>
</NoSql>

这是我的提供者;

@Plugin(name = "Mongo", category = "Core", printObject = true)
public class MongoProvider implements NoSqlProvider<MongoConnection> {
    ....
    host = MyConfiguration.DatabaseName; //I am getting null value here because Spring initializes after provider completed.
}

这是我的配置bean;

@Component
public class MyConfiguration {

  public static String DatabaseName;

  @PostConstruct
  private void setStaticFields() {
    MyConfiguration.DatabaseName= databaseName;
  }

  @Value("${mydbconf.name}")
  private String databaseName;



  public String getDatabaseName() {
    return DatabaseName;
  }

  public void setDatabaseName(String DatabaseName) {
    MyConfiguration.DatabaseName = DatabaseName;
  }


}

0 个答案:

没有答案