Skip to content

Commit a1e03ad

Browse files
committed
1 parent 139a298 commit a1e03ad

File tree

10 files changed

+164
-18
lines changed

10 files changed

+164
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package com.jbd.saop.order;
22

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+
@EnableAspectJAutoProxy
9+
@ComponentScan("com.jbd.saop.order")
310
public class ApplicationConfig {
411
}

spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/ExecutionTime.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.jbd.saop.order.advice;
2+
3+
import org.aspectj.lang.annotation.Aspect;
4+
import org.aspectj.lang.annotation.Pointcut;
5+
6+
@Aspect
7+
public class CommonPointcut {
8+
9+
@Pointcut("execution(* com.jbd.saop.order.dao.*.*(..))")
10+
public void anyDaoMethod() {}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.jbd.saop.order.advice;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.After;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Before;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.core.annotation.Order;
9+
10+
@Aspect
11+
@Configuration
12+
@Order(1)
13+
public class SecondAdvice {
14+
15+
@Before("CommonPointcut.anyDaoMethod()")
16+
public void beforeAdvice(JoinPoint joinPoint) {
17+
System.out.println("\n======= Inside @Before() - 1 =======");
18+
System.out.println(joinPoint.getSignature().toShortString());
19+
}
20+
21+
@After("CommonPointcut.anyDaoMethod()")
22+
public void afterAdvice(JoinPoint joinPoint) {
23+
System.out.println("\n======= Inside @After() - 1 =======");
24+
System.out.println(joinPoint.getSignature().toShortString());
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.jbd.saop.order.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+
import org.springframework.core.annotation.Order;
8+
9+
@Configuration
10+
@Aspect
11+
@Order(2)
12+
public class ThirdAdvice {
13+
14+
@Around("CommonPointcut.anyDaoMethod()")
15+
public Object aroundAdvice(ProceedingJoinPoint pJoinPoint) throws Throwable {
16+
System.out.println("\n======= Inside @Around() - 2 =======");
17+
System.out.println(pJoinPoint.getSignature().toShortString());
18+
Object result = pJoinPoint.proceed();
19+
System.out.println("\n======= Inside @Around() - 2 =======");
20+
return result;
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.jbd.saop.order.advice;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.*;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.core.annotation.Order;
7+
8+
@Aspect
9+
@Configuration
10+
@Order(0)
11+
public class ZeroAdvice {
12+
13+
@Before("CommonPointcut.anyDaoMethod()")
14+
public void beforeAdvice(JoinPoint joinPoint) {
15+
System.out.println("\n======= Inside @Before() - 0 =======");
16+
System.out.println(joinPoint.getSignature().toShortString());
17+
}
18+
19+
@After("CommonPointcut.anyDaoMethod()")
20+
public void afterAdvice(JoinPoint joinPoint) {
21+
System.out.println("\n======= Inside @After() - 0 =======");
22+
System.out.println(joinPoint.getSignature().toShortString());
23+
}
24+
25+
@AfterReturning(
26+
pointcut = "CommonPointcut.anyDaoMethod()",
27+
returning = "result")
28+
public void afterReturningAdvice(JoinPoint joinPoint, Object result) {
29+
System.out.println("\n======= Inside @AfterReturning() - 0 =======");
30+
System.out.println(joinPoint.getSignature().toShortString());
31+
System.out.println("result: " + result);
32+
}
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jbd.saop.order.dao;
2+
3+
import org.springframework.scheduling.annotation.Async;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public class EmailRepository {
8+
9+
@Async
10+
public void sendRegistrationEmail(String email){
11+
System.out.println("Registration email will be sent to: " + email);
12+
}
13+
}

spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/dao/UserRepository.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.jbd.saop.order.dao;
22

3-
import com.jbd.saop.order.ExecutionTime;
43
import org.springframework.stereotype.Repository;
54

65
//A very stupid demo repository
76
@Repository
87
public class UserRepository {
98
//Add a user
10-
@ExecutionTime
11-
public UserRepository add(String username) throws InterruptedException {
12-
Thread.sleep(100);
9+
public UserRepository add(String username, String password) {
1310
if(username == null) {
1411
throw new RuntimeException("username is null", new NullPointerException());
1512
}
13+
if (password == null) {
14+
throw new RuntimeException("password is null", new NullPointerException());
15+
}
1616
System.out.println("New user added: " + username);
1717
return this;
1818
}
@@ -24,7 +24,6 @@ public UserRepository update(String username, String email) {
2424
}
2525

2626
//Delete an user
27-
@ExecutionTime
2827
public boolean delete(String username){
2928
if (username == null) {
3029
throw new RuntimeException("username is null", new NullPointerException());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.jbd.saop.order.service;
2+
3+
import com.jbd.saop.order.dao.EmailRepository;
4+
import com.jbd.saop.order.dao.UserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class UserService {
10+
@Autowired
11+
private UserRepository userRepository;
12+
13+
@Autowired
14+
private EmailRepository emailRepository;
15+
16+
public void registerUser(String username, String email, String password) {
17+
userRepository.add(username, password);
18+
userRepository.update(username, email);
19+
emailRepository.sendRegistrationEmail(email);
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.jbd.saop.order;
2+
3+
import com.jbd.saop.order.dao.UserRepository;
4+
import com.jbd.saop.order.service.UserService;
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 TestAdviceOrdering {
11+
12+
@Autowired
13+
private UserService userService;
14+
15+
@Autowired
16+
private UserRepository userRepository;
17+
18+
@Test
19+
public void testAddUser() {
20+
userRepository.add("abc", "abc@123");
21+
}
22+
23+
@Test
24+
public void testRegUser() {
25+
userService.registerUser("abc", "abc@jbd.com", "abc@123");
26+
}
27+
}

0 commit comments

Comments
 (0)