Skip to content

Commit 1db0387

Browse files
committed
Added Spring-Aop Tutorials example
1 parent 8961630 commit 1db0387

File tree

41 files changed

+1336
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1336
-4
lines changed

pom.xml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@
1717
<module>spring-core-beanpostprocessor</module>
1818
<module>spring-core-environment-abstraction</module>
1919
<module>spring-core-events</module>
20+
<module>spring-mvc-00-getting-started</module>
21+
<module>spring-aop-01-@Before</module>
22+
<module>spring-aop-02-combine-joinpoints</module>
23+
<module>spring-aop-03-@AfterReturning</module>
24+
<module>spring-aop-04-@AfterThrowing</module>
25+
<module>spring-aop-05-@After</module>
26+
<module>spring-aop-06-@Around</module>
27+
<module>spring-aop-07-ordering-advice</module>
28+
<module>spring-aop-08-advice-parameters</module>
29+
<module>spring-aop-09-custom-annotation</module>
30+
<module>spring-aop-10-inter-type-declarations</module>
2031
</modules>
2132

2233
<properties>
2334
<!-- JDK Version -->
24-
<maven.compiler.target>1.8</maven.compiler.target>
25-
<maven.compiler.source>1.8</maven.compiler.source>
26-
<java.version>1.8</java.version>
35+
<maven.compiler.target>11</maven.compiler.target>
36+
<maven.compiler.source>11</maven.compiler.source>
37+
<java.version>11</java.version>
2738
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2839
</properties>
2940

30-
3141
</project>

