Skip to content

Commit 0c973f4

Browse files
lor6pivovarit
authored andcommitted
injection point example (eugenp#1850)
1 parent ce2f24d commit 0c973f4

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎spring-all/pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@
164164
<artifactId>ehcache</artifactId>
165165
<version>${ehcache.version}</version>
166166
</dependency>
167+
168+
<dependency>
169+
<groupId>org.apache.logging.log4j</groupId>
170+
<artifactId>log4j-api</artifactId>
171+
<version>${log4j.version}</version>
172+
</dependency>
173+
<dependency>
174+
<groupId>org.apache.logging.log4j</groupId>
175+
<artifactId>log4j-core</artifactId>
176+
<version>${log4j.version}</version>
177+
</dependency>
178+
167179
</dependencies>
168180

169181
<dependencyManagement>
@@ -264,6 +276,8 @@
264276
<assertj.version>3.6.1</assertj.version>
265277
<jasperreports.version>6.4.0</jasperreports.version>
266278

279+
<log4j.version>2.8.2</log4j.version>
280+
267281
</properties>
268282

269283
</project>

‎spring-all/src/main/java/org/baeldung/spring43/composedmapping/AppointmentsController.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import org.springframework.stereotype.Controller;
77
import org.springframework.web.bind.annotation.GetMapping;
88
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.apache.logging.log4j.Logger;
910

1011
@Controller
1112
@RequestMapping("/appointments")
1213
public class AppointmentsController {
1314

14-
private final AppointmentService appointmentService;
15+
private AppointmentService appointmentService;
16+
17+
@Autowired
18+
private Logger logger;
1519

1620
@Autowired
1721
public AppointmentsController(AppointmentService appointmentService) {
@@ -20,6 +24,7 @@ public AppointmentsController(AppointmentService appointmentService) {
2024

2125
@GetMapping
2226
public Map<String, Appointment> get() {
27+
logger.info("Getting appointments...");
2328
return appointmentService.getAppointmentsForToday();
2429
}
2530

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
rootLogger.level=info
2+
rootLogger.appenderRefs = stdout
3+
rootLogger.appenderRef.stdout.ref = STDOUT
4+
5+
appenders=console
6+
7+
appender.console.type = Console
8+
appender.console.name = STDOUT
9+
appender.console.layout.type = PatternLayout
10+
appender.console.layout.pattern = %p %d{yyyy-MM-dd} [%t] %c{1} - %msg%n
11+

‎spring-all/src/test/java/org/baeldung/spring43/composedmapping/ComposedMappingConfiguration.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import org.springframework.web.servlet.ViewResolver;
1010
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1111
import org.springframework.web.servlet.view.InternalResourceViewResolver;
12+
import org.springframework.beans.factory.InjectionPoint;
13+
import org.apache.logging.log4j.LogManager;
14+
import org.apache.logging.log4j.Logger;
15+
import org.springframework.context.annotation.Scope;
1216

1317
import static org.easymock.EasyMock.*;
1418

@@ -33,4 +37,10 @@ public AppointmentService appointmentBook() {
3337
return book;
3438
}
3539

40+
@Bean
41+
@Scope("prototype")
42+
public Logger logger(InjectionPoint injectionPoint) {
43+
return LogManager.getLogger(injectionPoint.getField().getDeclaringClass());
44+
}
45+
3646
}

0 commit comments

Comments
 (0)