Skip to content

Commit 5ae83cf

Browse files
committed
1 parent 9de2d80 commit 5ae83cf

File tree

11 files changed

+253
-7
lines changed

11 files changed

+253
-7
lines changed
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
package com.jbd.saop.after.aspect;
22

33
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.After;
45
import org.aspectj.lang.annotation.AfterThrowing;
56
import org.aspectj.lang.annotation.Aspect;
7+
import org.aspectj.lang.annotation.Pointcut;
68
import org.springframework.stereotype.Service;
79

810
@Service
911
@Aspect
1012
public class AllAfterAspects {
1113

14+
@Pointcut("execution(* com.jbd.saop.after.dao.*.*(..))")
15+
private void allDaoMethods(){}
16+
1217
@AfterThrowing(
13-
pointcut = "execution(* c.jbd.saop.at.dao.*.*(..))",
18+
pointcut = "allDaoMethods()",
1419
throwing = "exception")
1520
public void logDaoExceptions(JoinPoint joinPoint, RuntimeException exception) {
16-
System.out.println("Exception at: " + joinPoint.getSignature().toShortString());
21+
System.out.println("After Exception executed: "
22+
+ joinPoint.getSignature().toShortString());
1723
System.out.println("Exception details: " + exception);
1824
}
25+
26+
@After("allDaoMethods()")
27+
public void logResults(JoinPoint joinPoint){
28+
System.out.println("\nAfter advice executed: "
29+
+ joinPoint.getSignature().toShortString());
30+
}
1931
}

spring-aop-05-@After/src/test/java/com/jbd/saop/after/TestAfterAdvice.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ public void notNull() {
1818
}
1919

2020
@Test
21-
public void testThrows() {
21+
public void testAfterThrows() {
2222
//Assert exception
2323
Assertions.assertThrows(RuntimeException.class, () -> {
2424
userRepository.add(null);
25-
userRepository.delete(null);
2625
});
26+
}
2727

28-
//Assert exception
28+
@Test
29+
public void testAfter() {
30+
//Assert not exception
2931
Assertions.assertDoesNotThrow(() -> {
3032
userRepository.delete("alexa");
3133
});
3234
}
33-
3435
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.jbd.saop.around;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
6+
7+
@Configuration
8+
@ComponentScan("com.jbd.saop.around")
9+
@EnableAspectJAutoProxy
10+
public class ApplicationConfig {
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jbd.saop.around;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* Tracks the execution time.
7+
*/
8+
//Allowed to use only on methods
9+
@Target({ElementType.METHOD})
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Documented
12+
public @interface ExecutionTime {
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.jbd.saop.around.advice;
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.context.annotation.Configuration;
7+
8+
import java.time.LocalTime;
9+
10+
@Configuration
11+
@Aspect
12+
public class ExecutionTimeLogger {
13+
14+
@Around("@annotation(com.jbd.saop.around.ExecutionTime)")
15+
public Object logExecTime(ProceedingJoinPoint pJoinPoint){
16+
System.out.println("Before method: "
17+
+ pJoinPoint.getSignature().toShortString());
18+
long beforeTime = System.currentTimeMillis();
19+
Object result = null;
20+
try {
21+
result = pJoinPoint.proceed();//Important
22+
//If method throws Exception or any error occurs
23+
} catch (Throwable throwable) {
24+
throwable.printStackTrace();
25+
}
26+
long afterTime = System.currentTimeMillis();
27+
System.out.println("Time taken to execute: "
28+
+ (afterTime - beforeTime) + "ms");
29+
return result;
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jbd.saop.around.dao;
2+
3+
import com.jbd.saop.around.ExecutionTime;
4+
import org.springframework.stereotype.Repository;
5+
6+
//A very stupid demo repository
7+
@Repository
8+
public class UserRepository {
9+
//Add a user
10+
@ExecutionTime
11+
public UserRepository add(String username) throws InterruptedException {
12+
Thread.sleep(100);
13+
if(username == null) {
14+
throw new RuntimeException("username is null", new NullPointerException());
15+
}
16+
System.out.println("New user added: " + username);
17+
return this;
18+
}
19+
20+
//Update an user
21+
public UserRepository update(String username, String email) {
22+
System.out.println("Update email: " + email);
23+
return this;
24+
}
25+
26+
//Delete an user
27+
@ExecutionTime
28+
public boolean delete(String username){
29+
if (username == null) {
30+
throw new RuntimeException("username is null", new NullPointerException());
31+
}
32+
System.out.println("User deleted: " + username);
33+
return true;
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.jbd.saop.around;
2+
3+
import com.jbd.saop.around.dao.UserRepository;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
8+
9+
@SpringJUnitConfig(ApplicationConfig.class)
10+
public class TestAroundAdvice {
11+
@Autowired
12+
private UserRepository userRepository;
13+
14+
@Test
15+
public void testNull(){
16+
Assertions.assertNotNull(userRepository);
17+
}
18+
19+
@Test
20+
public void testAddUser() throws InterruptedException {
21+
userRepository.add("sample_username");
22+
}
23+
24+
@Test
25+
public void testAddUserFailure() throws InterruptedException {
26+
userRepository.add(null);
27+
}
28+
}

spring-aop-07-ordering-advice/pom.xml

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,71 @@
88
<version>1.0-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
11-
1211
<artifactId>spring-aop-07-ordering-advice</artifactId>
12+
<description>Ordering advices using @Order annotation</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-core</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-context</artifactId>
23+
</dependency>
24+
25+
<!-- Step-1 Add the dependencies -->
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-aop</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.aspectj</groupId>
33+
<artifactId>aspectjweaver</artifactId>
34+
</dependency>
35+
36+
<!-- Test Dependencies-->
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-api</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter-engine</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-test</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
1355

56+
<dependencyManagement>
57+
<dependencies>
58+
<dependency>
59+
<groupId>com.jstobigdata</groupId>
60+
<artifactId>spring-tutorial-boms</artifactId>
61+
<type>pom</type>
62+
<scope>import</scope>
63+
<version>1.0-SNAPSHOT</version>
64+
</dependency>
65+
</dependencies>
66+
</dependencyManagement>
1467

68+
<!-- For Junit-jupiter-->
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
<version>3.0.0-M3</version>
75+
</plugin>
76+
</plugins>
77+
</build>
1578
</project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.jbd.saop.order;
2+
3+
public class ApplicationConfig {
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jbd.saop.order;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* Tracks the execution time.
7+
*/
8+
//Allowed to use only on methods
9+
@Target({ElementType.METHOD})
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Documented
12+
public @interface ExecutionTime {
13+
}

0 commit comments

Comments
 (0)