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