Maven project with Spring, Log4j2 and properties
This a reference I use to quickly come up with a standalone app (no server) that uses Spring Boot, Log4j2 and a properties file.
The pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.testproject</groupId>
<artifactId>TestProject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>TestProject</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jodatime.version>2.5</jodatime.version>
<junit.version>4.12</junit.version>
<log4j.version>2.4.1</log4j.version>
<spring.boot.version>1.2.5.RELEASE</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
</project>
The configuration class that launches the app.
package org.testproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Configuration and runner for the test app.
*/
@Configuration
@ComponentScan(basePackages = { "org.testproject.*" })
@EnableAutoConfiguration
public final class TestApp {
/**
* Launch app.
*
* @param args
* not used
*/
public static void main(final String[] args) {
SpringApplication.run(Runner.class, args);
}
}
The runner - the class that does the actual work.
package org.testproject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* Read in a message file from Touch Copy and transform the contents to be diary
* ready.
*
* @author RobertMarkBram
*/
@Component
@PropertySources(value = {@PropertySource("classpath:/application.properties")})
public final class Runner implements CommandLineRunner {
/** Logger for this class. */
static final Logger LOG = LogManager.getLogger(Runner.class
.getName());
/** Use to access properties. */
@Autowired
private Environment env;
/* (non-Javadoc)
* @see org.springframework.boot.CommandLineRunner#run(java.lang.String[])
*/
@Override
public void run(final String... args) throws Exception {
String testproperty = env.getProperty("testProperty");
LOG.info(testproperty);
}
}
The application property file.
testProperty=Hello World
The log4j2 configuration - note that number 2 is there.. this not old log4j anymore.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<logger name="org.testproject" level="DEBUG" />
<Root level="ERROR">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
</Configuration>