Skip to content

Commit a5118db

Browse files
AOP - Pointcut Expressions - Match Method Parameter Types
1 parent 5632aa1 commit a5118db

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.luv2code.aopdemo;
2+
3+
public class Account {
4+
5+
private String name;
6+
private String level;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
public String getLevel() {
15+
return level;
16+
}
17+
public void setLevel(String level) {
18+
this.level = level;
19+
}
20+
21+
}

Diff for: spring-demo-aop/src/com/luv2code/aopdemo/MainDemoApp.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@ public static void main(String[] args) {
2121
// call the business method
2222
theAccountDAO.addAccount();
2323

24+
// call the business method with one param
25+
Account myAccount = new Account();
26+
theAccountDAO.addAccount(myAccount);
27+
28+
// call the business method with two param
29+
theAccountDAO.addAccount(myAccount, true);
30+
theAccountDAO.doWork();
31+
2432
// call the membership business method
2533
theMembershipDAO.addSillyMember();
2634

2735
// call the membership business method
2836
theMembershipDAO.addSillyMember1();
37+
theMembershipDAO.goToSleep();
2938

3039
// call the business method again
31-
System.out.println("\n Let's call it again!\n");
32-
theAccountDAO.addAccount();
40+
theAccountDAO.addAccount();
3341

3442
// close the context
3543
context.close();

Diff for: spring-demo-aop/src/com/luv2code/aopdemo/aspect/MyDemoLoggingAspect.java

+43-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class MyDemoLoggingAspect {
3434
// match method starting with "add" in any class
3535

3636
/*
37-
* @Before("execution(public void add*t())" ) public void
37+
* @Before("execution(public void add*())" ) public void
3838
* beforeAddAccountAdvice() {
3939
*
4040
* System.out.println("\n======>>> Executing @Before advice on addAccount()"); }
@@ -43,14 +43,54 @@ public class MyDemoLoggingAspect {
4343
// match method with based on Return Type
4444

4545
/*
46-
* @Before("execution(void add*t())" ) public void beforeAddAccountAdvice() {
46+
* @Before("execution(void add*())" ) public void beforeAddAccountAdvice() {
4747
*
4848
* System.out.println("\n======>>> Executing @Before advice on addAccount()"); }
4949
*/
5050

5151
// match method with ANY Return Type - use wildcards
5252

53-
@Before("execution(* add*())")
53+
/*
54+
* @Before("execution(* add*())") public void beforeAddAccountAdvice() {
55+
*
56+
* System.out.println("\n======>>> Executing @Before advice on addAccount()"); }
57+
*/
58+
59+
// Match method with "Account" Parameter Type (for the add* method)
60+
61+
/*
62+
* @Before("execution(* add*(com.luv2code.aopdemo.Account))") public void
63+
* beforeAddAccountAdvice() {
64+
*
65+
* System.out.println("\n======>>> Executing @Before advice on addAccount()"); }
66+
*/
67+
68+
// Match method with "Account" Param and more Param types
69+
// (..) => Match on any number of arguments
70+
71+
/*
72+
* @Before("execution(* add*(com.luv2code.aopdemo.Account, ..))") public void
73+
* beforeAddAccountAdvice() {
74+
*
75+
* System.out.println("\n======>>> Executing @Before advice on addAccount()"); }
76+
*/
77+
78+
// Match method with ANY parameters
79+
80+
/*
81+
* @Before("execution(* add*(..))") public void beforeAddAccountAdvice() {
82+
*
83+
* System.out.println("\n======>>> Executing @Before advice on addAccount()"); }
84+
*/
85+
86+
// Match methods in a given Package
87+
88+
// 1. * return type
89+
// 2. com.luv2code.aopdemo.dao -> the package
90+
// 3. .*.* -> the class and the method
91+
// 4. (..) -> params
92+
93+
@Before("execution(* com.luv2code.aopdemo.dao.*.*(..))")
5494
public void beforeAddAccountAdvice() {
5595

5696
System.out.println("\n======>>> Executing @Before advice on addAccount()");

Diff for: spring-demo-aop/src/com/luv2code/aopdemo/dao/AccountDAO.java

+16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22

33
import org.springframework.stereotype.Component;
44

5+
import com.luv2code.aopdemo.Account;
6+
57
@Component
68
public class AccountDAO {
79

810
public void addAccount() {
911
System.out.println(getClass() + ": Doing my DB work: Adding an account");
1012
}
13+
14+
public void addAccount(Account theAccount) {
15+
System.out.println(getClass() + ": Doing my DB work: Adding an account");
16+
}
17+
18+
public void addAccount(Account theAccount, boolean vipFlag) {
19+
System.out.println(getClass() + ": Doing my DB work: Adding an account");
20+
}
21+
22+
public boolean doWork() {
23+
24+
System.out.println(getClass() + ": doWork()");
25+
return false;
26+
}
1127
}

Diff for: spring-demo-aop/src/com/luv2code/aopdemo/dao/MembershipDAO.java

+5
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public boolean addSillyMember1() {
1818

1919
return true;
2020
}
21+
22+
public void goToSleep() {
23+
24+
System.out.println(getClass() + ": I am going to sleep now");
25+
}
2126
}

0 commit comments

Comments
 (0)