Spring AOP – @Before Advice Type

Spring AOP – @Before Advice Type

As we have discussed, AOP (Aspect-Oriented Programming) is one of the powerful features of the Spring framework. It complements the Object-Oriented Programming (OOPs). AOP provides some concepts and terminology, and Advice is one of them. 

Advice: An action taken by an aspect at a particular join point is called Advice. In other words, an advice is an interceptor, retaining a chain of interceptors "around" the join point is called Advice. It represents the code that is executed by an aspect at a particular join point. There are three types of advice available in AOP: "around," "before" and "after" advice.

Before Advice: It is executed before a method is invoked. Also, it is performed before a join point, but it cannot prevent the execution flow proceeding to the join point.

Here, we are using the @Before annotation for declaring a method as before advice.

Example of @Before Advice

Let’s understand the @Before advice type with the help of an example. In this example, we are going to create the following Java classes step by step:

  • Add Maven dependencies required in the project

Along with Spring dependencies, Spring AOP projects require one more dependency of AspectJ, which will be added inside the pom.xml.

Below is given the dependency of AspectJ:

pom.xml


 
  aspectj
  aspectjweaver
  1.5.4
  
  • Create the DAO class

In this step, we are going to create a DAO class named StudentAccountDao.java, that contains the actual business logic of the application.

StudentAccountDao.java

import org.springframework.stereotype.Component;
@Component
public class StudentAccountDao {
public void newAccount() {
System.out.println(getClass() + " : To open a new acount" );
}
} 
  • Create the Class that performs component scanning

In this step, we are going to create a class that performs component scanning. We use @EnableAspectJAutoProxy annotation that enables support for handling components marked as @Aspect.

DemoAOP.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(" com.app.SpringAspectOP ")
public class DemoAOP {
} 
  • Create the Aspect class

In this step, we are going to create an aspect class named AspectClass that contains the aspect logic, such as all AOP advice types that are defined in this class.

AspectClass.java

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectClass {
@Before("execution(public void newAccount())")
public void BeforeNewAccount() {
System.out.println( " @Before Advice - Executing the @Before advice " );
} 
} 
  • Create the main class

In this step, we are going to create the main class named App.java that contains the main method.

App.java

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App 
{
public static void main( String[] args )
{
AnnotationConfigApplicationContext appcontext = new AnnotationConfigApplicationContext(DemoAOP.class);
StudentAccountDao studacc = appcontext.getBean("studentAccountDao", StudentAccountDao.class);
studacc.newAccount();
appcontext.close(); 
}
} 

Output

In the following output we can see that @Before advice is executed before executing the actual method.

@Before advice is executed

Related Topics

Spring Tutorial

What is Spring Framework? Spring is one of the most popular enterprise application framework used for developing Java applications. “Spring is a lightweight open-source framework. It allows Java developer to build simple, reliable,...

6 minutes read.

Spring Autowiring – Setter Injection

Spring Autowiring – Setter Injection We have previously discussed an example of Setter Dependency Injection (SDI) using an XML file. In the previous case, we have defined the property name for the dependency...

2 minutes read.

Spring AOP – @Before Advice Type

Spring AOP – @Before Advice Type As we have discussed, AOP (Aspect-Oriented Programming) is one of the powerful features of the Spring framework. It complements the Object-Oriented Programming (OOPs). AOP provides some concepts and terminology,...

2 minutes read.

Spring – Dependency Injection

Spring – Dependency Injection One of the main features of Inversion of Control (IoC) is Dependency Injection (DI). It is a process of supplying the dependencies from one object to others. The Spring-Core container...

8 minutes read.

Spring MVC Example – Reading HTML Form Data

We have discussed earlier a simple example of Spring MVC in which we have created multiple view pages. Here, we are going to create another Spring MVC example in which we are...

7 minutes read.

Spring - Bean lifecycle using annotations

Spring - Bean lifecycle using annotations We have already discussed the bean lifecycle using an XML-based configuration. In the previous case, we have declared the init-method and destroy-method attributes in the bean-configuration file. Here, we...

2 minutes read.

Spring MVC Form Text Field

We have previously discussed the Spring MVC form tags. The form tag library provides a variety of form tags that are used to build web applications. One of them is a <form:input>...

8 minutes read.

Spring AOP - @AfterThrowing Advice

Spring AOP - @AfterThrowing Advice As we have discussed in our previous tutorial, Advice is one of the essential concepts of the Spring AOP. The after throwing advice is one of the types of...

3 minutes read.

Spring MVC Form – Drop-Down List

We have already discussed the Spring MVC form tags available in the form tag library. Form tags are used to develop web applications easily. In this tutorial, we are going to discuss the...

13 minutes read.

Spring AOP – Pointcut Expressions

Spring AOP – Pointcut Expressions Pointcut refers to a collection of join points that specify where advice to be applied. In other words, pointcut represents a set of various join points. It defines that...

7 minutes read.

Spring - Injecting values from Property Files

Spring - Injecting values from Property Files In this section, we will discuss how to inject values from a property file using the Java-based configuration. Here, we will use two annotations @Value and @PropertySource...

3 minutes read.

Spring MVC Tutorial

Spring MVC is a web Model-View-Controller framework built on Servlet API. It is used to build web applications. The MVC framework works around central servlet called DispatcherServlet. It dispatches the requests to the...

4 minutes read.

Spring MVC Regular Expression Validation

Example of Regular Expression Validation Here, we are going to create an example of regular expression validation. Following are the steps to create an example of a regular expression: Create a request page In this step, we are going...

13 minutes read.

Spring – Beans using @Bean

Spring – Beans using @Bean We have previously discussed an example of Spring configuration using an XML-based configuration. In the previous example, we have defined beans inside the bean-configuration file (applicationContext.xml). To make the...

3 minutes read.

Spring – Annotation-based Inversion of Control (IoC)

Spring – Annotation-based Inversion of Control (IoC) We have previously discussed an example of Spring IoC (Inversion of Control). In the previous case, we have used the bean-configuration file to define the bean...

3 minutes read.

Spring – Configuration using @Configuration

Spring – Configuration using @Configuration We have previously discussed the Spring configuration using an XML configuration file. In that case, we have defined all the bean information in the bean-configuration file (applicationContext.xml). ...

3 minutes read.

Spring MVC Form Tag Library

The Spring MVC provides a variety of tags available in the form of a tag library, which is used to develop web pages (JSP view pages). The Spring’s form tag...

6 minutes read.

Spring Autowiring –Field Injection

Spring Autowiring –Field Injection The Field Injection is used to inject the fields, variables, or properties of the beans. It is not available in autowiring using XML- based configuration. It is a...

2 minutes read.

Spring – Bean Lifecycle

Spring – Bean Lifecycle Spring bean is responsible for managing the lifecycle of beans created by the Spring container.  The lifecycle of the bean is easy to understand. When a bean is created, it requires...

4 minutes read.

Spring MVC Form – Radio Button

A variety of form tags are available in Spring MVC for developing web applications, <form:radiobutton> tag is one of them. The radio button tag is used to choose only one option among a...

10 minutes read.