<div dir="ltr" style="text-align: justify;">In this spring aop before advice example, we will discuss how to use aspectj @Before annotation with java configuration in the application. In Spring AOP Before Advice is that executes before a join point i.e a method which annotated with AspectJ @Before annotation run exactly before the all methods matching with pointcut expression. But Before Advice does not have the ability to prevent execution flow proceeding to the join point.Let&#8217;s create a simple spring application and add logging aspect to be invoked every joint point in the service class in the application. This example is also available with XML configuration in the application <a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><b>Spring AOP Before Advice Example</b></a>.</p>
<h2><b>AspectJ @Before Annotation</b></h2>
<p>@Before annotation is not spring annotation, it is aspectj lib annotation so we have add aspectj maven dependency with spring aop in this application. Let&#8217;s see below <b>LoggingAspect </b>class with before advice annotation.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p> ;</p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Aspect 
@Component 
public class LoggingAspect { 
 
 /** 
 * Declaring before advice 
 * @param jp 
 * @throws Throwable 
 */ 
 //@Before("execution(* com.doj.aopapp.service.*.*(..))") // before advice with pointcut expression directly 
 @Before("logForAllMethods()") //before advice with named pointcut that declared as name logForAllMethods() 
 public void beforeAdviceForAllMethods(JoinPoint jp) throws Throwable { 
 System.out.println("****LoggingAspect.beforeAdviceForAllMethods() " + jp.getSignature().getName()); 
 } 
 
 /** 
 * Declaring before advice for all transfer methods whose taking three arguments of any type 
 * of all classes in the package com.doj.aopapp.service 
 * @param jp 
 * @throws Throwable 
 */ 
 @Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
 public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable { 
 System.out.println("****LoggingAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName()); 
 } 
 
 /** 
 * Declaring named pointcut 
 */ 
 @Pointcut("execution(* com.doj.aopapp.service.*.*(..))") 
 public void logForAllMethods(){} 
} 
 
</pre>
<h3><b>Pointcut expressions</b></h3>
<p><b>1.</b> Following pointcut expression for before advice is valid for all public methods with any arguments of any type and any return type of all classes in the <b><i>com.doj.aopapp.service</i></b> package.</p>
<pre class="highlight">@Before("execution(* com.doj.aopapp.service.*.*(..))") 
</pre>
<p><b>2.</b> Following pointcut expression for before advice is valid for all public methods whose name are <b>transfer() </b>with taking three arguments of any type and any return type of all classes in the <b><i>com.doj.aopapp.service</i></b> package.</p>
<pre class="highlight">@Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
</pre>
<h3><b>Named Pointcut</b></h3>
<p>We can also define pointcut methods for named pointcut. Actually named pointcut is nothing but it is simple way to declaring common pointcut expressions above a method with @Pointcut annotation. This helps to reduce the complexity of writing common complex pointcut expression.</p>
<pre class="highlight">/** 
* Declaring named pointcut 
 */ 
@Pointcut("execution(* com.doj.aopapp.service.*.*(..))") 
public void logForAllMethods(){} 
 
/** 
* before advice with named pointcut that declared as name logForAllMethods() 
 */ 
@Before("logForAllMethods()") //before advice with name pointcut that declared as name logForAllMethods() 
public void beforeAdviceForAllMethods(JoinPoint jp) throws Throwable { 
 System.out.println("****LoggingAspect.beforeAdviceForAllMethods() " + jp.getSignature().getName()); 
} 
</pre>
<h2><b>Spring AOP AspectJ @Before Annotation Example</b></h2>
<p>Now let&#8217;s see complete example of Spring AOP aspectj @Before annotation.</p>
<h3><b>Spring AOP and AspectJ Maven Dependencies</b></h3>
<pre class="highlight"><;properties>; 
 <;spring.version>;4.3.7.RELEASE<;/spring.version>; 
 <;aspectj.version>;1.8.9<;/aspectj.version>; 
 <;/properties>; 
 
 <;dependencies>; 
 <;dependency>; 
 <;groupId>;org.springframework<;/groupId>; 
 <;artifactId>;spring-context<;/artifactId>; 
 <;version>;${spring.version}<;/version>; 
 <;/dependency>; 
 <;dependency>; 
 <;groupId>;org.springframework<;/groupId>; 
 <;artifactId>;spring-context-support<;/artifactId>; 
 <;version>;${spring.version}<;/version>; 
 <;/dependency>; 
 <;dependency>; 
 <;groupId>;org.springframework<;/groupId>; 
 <;artifactId>;spring-aop<;/artifactId>; 
 <;version>;${spring.version}<;/version>; 
 <;/dependency>; 
 
 <;dependency>; 
 <;groupId>;org.aspectj<;/groupId>; 
 <;artifactId>;aspectjrt<;/artifactId>; 
 <;version>;${aspectj.version}<;/version>; 
 <;/dependency>; 
 <;dependency>; 
 <;groupId>;org.aspectj<;/groupId>; 
 <;artifactId>;aspectjweaver<;/artifactId>; 
 <;version>;${aspectj.version}<;/version>; 
 <;/dependency>; 
 <;/dependencies>; 
</pre>
<h3><b>ApplicationContext Configuration file based on Java Config </b></h3>
<p><b>AppConfig.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.config; 
 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.EnableAspectJAutoProxy; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Configuration 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages={"com.doj.aopapp.aspect", "com.doj.aopapp.service"}) 
