eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

AspectJ is a powerful tool for handling cross-cutting concerns like logging, security, and transaction management in Java applications. A common use case is applying an aspect to all methods within a specific package. In this tutorial, we’ll learn to create a pointcut in AspectJ that matches all methods in a package, with step-by-step code examples.

To learn more about AspectJ, check out our comprehensive AspectJ tutorials.

2. Maven Dependencies

When running an AspectJ program, the classpath should contain the classes and aspects, along with the AspectJ runtime library aspectjrt:

<dependency>
    <groupId>org.aspectj</groupId> 
    <artifactId>aspectjrt</artifactId>
    <version>1.9.22.1</version>
</dependency>

In addition to the AspectJ runtime dependency, we’ll also need to include the aspectjweaver library to introduce advice to the Java class at load time:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId> 
    <version>1.9.22.1</version>
</dependency>

3. What Is a Pointcut?

A pointcut in AspectJ is a core concept defining where an aspect should be applied in the code. Aspects manage cross-cutting concerns like logging, security, or transaction management. A pointcut specifies specific points, called join points, in the program’s execution where the aspect’s advice (or action) should run. These join points can be identified using different expressions, including method signatures, class names, or specific packages.

A join point is a specific moment in program execution where an aspect can be applied. This includes method calls, executions, object instantiations, and field accesses. Advice is the action an aspect takes at a join point. It can occur before (@Before), after (@After), or around (@Around) the joining point. A pointcut expression is a declaration that defines which join points should be matched. This expression follows a specific syntax that enables it to target method executions, field accesses, and more.

3.2. Pointcut Syntax

A pointcut expression usually has two key components: the type of join point and the signature pattern. The type of join point defines the event, including a method call, method execution, or constructor execution. The signature pattern identifies specific methods or fields using class, package, parameters, or return type criteria.

4. Pointcut Expression

To create a pointcut that matches all methods in a specific package, we can use the following expression:

execution(* com.baeldung.aspectj..*(..))

Here’s a breakdown of this expression:

  • execution: The pointcut designator, specifies that we’re targeting method execution.
  • *: A wildcard indicating any return type.
  • com.baeldung.aspectj..*: Matches any class within the com.baeldung.aspectj package and any sub-packages.
  • (..): Matches any method parameters.

4.1. Logging Aspect for All Methods in a Package

Let’s create an example aspect that logs the execution of all methods within a package named com.baeldung.aspectj:

@Before("execution(* com.baeldung.aspectj..*(..))")
public void pointcutInsideAspectjPackage(JoinPoint joinPoint) {
    String methodName = joinPoint.getSignature().getName();
    String className = joinPoint.getTarget().getClass().getSimpleName();
    System.out.println(
        "Executing method inside aspectj package: " + className + "." + methodName
    );
}

The pointcut expression in @Before targets all methods within the com.baeldung.aspectj package and its sub-packages.

Let’s create UserService in the service package:

@Service
public class UserService {
    public void createUser(String name, int age) {
        System.out.println("Request to create user: " + name + " | age: " + age);
    }

    public void deleteUser(String name) {
        System.out.println("Request to delete user: " + name);
    }
}

When the UserService methods run, the aspect pointcutInsideAspectjPackage() will log both methods. Now, let’s test our code:

@Test
void testUserService() {
    userService.createUser("create new user john", 21);
    userService.deleteUser("john");
}

The aspect pointcutInsideAspectjPackage() should be invoked right before the createdUser() and deleteUser() in the UserService class are executed:

Executing method inside aspectj package: UserService.createUser
Request to create user: create new user john | age: 21
Executing method inside aspectj package: UserService.deleteUser
Request to delete user: john

Next, let’s create another class named UserRepository in a different package called the repository package:

@Repository
public class UserRepository {
    public void createUser(String name, int age) {
        System.out.println("User: " + name + ", age:" + age + " is created.");
    }

    public void deleteUser(String name) {
        System.out.println("User: " + name + " is deleted.");
    }
}

When methods in the UserRepository class are executed, the aspect pointcutInsideAspectjPackage() will log both methods. Now, let’s test our code:

@Test
void testUserRepository() {
    userRepository.createUser("john", 21);
    userRepository.deleteUser("john");
}

The aspect pointcutInsideAspectjPackage() should be invoked right before the createdUser() and deleteUser() methods in the UserService class are executed:

