title: log4j2 JDBCAppender with Spring
body: <p>I have already defined a datasource in Spring configuration.</p>
  
  <pre><code>&lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/db" /&gt;
      &lt;property name="username" value="root" /&gt;
      &lt;property name="password" value="password" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>For LOG4J2 JDBCAppender, I know we need to obtain the datasource:</p>
  
  <pre><code> &lt;JDBC name="databaseAppender" tableName="error_log"&gt;
          &lt;ConnectionFactory class="net.example.db"
              method="getDatabaseConnection" /&gt;
          &lt;Column name="EVENT_DATE" isEventTimestamp="true" /&gt;
          &lt;Column name="LEVEL" pattern="%level" /&gt;
          &lt;Column name="LOGGER" pattern="%logger" /&gt;
          &lt;Column name="MESSAGE" pattern="%message" /&gt;
          &lt;Column name="THROWABLE" pattern="%ex{full}" /&gt;
      &lt;/JDBC&gt;
  </code></pre>
  
  <p>I have already added the package into the component-scan:</p>
  
  <pre><code>&lt;context:component-scan base-package="com.test,net.example.db" /&gt;
  </code></pre>
  
  <p>And add annotation at the class ConnectionFactory as a Component</p>
  
  <pre><code>import java.sql.Connection;
  import javax.sql.DataSource;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  @Component
  public class ConnectionFactory {
  
      @Autowired
      private DataSource dataSource;
  
      public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      public Connection getDatabaseConnection() throws Exception {
          return dataSource.getConnection();
      }
  
  }
  </code></pre>
  
  <p>However, I am getting null when call getDatabaseConnection()</p>
  
  <pre><code>2017-01-14 14:17:24,579 localhost-startStop-1 ERROR JdbcDatabaseManager jdbcManager{ description=databaseAppender, bufferSize=0, connectionSource=factory{ public static java.sql.Connection net.example.db.ConnectionFactory.getDatabaseConnection() }, tableName=error_log, columns=[ { name=EVENT_DATE, layout=null, literal=null, timestamp=true }, { name=LEVEL, layout=%level, literal=null, timestamp=false }, { name=LOGGER, layout=%logger, literal=null, timestamp=false }, { name=MESSAGE, layout=%message, literal=null, timestamp=false }, { name=THROWABLE, layout=%ex{full}, literal=null, timestamp=false } ] } Could not perform database startup operations: java.sql.SQLException: Failed to obtain connection from factory method. java.sql.SQLException: Failed to obtain connection from factory method.
      at org.apache.logging.log4j.core.appender.db.jdbc.FactoryMethodConnectionSource$1.getConnection(FactoryMethodConnectionSource.java:107)
      at org.apache.logging.log4j.core.appender.db.jdbc.FactoryMethodConnectionSource.getConnection(FactoryMethodConnectionSource.java:53)
      at org.apache.logging.log4j.core.appender.db.jdbc.JdbcDatabaseManager.startupInternal(JdbcDatabaseManager.java:60)
      at org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager.startup(AbstractDatabaseManager.java:65)
      at org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender.start(AbstractDatabaseAppender.java:89)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:255)
      at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:530)
      at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:258)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:239)
      at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:158)
      at org.apache.logging.log4j.web.Log4jWebInitializerImpl.initializeNonJndi(Log4jWebInitializerImpl.java:168)
      at org.apache.logging.log4j.web.Log4jWebInitializerImpl.start(Log4jWebInitializerImpl.java:110)
      at org.apache.logging.log4j.web.Log4jServletContainerInitializer.onStartup(Log4jServletContainerInitializer.java:57)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5604)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.NullPointerException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.apache.logging.log4j.core.appender.db.jdbc.FactoryMethodConnectionSource$1.getConnection(FactoryMethodConnectionSource.java:105)
      ... 20 more
  </code></pre>
  
  <p>I am skeptical that whether the bean dataSource has not yet created when LOGJ2 calls getDatabaseConnection(). Please let me know what would be the reason and solution.</p>