public class AppConfig { 
 
} 
 
</pre>
<h3><b>@EnableAspectJAutoProxy in Java Configuration:</b></h3>
<p>This spring aop annotation is used to enable @AspectJ support with Java @Configuration add this @EnableAspectJAutoProxy annotation to the application context file.</p>
<pre class="highlight">@Configuration 
@EnableAspectJAutoProxy 
public class AppConfig { 
 
} 
</pre>
<h3><b>Enabling @AspectJ Support with XML Configuration:</b></h3>
<p>To enable @AspectJ support with XML based configuration use the <b><i><;aop:aspectj-autoproxy/>; </i></b>element.</p>
<pre class="highlight"><;!-- Enable @AspectJ annotation support -->; 
 <;aop:aspectj-autoproxy />; 
</pre>
<p><b>Target method of Service class on which aspects needs to apply</b></p>
<p><b>TransferService.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.service; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public interface TransferService { 
 
 void transfer(String accountA, String accountB, Long amount); 
 
 Double checkBalance(String account); 
 
 Long withdrawal(String account, Long amount); 
 
 void diposite(String account, Long amount); 
} 
 
</pre>
<p><b>TransferServiceImpl.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.service; 
 
import org.springframework.stereotype.Service; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Service 
public class TransferServiceImpl implements TransferService { 
 
 @Override 
 public void transfer(String accountA, String accountB, Long amount) { 
 System.out.println(amount+" Amount has been tranfered from "+accountA+" to "+accountB); 
 } 
 
 @Override 
 public Double checkBalance(String account) { 
 return new Double(50000); 
 } 
 
 @Override 
 public Long withdrawal(String account, Long amount) { 
 return amount; 
 } 
 
 @Override 
 public void diposite(String account, Long amount) { 
 System.out.println(amount+" Amount has been diposited to "+account); 
 } 
 
} 
 
</pre>
<p><b>Aspect class:</b><br />
LoggingAspect.java as given above in this tutorial.</p>
<h3><b>Test Class for Spring AspectJ Configuration and Execution</b></h3>
<p>Now let’s see how to run and what would be output of this application when above configured aspects execute on given pointcut information.</p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.test; 
 
import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
 
import com.doj.aopapp.config.AppConfig; 
import com.doj.aopapp.service.TransferService; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class Main { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); 
 TransferService transferService = applicationContext.getBean(TransferService.class); 
 transferService.transfer("accountA", "accountB", 50000l); 
 System.out.println("Available balance: " +transferService.checkBalance("accountA")); 
 transferService.diposite("accountA", 50000l); 
 System.out.println("Withdrawal amount: " +transferService.withdrawal("accountB", 40000l)); 
 applicationContext.close(); 
 } 
 
} 
 
</pre>
<h3><b>Output on the Console:</b></h3>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">Mar 07, 2017 4:39:17 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh<br />
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6576fe71: startup date [Tue Mar 07 16:39:17 IST 2017]; root of context hierarchy<br />
****LoggingAspect.beforeAdviceForAllMethods() transfer<br />
****LoggingAspect.beforeAdviceForTransferMethods() transfer<br />
50000 Amount has been tranfered from accountA to accountB<br />
****LoggingAspect.beforeAdviceForAllMethods() checkBalance<br />
Available balance: 50000.0<br />
****LoggingAspect.beforeAdviceForAllMethods() diposite<br />
50000 Amount has been diposited to accountA<br />
****LoggingAspect.beforeAdviceForAllMethods() withdrawal<br />
Withdrawal amount: 40000<br />
Mar 07, 2017 4:39:17 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose<br />
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6576fe71: startup date [Tue Mar 07 16:39:17 IST 2017]; root of context hierarchy</div>
<p>Analyse the output, all join points execute with given information in this example.</p>
<h3><b>Project Structure</b></h3>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/03/Before-Annotation-Advice-Example.jpg" border="0" /></div>
<h2><b>Application Source Code</b></h2>
<p><b><a href="https://github.com/DOJ-SoftwareConsultant/SpringAOP-Before-Advice-Annotation" target="_blank" rel="noopener">Spring AOP AspectJ @Before Annotation Advice Example on GitHub</a>.</b></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Spring AOP Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/after-advice-type-and-around-advice/"><span style="color: red;">Advices in Spring AOP </span></a></b></li>
<li><b><a href="https://dineshonjava.com/joinpoints-and-advice-arguments-chapter/"><span style="color: red;">Spring AOP JoinPoints and Advice Arguments</span></a></b></li>
<li><b><a href="https://dineshonjava.com/pointcuts-and-wildcard-expressions/"><span style="color: red;">Spring AOP-Declaring pointcut Expressions with Examples</span></a></b></li>
<li><b><a href="https://dineshonjava.com/aop-xml-configuration-chapter-30/"><span style="color: red;">Spring AOP XML configuration</span></a></b></li>
<li><b><a href="https://dineshonjava.com/xml-schema-based-aop-example/"><span style="color: red;"> Spring AOP XML Schema based Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;">Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-first-aspectj-program-in-spring/"><span style="color: red;">Spring AOP Writing First AspectJ Program in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '54'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…