Skip to content

Commit 0dd102d

Browse files
committed
Add 3 interfaces and aspects.
1 parent 2cc4cc3 commit 0dd102d

12 files changed

Lines changed: 166 additions & 3 deletions

File tree

spring-aop/pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<name>spring-aop</name>
1515
<description>spring-aop</description>
1616
<properties>
17-
<java.version>17</java.version>
17+
<java.version>11</java.version>
1818
</properties>
1919
<dependencies>
2020
<dependency>
@@ -25,6 +25,14 @@
2525
<groupId>org.springframework.boot</groupId>
2626
<artifactId>spring-boot-starter-aop</artifactId>
2727
</dependency>
28+
<!-- <dependency>-->
29+
<!-- <groupId>org.springframework</groupId>-->
30+
<!-- <artifactId>spring-aop</artifactId>-->
31+
<!-- </dependency>-->
32+
<!-- <dependency>-->
33+
<!-- <groupId>org.springframework</groupId>-->
34+
<!-- <artifactId>spring-aspects</artifactId>-->
35+
<!-- </dependency>-->
2836

2937
<dependency>
3038
<groupId>org.springframework.boot</groupId>

spring-aop/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# Spring AOP
22

33
https://reflectoring.io/aop-spring/
4+
5+
https://github.com/eugenp/tutorials/tree/master/spring-aop
6+
7+
https://www.baeldung.com/spring-aop
8+
9+
https://www.baeldung.com/spring-aop-vs-aspectj
10+
11+
https://jstobigdata.com/spring/pointcut-expressions-in-spring-aop/
12+

spring-aop/src/main/java/com/github/emeraldjava/springaop/SpringAopApplication.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.github.emeraldjava.springaop;
22

33
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
7+
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
8+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
59

6-
@SpringBootApplication
10+
@SpringBootApplication(exclude = {
11+
DataSourceAutoConfiguration.class,
12+
DataSourceTransactionManagerAutoConfiguration.class,
13+
HibernateJpaAutoConfiguration.class})
714
public class SpringAopApplication {
815

916
public static void main(String[] args) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.emeraldjava.springaop.aspect;
2+
3+
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Before;
7+
import org.aspectj.lang.annotation.Pointcut;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
@Aspect
12+
public class LoggingAspect {
13+
14+
@Pointcut("execution(* com.github.emeraldjava.springaop.service.IBillNoArgs.createBill())")
15+
public void logPointcutNoArgs() {}
16+
17+
@Before("logPointcutNoArgs()")
18+
public void logMethodCallsWithArgsAdvice() {
19+
System.out.println("In Aspect from logPointcutNoArgs");
20+
}
21+
}
22+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.emeraldjava.springaop.aspect;
2+
3+
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Before;
7+
import org.aspectj.lang.annotation.Pointcut;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
@Aspect
12+
public class LoggingAspectWithArg {
13+
14+
@Pointcut("execution(* com.github.emeraldjava.springaop.service.IBillArgs.createBill(*))")
15+
public void logPointcutArgs() {}
16+
17+
/**
18+
*
19+
* https://reflectoring.io/aop-spring/
20+
* @param joinPoint
21+
*/
22+
@Before("logPointcutArgs()")
23+
public void logMethodCallsWithArgsAdvice(JoinPoint joinPoint) {
24+
System.out.println("In Aspect from logPointcutArgs "+joinPoint.getSignature().getName());
25+
System.out.println("arg "+joinPoint.getArgs()[0]);
26+
}
27+
}
28+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.emeraldjava.springaop.aspect;
2+
3+
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Before;
7+
import org.aspectj.lang.annotation.Pointcut;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
@Aspect
12+
public class LoggingAspectWithArgException {
13+
14+
@Pointcut("execution(* com.github.emeraldjava.springaop.service.IBillArgsException.createBillEx(*))")
15+
public void logPointcutArgsException() {}
16+
17+
@Before("logPointcutArgsException()")
18+
public void logMethodCallsWithArgsAdvice(JoinPoint joinPoint) {
19+
System.out.println("In Aspect from logPointcutArgsException "+joinPoint.getSignature().getName());
20+
System.out.println("arg "+joinPoint.getArgs()[0]);
21+
}
22+
}
23+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.emeraldjava.springaop.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
5+
6+
/**
7+
*
8+
* See https://docs.spring.io/spring-framework/docs/4.3.15.RELEASE/spring-framework-reference/html/aop.html
9+
*/
10+
@Configuration
11+
@EnableAspectJAutoProxy
12+
public class AspectConfig {
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.emeraldjava.springaop.service;
2+
3+
public interface IBillArgs {
4+
5+
void createBill(Long id);
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.emeraldjava.springaop.service;
2+
3+
public interface IBillArgsException {
4+
5+
void createBillEx(Long id) throws Exception;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.emeraldjava.springaop.service;
2+
3+
public interface IBillNoArgs {
4+
5+
void createBill();
6+
}

0 commit comments

Comments
 (0)