Skip to content

Commit 7f92b8a

Browse files
authored
Merge pull request #17726 from oscarramadhan/BAEL-8421
BAEL-8421
2 parents 1a6c16d + 950f627 commit 7f92b8a

File tree

9 files changed

+220
-0
lines changed

9 files changed

+220
-0
lines changed

aspectj/.gitignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
mvnw
7+
.mvn/wrapper/maven-wrapper.properties
8+
9+
### STS ###
10+
.apt_generated
11+
.classpath
12+
.factorypath
13+
.project
14+
.settings
15+
.springBeans
16+
.sts4-cache
17+
18+
### IntelliJ IDEA ###
19+
.idea
20+
*.iws
21+
*.iml
22+
*.ipr
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/

aspectj/pom.xml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>parent-boot-3</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<relativePath>../parent-boot-3</relativePath>
10+
</parent>
11+
<artifactId>aspectj</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<name>aspectj</name>
14+
<description>Demo project for Spring Boot to Pointcut all methods in a certain package</description>
15+
<properties>
16+
<java.version>17</java.version>
17+
</properties>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.aspectj</groupId>
26+
<artifactId>aspectjrt</artifactId>
27+
<version>1.9.22.1</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.aspectj</groupId>
31+
<artifactId>aspectjweaver</artifactId>
32+
<version>1.9.22.1</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.13.0</version>
52+
<configuration>
53+
<source>${java.version}</source>
54+
<target>${java.version}</target>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.aspectj;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class AspectjApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(AspectjApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.aspectj.config;
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.stereotype.Component;
7+
8+
@Aspect
9+
@Component
10+
public class LoggingAspect {
11+
@Before("execution(* com.baeldung.aspectj..*(..))")
12+
public void pointcutInsideAspectjPackage(JoinPoint joinPoint) {
13+
String methodName = joinPoint.getSignature().getName();
14+
String className = joinPoint.getTarget().getClass().getSimpleName();
15+
System.out.println("Executing method inside aspectj package: " + className + "." + methodName);
16+
}
17+
18+
@Before("execution(* com.baeldung.aspectj.service..*(..))")
19+
public void pointcutInsideServicePackage(JoinPoint joinPoint) {
20+
String methodName = joinPoint.getSignature().getName();
21+
String className = joinPoint.getTarget().getClass().getSimpleName();
22+
System.out.println("Executing method inside service package: " + className + "." + methodName);
23+
}
24+
25+
@Before("execution(* com.baeldung.aspectj..*(..)) && !execution(* com.baeldung.aspectj.repository..*(..))")
26+
public void pointcutWithoutSubPackageRepository(JoinPoint joinPoint) {
27+
String methodName = joinPoint.getSignature().getName();
28+
String className = joinPoint.getTarget().getClass().getSimpleName();
29+
System.out.println("Executing method without sub-package repository: " + className + "." + methodName);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.aspectj.repository;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
@Repository
6+
public class UserRepository {
7+
public void createUser(String name, int age) {
8+
System.out.println("User: " + name + ", age:" + age + " is created.");
9+
}
10+
11+
public void deleteUser(String name) {
12+
System.out.println("User: " + name + " is deleted.");
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.aspectj.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class MessageService {
7+
public void sendMessage(String message) {
8+
System.out.println("sending message: " + message);
9+
}
10+
11+
public void receiveMessage(String message) {
12+
System.out.println("receiving message: " + message);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.aspectj.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class UserService {
7+
public void createUser(String name, int age) {
8+
System.out.println("Request to create user: " + name + " | age: " + age);
9+
}
10+
11+
public void deleteUser(String name) {
12+
System.out.println("Request to delete user: " + name);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=aspectj
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.aspectj;
2+
3+
import com.baeldung.aspectj.service.MessageService;
4+
import com.baeldung.aspectj.service.UserService;
5+
import com.baeldung.aspectj.repository.UserRepository;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
10+
@SpringBootTest
11+
class AspectjApplicationTests {
12+
@Autowired
13+
UserService userService;
14+
15+
@Autowired
16+
MessageService messageService;
17+
18+
@Autowired
19+
UserRepository userRepository;
20+
21+
@Test
22+
void testUserService() {
23+
userService.createUser("create new user john", 21);
24+
userService.deleteUser("john");
25+
}
26+
27+
@Test
28+
void testMessageService() {
29+
messageService.sendMessage("send message from user john");
30+
messageService.receiveMessage("receive message from user john");
31+
}
32+
33+
@Test
34+
void testUserRepository() {
35+
userRepository.createUser("john", 21);
36+
userRepository.deleteUser("john");
37+
}
38+
}

0 commit comments

Comments
 (0)