BAEL-4071 - Get advised method info in Spring AOP
This commit is contained in:
parent
5ee88c852e
commit
73af581e18
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.method.info;
|
||||||
|
|
||||||
|
public class Account {
|
||||||
|
|
||||||
|
private String accountNumber;
|
||||||
|
private double balance;
|
||||||
|
|
||||||
|
public String getAccountNumber() {
|
||||||
|
return accountNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccountNumber(String accountNumber) {
|
||||||
|
this.accountNumber = accountNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getBalance() {
|
||||||
|
return balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBalance(double balance) {
|
||||||
|
this.balance = balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Account{" +
|
||||||
|
"accountNumber='" + accountNumber + '\'' +
|
||||||
|
", balance=" + balance +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.method.info;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface AccountOperation {
|
||||||
|
String operation();
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.method.info;
|
||||||
|
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Before;
|
||||||
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class BankAccountAspect {
|
||||||
|
|
||||||
|
@Before(value="@annotation(com.baeldung.method.info.AccountOperation)")
|
||||||
|
public void getAccountOperationInfo(JoinPoint joinPoint) {
|
||||||
|
|
||||||
|
//Method Information
|
||||||
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
|
|
||||||
|
System.out.println("full method description: " + signature.getMethod());
|
||||||
|
|
||||||
|
System.out.println("method name: " + signature.getMethod().getName());
|
||||||
|
|
||||||
|
System.out.println("declaring type: " + signature.getDeclaringType());
|
||||||
|
|
||||||
|
//Method args
|
||||||
|
System.out.println("Method args names:");
|
||||||
|
Arrays.stream(signature.getParameterNames()).forEach(s -> System.out.println("arg name: " + s));
|
||||||
|
|
||||||
|
System.out.println("Method args types:");
|
||||||
|
Arrays.stream(signature.getParameterTypes()).forEach(s -> System.out.println("arg type: " + s));
|
||||||
|
|
||||||
|
System.out.println("Method args values:");
|
||||||
|
Arrays.stream(joinPoint.getArgs()).forEach(o -> System.out.println("arg value: " + o.toString()));
|
||||||
|
|
||||||
|
//Additional Information
|
||||||
|
System.out.println("returning type: " + signature.getReturnType());
|
||||||
|
System.out.println("method modifier: " + Modifier.toString(signature.getModifiers()));
|
||||||
|
Arrays.stream(signature.getExceptionTypes())
|
||||||
|
.forEach(aClass -> System.out.println("exception type: " + aClass));
|
||||||
|
|
||||||
|
//Method annotation
|
||||||
|
Method method = signature.getMethod();
|
||||||
|
AccountOperation accountOperation = method.getAnnotation(AccountOperation.class);
|
||||||
|
System.out.println("Account operation annotation: " + accountOperation);
|
||||||
|
System.out.println("Account operation value: " + accountOperation.operation());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.method.info;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomUtils;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class BankAccountService {
|
||||||
|
|
||||||
|
@AccountOperation(operation = "deposit")
|
||||||
|
public void deposit(Account account, Double amount) {
|
||||||
|
account.setBalance(account.getBalance() + amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AccountOperation(operation = "withdraw")
|
||||||
|
public void withdraw(Account account, Double amount) throws WithdrawLimitException {
|
||||||
|
|
||||||
|
if(amount > 500.0) {
|
||||||
|
throw new WithdrawLimitException("Withdraw limit exceeded.");
|
||||||
|
}
|
||||||
|
|
||||||
|
account.setBalance(account.getBalance() - amount);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getBalance() {
|
||||||
|
return RandomUtils.nextDouble();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.method.info;
|
||||||
|
|
||||||
|
public class WithdrawLimitException extends RuntimeException {
|
||||||
|
public WithdrawLimitException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.baeldung.method.info;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class BankAccountServiceIntegrationTest {
|
||||||
|
|
||||||
|
private Account account;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
account = new Account();
|
||||||
|
account.setAccountNumber("12345");
|
||||||
|
account.setBalance(2000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
BankAccountService bankAccountService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void withdraw() {
|
||||||
|
bankAccountService.withdraw(account, 500.0);
|
||||||
|
assertTrue(account.getBalance() == 1500.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void withdrawWhenLimitReached() {
|
||||||
|
Assertions.assertThatExceptionOfType(WithdrawLimitException.class).isThrownBy(() -> bankAccountService.withdraw(account, 600.0));
|
||||||
|
assertTrue(account.getBalance() == 2000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deposit() {
|
||||||
|
bankAccountService.deposit(account, 500.0);
|
||||||
|
assertTrue(account.getBalance() == 2500.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getBalance() {
|
||||||
|
bankAccountService.getBalance();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue