injection point example (#1850)

This commit is contained in:
lor6 2017-05-15 14:46:35 +03:00 committed by Grzegorz Piwowarek
parent ce2f24d048
commit 0c973f492e
4 changed files with 41 additions and 1 deletions

View File

@ -164,6 +164,18 @@
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
@ -264,6 +276,8 @@
<assertj.version>3.6.1</assertj.version>
<jasperreports.version>6.4.0</jasperreports.version>
<log4j.version>2.8.2</log4j.version>
</properties>
</project>

View File

@ -6,12 +6,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.logging.log4j.Logger;
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
private final AppointmentService appointmentService;
private AppointmentService appointmentService;
@Autowired
private Logger logger;
@Autowired
public AppointmentsController(AppointmentService appointmentService) {
@ -20,6 +24,7 @@ public class AppointmentsController {
@GetMapping
public Map<String, Appointment> get() {
logger.info("Getting appointments...");
return appointmentService.getAppointmentsForToday();
}

View File

@ -0,0 +1,11 @@
rootLogger.level=info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
appenders=console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %p %d{yyyy-MM-dd} [%t] %c{1} - %msg%n

View File

@ -9,6 +9,10 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.beans.factory.InjectionPoint;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Scope;
import static org.easymock.EasyMock.*;
@ -33,4 +37,10 @@ public class ComposedMappingConfiguration {
return book;
}
@Bean
@Scope("prototype")
public Logger logger(InjectionPoint injectionPoint) {
return LogManager.getLogger(injectionPoint.getField().getDeclaringClass());
}
}