Skip to content

Commit 0b78cc9

Browse files
mogronalolpivovarit
authored andcommitted
Custom aop (eugenp#1451)
1 parent 6c3f4d8 commit 0b78cc9

6 files changed

Lines changed: 135 additions & 0 deletions

File tree

‎spring-custom-aop/pom.xml‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-custom-aop</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<name>spring-custom-aop</name>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.5.2.RELEASE</version>
14+
</parent>
15+
16+
<dependencies>
17+
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter</artifactId>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-aop</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
</plugin>
42+
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<configuration>
47+
<source>1.8</source>
48+
<target>1.8</target>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
import org.aspectj.lang.annotation.Around;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.springframework.stereotype.Component;
7+
8+
@Aspect
9+
@Component
10+
public class ExampleAspect {
11+
12+
@Around("@annotation(LogExecutionTime)")
13+
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
14+
final long start = System.currentTimeMillis();
15+
16+
final Object proceed = joinPoint.proceed();
17+
18+
final long executionTime = System.currentTimeMillis() - start;
19+
20+
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
21+
22+
return proceed;
23+
}
24+
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface LogExecutionTime {
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.baeldung;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class Service {
7+
8+
@LogExecutionTime
9+
public void serve() throws InterruptedException {
10+
Thread.sleep(2000);
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
9+
@RunWith(SpringJUnit4ClassRunner.class)
10+
@SpringBootTest
11+
public class CustomAnnotationTest {
12+
13+
@Autowired
14+
private Service service;
15+
16+
@Test
17+
public void shouldApplyCustomAnnotation() throws InterruptedException {
18+
service.serve();
19+
}
20+
21+
}

0 commit comments

Comments
 (0)