Executing method inside aspectj package: UserRepository.createUser
User: john, age:21 is created
Executing method inside aspectj package: UserRepository.deleteUser
User: john is deleted.

4.2. Logging Aspect for All Methods in a Sub-Package

Let’s create an example aspect that logs the execution of all methods within a package named com.baeldung.aspectj.service:

@Before("execution(* com.baeldung.aspectj.service..*(..))")
public void pointcutInsideServicePackage(JoinPoint joinPoint) {
    String methodName = joinPoint.getSignature().getName();
    String className = joinPoint.getTarget().getClass().getSimpleName();
    System.out.println(
        "Executing method inside service package: " + className + "." + methodName
    );
}

The pointcut expression inside the @Before annotation (execution(* com.baeldung.aspectj.service..*(..))) matches all methods within the com.baeldung.aspectj.service package.

Next, let’s create another class named MessageService in the service package to provide additional test cases:

@Service
public class MessageService {
    public void sendMessage(String message) {
        System.out.println("sending message: " + message);
    }

    public void receiveMessage(String message) {
        System.out.println("receiving message: " + message);
    }
}

When any method of MessageService is executed, the aspect pointcutInsideServicePackage() will log both methods. Now, let’s test our code:

@Test
void testMessageService() {
    messageService.sendMessage("send message from user john");
    messageService.receiveMessage("receive message from user john");
}

The aspect pointcutInsideAspectjPackage() previously and pointcutInsideServicePackage() should be invoked right before the method sendMessage() and receiveMessage() in the MessageService class are called:

Executing method inside aspectj package: MessageService.sendMessage
Executing method inside service package: MessageService.sendMessage 
sending message: send message from user john
Executing method inside aspectj package: MessageService.receiveMessage
Executing method inside service package: MessageService.receiveMessage
receiving message: receive message from user john

4.3. Logging Aspect by Excluding a Specific Package

Let’s create an example aspect that will exclude the execution of a specific package named com.baeldung.aspectj.service:

@Before("execution(* com.baeldung.aspectj..*(..)) && !execution(* com.baeldung.aspectj.repository..*(..))")
public void pointcutWithoutSubPackageRepository(JoinPoint joinPoint) {
    String methodName = joinPoint.getSignature().getName();
    String className = joinPoint.getTarget().getClass().getSimpleName();
    System.out.println(
        "Executing method without sub-package repository: " + className + "." + methodName
    );
}

The pointcut expression inside the @Before annotation (execution(* com.baeldung.aspectj..*(..)) && !execution(* com.baeldung.aspectj.repository..*(..))) matches all methods within the com.baeldung.aspectj package and its sub-packages and excluding the sub-package repository.

Now, let’s re-run our previous unit test. The aspect named pointcutWithoutSubPackageRepository() should be invoked right before all methods in the aspectj package while excluding the repository sub-package in this case:

Executing method inside aspectj package: UserService.createUser
Executing method inside service package: UserService.createUser
Executing method without sub-package repository: UserService.createUser
Request to create user: create new user john | age: 21
Executing method inside aspectj package: UserService.deleteUser
Executing method inside service package: UserService.deleteUser
Executing method without sub-package repository: UserService.deleteUser
Request to delete user: john
Executing method inside aspectj package: UserRepository.createUser
User: john, age:21 is created.
Executing method inside aspectj package: UserRepository.deleteUser
User: john is deleted.
Executing method inside aspectj package: MessageService.sendMessage
Executing method inside service package: MessageService.sendMessage
Executing method without sub-package repository: MessageService.sendMessage
sending message: send message from user john
Executing method inside aspectj package: MessageService.receiveMessage
Executing method inside service package: MessageService.receiveMessage
Executing method without sub-package repository: MessageService.receiveMessage
receiving message: receive message from user john

5. Conclusion

In this tutorial, we learned that a pointcut in AspectJ is a powerful tool for specifying exactly where aspect advice should be applied (such as to methods, classes, or fields).

Creating a pointcut to target all methods within the main package or a specific package is straightforward with AspectJ. We can also exclude certain packages if needed.

This approach is useful for applying the same logic such as logging or security checks across multiple classes and methods without duplicating code. By defining a pointcut for the desired package, we can keep your code clean and easy to maintain.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)