spring-aop-01-@Before/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-framework-tutorial-parent</artifactId>
7+
<groupId>com.jstobigdata</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>spring-aop-01-@Before</artifactId>
12+
<description>Spring AOP - @Before advice example</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-core</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-context</artifactId>
23+
</dependency>
24+
25+
<!-- Step-1 Add the dependencies -->
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-aop</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.aspectj</groupId>
33+
<artifactId>aspectjweaver</artifactId>
34+
</dependency>
35+
36+
<!-- Test Dependencies-->
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-api</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter-engine</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-test</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<dependencyManagement>
57+
<dependencies>
58+
<dependency>
59+
<groupId>com.jstobigdata</groupId>
60+
<artifactId>spring-tutorial-boms</artifactId>
61+
<type>pom</type>
62+
<scope>import</scope>
63+
<version>1.0-SNAPSHOT</version>
64+
</dependency>
65+
</dependencies>
66+
</dependencyManagement>
67+
68+
<!-- To get rid of JUnit-5 exception https://stackoverflow.com/questions/36427868/ -->
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
<version>3.0.0-M3</version>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package c.jbd.saop.gettingstarted.aspect;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.Aspect;
5+
import org.aspectj.lang.annotation.Before;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
@Aspect //Important to say this is a Aspect config class
10+
public class BeforeAdvice {
11+
12+
//execution(RETURN_TYPE PACKAGE.CLASS.METHOD(..PARAMETER_LIST))
13+
//execution(* PACKAGE.*.*(..))
14+
15+
/**
16+
* Before Interceptor all Repository add() method.
17+
*
18+
* @param joinPoint
19+
*/
20+
@Before("execution(* c.jbd.saop.gettingstarted.dao.*.add(..))")
21+
public void allRepoAddMethods(JoinPoint joinPoint) {
22+
System.out.println("Intercepted method: " + joinPoint);
23+
System.out.println("Arguments: " + joinPoint.getArgs()[0]);
24+
System.out.println(joinPoint.getTarget());
25+
}
26+
27+
@Before("execution(* c.jbd.saop.gettingstarted.dao.*.*(..))")
28+
public void allClassAnyMethod(JoinPoint joinPoint) {
29+
System.out.println("Intercepted method: " + joinPoint);
30+
System.out.println("Arguments: " + joinPoint.getArgs()[0]);
31+
System.out.println(joinPoint.getTarget());
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package c.jbd.saop.gettingstarted.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
6+
7+
/**
8+
* Getting started AppConfig.
9+
*/
10+
@Configuration
11+
//Step-2 - Add the annotation
12+
@EnableAspectJAutoProxy
13+
@ComponentScan("c.jbd.saop.gettingstarted")
14+
public class ApplicationConfig {
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package c.jbd.saop.gettingstarted.dao;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
@Repository
6+
public class ActorRepository {
7+
//Add an actor
8+
public ActorRepository add(String actorName) {
9+
if (actorName == null) {
10+
throw new RuntimeException("actorName is null", new NullPointerException());
11+
}
12+
System.out.println("New Actor added: " + actorName);
13+
return this;
14+
}
15+
16+
//Delete an actor
17+
public boolean delete(String actorName) {
18+
if (actorName == null) {
19+
throw new RuntimeException("actorName is null", new NullPointerException());
20+
}
21+
System.out.println("Actor deleted: " + actorName);
22+
return true;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package c.jbd.saop.gettingstarted.dao;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
@Repository
6+
public class MovieRepository {
7+
//Add a movie method
8+
public MovieRepository add(String movieName) {
9+
if (movieName == null) {
10+
throw new RuntimeException("movieName is null", new NullPointerException());
11+
}
12+
System.out.println("New movie added: " + movieName);
13+
return this;
14+
}
15+
16+
//Delete a movie
17+
public boolean delete(String movieName) {
18+
if (movieName == null) {
19+
throw new RuntimeException("movieName is null", new NullPointerException());
20+
}
21+
System.out.println("Movie deleted: " + movieName);
22+
return true;
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package c.jbd.saop.gettingstarted.service;
2+
3+
import c.jbd.saop.gettingstarted.dao.ActorRepository;
4+
import c.jbd.saop.gettingstarted.dao.MovieRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class MovieService {
10+
@Autowired
11+
private ActorRepository actorRepository;
12+
13+
@Autowired
14+
private MovieRepository movieRepository;
15+
16+
public void addMovie(String movieName, String... actors) {
17+
movieRepository.add(movieName);
18+
for (String actor : actors) {
19+
actorRepository.add(movieName);
20+
}
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package c.jbd.saop.gettingstarted;
2+
3+
import c.jbd.saop.gettingstarted.config.ApplicationConfig;
4+
import c.jbd.saop.gettingstarted.dao.ActorRepository;
5+
import c.jbd.saop.gettingstarted.dao.MovieRepository;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
10+
11+
@SpringJUnitConfig(ApplicationConfig.class)
12+
public class TestBeforeAdvice {
13+
14+
@Autowired
15+
private ActorRepository actorRepository;
16+
17+
@Autowired
18+
private MovieRepository movieRepository;
19+
20+
@Test
21+
public void notNull() {
22+
Assertions.assertNotNull(actorRepository);
23+
Assertions.assertNotNull(movieRepository);
24+
}
25+
26+
@Test
27+
public void testAddAspects() {
28+
actorRepository.add("Hrithik Roshan");
29+
movieRepository.add("Sholey");
30+
}
31+
32+
@Test
33+
public void testDeleteAspects() {
34+
actorRepository.delete("John Doe");
35+
movieRepository.delete("Abc");
36+
}
37+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-framework-tutorial-parent</artifactId>
7+
<groupId>com.jstobigdata</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>spring-aop-02-combine-joinpoints</artifactId>
12+
<description>This tutorial is about combining Pointcuts and reusing them in Spring-AOP</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-core</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-context</artifactId>
23+
</dependency>
24+
25+
<!-- Step-1 Add the dependencies -->
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-aop</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.aspectj</groupId>
33+
<artifactId>aspectjweaver</artifactId>
34+
</dependency>
35+
36+
<!-- Test Dependencies-->
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-api</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter-engine</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-test</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<dependencyManagement>
57+
<dependencies>
58+
<dependency>
59+
<groupId>com.jstobigdata</groupId>
60+
<artifactId>spring-tutorial-boms</artifactId>
61+
<type>pom</type>
62+
<scope>import</scope>
63+
<version>1.0-SNAPSHOT</version>
64+
</dependency>
65+
</dependencies>
66+
</dependencyManagement>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-surefire-plugin</artifactId>
73+
<version>3.0.0-M3</version>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package c.jbd.saop.cjp;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
6+
7+
@Configuration
8+
@EnableAspectJAutoProxy
9+
@ComponentScan("c.jbd.saop.cjp")
10+
public class ApplicationConfig {
11+
}

0 commit comments

Comments
 (0)