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 {
|
||||
|
||||
private String accountNumber;
|
||||
private double balance;
|
||||
private String accountNumber;
|
||||
private double balance;
|
||||
|
||||
public String getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
public String getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
|
||||
public void setAccountNumber(String accountNumber) {
|
||||
this.accountNumber = accountNumber;
|
||||
}
|
||||
public void setAccountNumber(String accountNumber) {
|
||||
this.accountNumber = accountNumber;
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public void setBalance(double balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
public void setBalance(double balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Account{" +
|
||||
"accountNumber='" + accountNumber + '\'' +
|
||||
", balance=" + balance +
|
||||
'}';
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Account{" + "accountNumber='" + accountNumber + '\'' + ", balance=" + balance + '}';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,5 +8,5 @@ import java.lang.annotation.Target;
|
|||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AccountOperation {
|
||||
String operation();
|
||||
String operation();
|
||||
}
|
||||
|
|
|
@ -14,40 +14,42 @@ import java.util.Arrays;
|
|||
@Component
|
||||
public class BankAccountAspect {
|
||||
|
||||
@Before(value="@annotation(com.baeldung.method.info.AccountOperation)")
|
||||
public void getAccountOperationInfo(JoinPoint joinPoint) {
|
||||
@Before(value = "@annotation(com.baeldung.method.info.AccountOperation)")
|
||||
public void getAccountOperationInfo(JoinPoint joinPoint) {
|
||||
|
||||
//Method Information
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
// Method Information
|
||||
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
|
||||
System.out.println("Method args names:");
|
||||
Arrays.stream(signature.getParameterNames()).forEach(s -> System.out.println("arg name: " + s));
|
||||
// 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 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()));
|
||||
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));
|
||||
// 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());
|
||||
// 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());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,25 +6,24 @@ 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.");
|
||||
@AccountOperation(operation = "deposit")
|
||||
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.");
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return RandomUtils.nextDouble();
|
||||
}
|
||||
account.setBalance(account.getBalance() - amount);
|
||||
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return RandomUtils.nextDouble();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.method.info;
|
||||
|
||||
public class WithdrawLimitException extends RuntimeException {
|
||||
public WithdrawLimitException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public class WithdrawLimitException extends RuntimeException {
|
||||
public WithdrawLimitException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,38 +11,39 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||
@SpringBootTest
|
||||
class BankAccountServiceIntegrationTest {
|
||||
|
||||
private Account account;
|
||||
private Account account;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
account = new Account();
|
||||
account.setAccountNumber("12345");
|
||||
account.setBalance(2000.0);
|
||||
}
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
account = new Account();
|
||||
account.setAccountNumber("12345");
|
||||
account.setBalance(2000.0);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
BankAccountService bankAccountService;
|
||||
@Autowired
|
||||
BankAccountService bankAccountService;
|
||||
|
||||
@Test
|
||||
void withdraw() {
|
||||
bankAccountService.withdraw(account, 500.0);
|
||||
assertTrue(account.getBalance() == 1500.0);
|
||||
}
|
||||
@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 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 deposit() {
|
||||
bankAccountService.deposit(account, 500.0);
|
||||
assertTrue(account.getBalance() == 2500.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBalance() {
|
||||
bankAccountService.getBalance();
|
||||
}
|
||||
@Test
|
||||
void getBalance() {
|
||||
bankAccountService.getBalance();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue