diff --git a/spring-aop/src/main/java/com/baeldung/method/info/Account.java b/spring-aop/src/main/java/com/baeldung/method/info/Account.java new file mode 100644 index 0000000000..1c946501fd --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/method/info/Account.java @@ -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 + + '}'; + } +} diff --git a/spring-aop/src/main/java/com/baeldung/method/info/AccountOperation.java b/spring-aop/src/main/java/com/baeldung/method/info/AccountOperation.java new file mode 100644 index 0000000000..db725a724f --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/method/info/AccountOperation.java @@ -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(); +} diff --git a/spring-aop/src/main/java/com/baeldung/method/info/BankAccountAspect.java b/spring-aop/src/main/java/com/baeldung/method/info/BankAccountAspect.java new file mode 100644 index 0000000000..6c8ef9d8d6 --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/method/info/BankAccountAspect.java @@ -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()); + + } +} + diff --git a/spring-aop/src/main/java/com/baeldung/method/info/BankAccountService.java b/spring-aop/src/main/java/com/baeldung/method/info/BankAccountService.java new file mode 100644 index 0000000000..4c7d0e3cac --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/method/info/BankAccountService.java @@ -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(); + } + + +} diff --git a/spring-aop/src/main/java/com/baeldung/method/info/WithdrawLimitException.java b/spring-aop/src/main/java/com/baeldung/method/info/WithdrawLimitException.java new file mode 100644 index 0000000000..85b0b4acfb --- /dev/null +++ b/spring-aop/src/main/java/com/baeldung/method/info/WithdrawLimitException.java @@ -0,0 +1,7 @@ +package com.baeldung.method.info; + +public class WithdrawLimitException extends RuntimeException { + public WithdrawLimitException(String message) { + super(message); + } +} diff --git a/spring-aop/src/test/java/com/baeldung/method/info/BankAccountServiceIntegrationTest.java b/spring-aop/src/test/java/com/baeldung/method/info/BankAccountServiceIntegrationTest.java new file mode 100644 index 0000000000..9e0ebfa903 --- /dev/null +++ b/spring-aop/src/test/java/com/baeldung/method/info/BankAccountServiceIntegrationTest.java @@ -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(); + } +}