BAEL-1240 An introduction to Spring AOP (#2733)
* BAL-36 File size api in java and apache commons IO * BAEL-282 grep in java - fixes after code review * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor * BAEL-519 Moved all supporting classes to main source * BAEL-519 Moved all supporting classes to main source * BAEL-519 Moved asserts and test classes in test folder. * BAEL-519 moved test related producer and consumer to src. * BAEL-586 Guide to Guava BiMap. * BAEL-587 formatted code. * BAEL-519 LMAX Disruptor * BAEL-587 resolved merge * BAEL-587 Resolved merge * BAEL-519 Removed disruptor link. * BAEL-519 Reverted Guava changes * RFQ-587 Added disruptor as a separate module. * BAEL-519 Disruptor changes. * BAEL-519 Removed disruptor from core-java module. * BAEL-729 Expose additional information programmatically in /info endpoint of actuator. * BAEL-1240 Introduction to Spring AOP * BAEL-1240 - Spring AOP using configuration in XML.
This commit is contained in:
parent
eece3d02ba
commit
4aaaf4cea5
|
@ -0,0 +1,13 @@
|
||||||
|
package org.baeldung.logger;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AdderAfterAspect {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
public void afterAdvice() throws Throwable {
|
||||||
|
logger.info("I'm done calling the method");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package org.baeldung.logger;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AdderAfterReturnAspect {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
public void afterReturn(final Object returnValue) throws Throwable {
|
||||||
|
logger.info("value return was {}", returnValue);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package org.baeldung.logger;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AdderAfterThrowAspect {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
public void afterThrow(final Exception exception) throws Throwable {
|
||||||
|
logger.info("Exception thrown was {}", exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package org.baeldung.logger;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AdderAroundAspect {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
public Object aroundAdvice(final ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
logger.info("Arguments passed to method are: " + Arrays.toString(joinPoint.getArgs()));
|
||||||
|
final Object result = joinPoint.proceed();
|
||||||
|
logger.info("Result from method is: " + result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package org.baeldung.logger;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AdderBeforeAspect {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
public void beforeAdvice() throws Throwable {
|
||||||
|
logger.info("I would be executed just before method starts");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package org.baeldung.logger;
|
||||||
|
|
||||||
|
public class SampleAdder {
|
||||||
|
|
||||||
|
public int add(int a, int b) {
|
||||||
|
if (a < 0 || b < 0) {
|
||||||
|
throw new IllegalArgumentException("Make sure all the arguments are greater than zero.");
|
||||||
|
}
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||||
|
xmlns:task="http://www.springframework.org/schema/task"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
|
||||||
|
|
||||||
|
<bean id="sampleAdder"
|
||||||
|
class="org.baeldung.logger.SampleAdder" />
|
||||||
|
|
||||||
|
<bean id="doBeforeAspect" class="org.baeldung.logger.AdderBeforeAspect" />
|
||||||
|
<bean id="doAfterAspect" class="org.baeldung.logger.AdderAfterAspect" />
|
||||||
|
<bean id="doAfterThrowingAspect" class="org.baeldung.logger.AdderAfterThrowAspect" />
|
||||||
|
<bean id="doAfterReturningAspect" class="org.baeldung.logger.AdderAfterReturnAspect" />
|
||||||
|
<bean id="doAroundAspect" class="org.baeldung.logger.AdderAroundAspect" />
|
||||||
|
|
||||||
|
<aop:config>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doBeforeAspect">
|
||||||
|
<aop:pointcut id="pointCutBefore"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:before method="beforeAdvice" pointcut-ref="pointCutBefore" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAfterAspect">
|
||||||
|
<aop:pointcut id="pointCutAfter"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:after method="afterAdvice" pointcut-ref="pointCutAfter" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAfterReturningAspect">
|
||||||
|
<aop:pointcut id="pointCutAfterReturning"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:after-returning method="afterReturn"
|
||||||
|
returning="returnValue" pointcut-ref="pointCutAfterReturning" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAfterThrowingAspect">
|
||||||
|
<aop:pointcut id="pointCutAfterThrowing"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:after-throwing method="afterThrow"
|
||||||
|
throwing="error" pointcut-ref="pointCutAfterThrowing" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAroundAspect">
|
||||||
|
<aop:pointcut id="pointCutAround"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:around method="aroundAdvice" pointcut-ref="pointCutAround" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
|
||||||
|
</aop:config>
|
||||||
|
|
||||||
|
</beans>
|
|
@ -0,0 +1,29 @@
|
||||||
|
package org.baeldung.logger;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(value = {"classpath:springAop-applicationContext.xml"})
|
||||||
|
public class CalculatorTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SampleAdder sampleAdder;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAddValidValues_returnsSucessfully() {
|
||||||
|
final int addedValue = sampleAdder.add(12, 12);
|
||||||
|
|
||||||
|
assertEquals(24, addedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test (expected = IllegalArgumentException.class)
|
||||||
|
public void whenAddInValidValues_throwsException() {
|
||||||
|
sampleAdder.add(12, -12);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||||
|
xmlns:task="http://www.springframework.org/schema/task"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
|
||||||
|
|
||||||
|
<bean id="sampleAdder"
|
||||||
|
class="org.baeldung.logger.SampleAdder" />
|
||||||
|
|
||||||
|
<bean id="doBeforeAspect" class="org.baeldung.logger.AdderBeforeAspect" />
|
||||||
|
<bean id="doAfterAspect" class="org.baeldung.logger.AdderAfterAspect" />
|
||||||
|
<bean id="doAfterThrowingAspect" class="org.baeldung.logger.AdderAfterThrowAspect" />
|
||||||
|
<bean id="doAfterReturningAspect" class="org.baeldung.logger.AdderAfterReturnAspect" />
|
||||||
|
<bean id="doAroundAspect" class="org.baeldung.logger.AdderAroundAspect" />
|
||||||
|
|
||||||
|
<aop:config>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doBeforeAspect">
|
||||||
|
<aop:pointcut id="pointCutBefore"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:before method="beforeAdvice" pointcut-ref="pointCutBefore" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAfterAspect">
|
||||||
|
<aop:pointcut id="pointCutAfter"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:after method="afterAdvice" pointcut-ref="pointCutAfter" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAfterReturningAspect">
|
||||||
|
<aop:pointcut id="pointCutAfterReturning"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:after-returning method="afterReturn"
|
||||||
|
returning="returnValue" pointcut-ref="pointCutAfterReturning" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAfterThrowingAspect">
|
||||||
|
<aop:pointcut id="pointCutAfterThrowing"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:after-throwing method="afterThrow"
|
||||||
|
throwing="exception" pointcut-ref="pointCutAfterThrowing" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
<aop:aspect id="aspects" ref="doAroundAspect">
|
||||||
|
<aop:pointcut id="pointCutAround"
|
||||||
|
expression="execution(* org.baeldung.logger.SampleAdder+.*(..))" />
|
||||||
|
<aop:around method="aroundAdvice" pointcut-ref="pointCutAround" />
|
||||||
|
</aop:aspect>
|
||||||
|
|
||||||
|
|
||||||
|
</aop:config>
|
||||||
|
|
||||||
|
</beans>
|
|
@ -0,0 +1,4 @@
|
||||||
|
/target/
|
||||||
|
.settings/
|
||||||
|
.classpath
|
||||||
|
.project
|
|
@ -0,0 +1,121 @@
|
||||||
|
<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/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-boot-actuator</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>spring-boot</name>
|
||||||
|
<description>This is simple boot application for Spring boot actuator test</description>
|
||||||
|
|
||||||
|
<!-- Inherit defaults from Spring Boot -->
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.2.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>spring-boot-actuator</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
</plugins>
|
||||||
|
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>integration</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<includes>
|
||||||
|
<include>**/*IntegrationTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<start-class>org.baeldung.MainApplication</start-class>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,13 @@
|
||||||
|
package org.baeldung;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.baeldung.config.MainConfig;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class MainApplication {
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
SpringApplication.run(MainConfig.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.baeldung.config;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import org.springframework.boot.actuate.info.InfoContributor;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
public class MainConfig {
|
||||||
|
|
||||||
|
public MainConfig() {}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public InfoContributor getInfoContributor() {
|
||||||
|
return (infoBuilder) -> infoBuilder.withDetail("applicationInfo", Collections.singletonMap("ActiveUserCount", "10"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
info.app.name=Sample application
|
|
@ -0,0 +1,32 @@
|
||||||
|
package org.baeldung.config;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MainConfig.class)
|
||||||
|
@TestPropertySource(properties = { "security.basic.enabled=false" })
|
||||||
|
public class ActuatorInfoIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetInfo_thenAdditionalInfoReturned() throws IOException {
|
||||||
|
final String expectedResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/expectedResponse.json")));
|
||||||
|
final ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/info", String.class);
|
||||||
|
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||||
|
assertEquals(expectedResponse, responseEntity.getBody());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{"app":{"name":"Sample application"},"applicationInfo":{"ActiveUserCount":"10"}}
|
Loading…
Reference in New Issue