{"id":4075,"date":"2026-01-21T05:09:40","date_gmt":"2026-01-21T10:09:40","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4075"},"modified":"2026-01-21T05:09:42","modified_gmt":"2026-01-21T10:09:42","slug":"spring-aop","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/spring-aop\/","title":{"rendered":"Spring AOP"},"content":{"rendered":"\n<p>Aspect-Oriented Programming is a paradigm that allows adding additional behavior to existing code without its modification. That additional code usually is needed for multiple types and objects. It can be a logic for transaction management, logging, or security. It is calling cross-cutting concerns. Spring framework has its own library to make aspects possible (Spring AOP). We can call <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-spring-aop\/\">aspect-oriented programming<\/a> as an evolution of the decorator design pattern. But AOP provides you the possibility to enhance classes with much greater flexibility than the decorator. You can even do your manipulations with code from third-party sources.<\/p>\n\n\n\n<p>You don&#8217;t need a special compilation process for Spring AOP because it is implemented in pure Java. The approach of Spring AOP differs from that of most other AOP frameworks. It provides close integration between AOP implementation and Spring IoC, that help solve common problems in enterprise applications.<\/p>\n\n\n\n<p>Spring AOP Aspects are creating using normal bean definition syntax.<\/p>\n\n\n\n<p>But there are some things you cannot do easily with Spring AOP, AspectJ is the best choice in such cases. For example,&nbsp; advise domain objects. In general, Spring AOP provides a great solution to a lot of enterprise Java application issues. Spring AOP and AspectJ are complementary.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/07\/Spring-AOP-in-a-tech-world-1024x683.png\" alt=\"Spring AOP\" class=\"wp-image-34404\" style=\"aspect-ratio:1.4992874769733413;width:649px;height:auto\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/07\/Spring-AOP-in-a-tech-world-1024x683.png 1024w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/07\/Spring-AOP-in-a-tech-world-300x200.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/07\/Spring-AOP-in-a-tech-world-768x512.png 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/07\/Spring-AOP-in-a-tech-world-150x100.png 150w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/07\/Spring-AOP-in-a-tech-world.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Terminology<\/h2>\n\n\n\n<p>One of the most important terms of AOP is advice. An action by an aspect at a particular join-point.<\/p>\n\n\n\n<p>Join-point is a point of an application, like a method or an exception handling. In Spring AOP a joinpoint is a method execution.<\/p>\n\n\n\n<p>Pointcut is an expression that corresponds to join-points. Advice runs when the join-point matched by the pointcut. Spring by default is using the AspectJ pointcut expression language<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of AOP Advices<\/strong><\/h2>\n\n\n\n<p>Spring AOP has five types of advice.<\/p>\n\n\n\n<p><strong><em>Before advice<\/em>:<\/strong> this type of advice executes before a join point. It cannot prevent execution flow proceeding to the join point (if it does not throw an exception).<\/p>\n\n\n\n<p><strong><em>After returning advice:<\/em><\/strong> this advice executes after a normal completion of a join point (if no exception occurred).<\/p>\n\n\n\n<p><strong><em>After throwing advice<\/em>:<\/strong> this advice is executed if a method throws an exception.<\/p>\n\n\n\n<p><strong><em>After advice<\/em>:<\/strong> this advice is executed normal or exceptional return from a join point.<\/p>\n\n\n\n<p><strong><em>Around advice<\/em>:<\/strong> this kind of advice is the most powerful. It surrounds a join point such as a method invocation. This advice can invoke custom behavior before and after the method. It also decides whether to proceed to the join point or to return its own value or throwing an exception.<\/p>\n\n\n\n<p>All types of advices are implemented as interfaces in AOP and they have a hierarchy. The parent interface is Advice. Interfaces BeforeAdvice, AfterAdvice, and Interceptor extending Advice interface. MethodBeforeAdvice extends the BeforeAdvice interface, AfterReturningAdvice extends the AfterAdvice, ThrowsAdvice interface extends the AfterAdvice, MethodInterceptor interface extends the Interceptor interface and used in around advice.<\/p>\n\n\n\n<p>Let&#8217;s create an example with BeforeAdvisor by implementing MethodBeforeAdvice.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> edu.example.aop;\n\n<strong>import<\/strong> java.lang.reflect.Method;&nbsp;&nbsp;\n<strong>import<\/strong> org.springframework.aop.MethodBeforeAdvice;&nbsp;&nbsp;\n\n<strong>public<\/strong> <strong>class<\/strong> BeforeAdviceExecutor <strong>implements<\/strong> MethodBeforeAdvice {\n\n@Override\n<strong>public<\/strong> <strong>void<\/strong> before(Method method, Object[] args, Object target) <strong>throws<\/strong> Throwable {\nSystem.<strong><em>out<\/em><\/strong>.println(\"We are going to run this code before an actual logic\");&nbsp;&nbsp;\n}\n}\n\nThe class where the advice is used will be simple:\n<strong>package<\/strong> edu.example.aop;\n\n<strong>public<\/strong> <strong>class<\/strong> AdviceExample {\n\n<strong>public<\/strong> <strong>void<\/strong> method() {\nSystem.<strong><em>out<\/em><\/strong>.println(\"Here is our main logic\");&nbsp;&nbsp;\n}\n}\n<\/pre>\n\n\n\n<p>In the XML configuration file, we will create 3 beans:&nbsp; for AdviceExample class, for Advisor class, and for <a href=\"https:\/\/docs.spring.io\/spring\/docs\/current\/javadoc-api\/overview-summary.html\" rel=\"nofollow noopener\" target=\"_blank\">ProxyFactoryBean <\/a>class. The class ProxyFactoryBean is provided by Spring Framework. It contains a target and an interceptor name. The instance of the AdviceExample class will be our target object and the instance of advisor class will be an interceptor. We can have several interceptors. Here is our configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;\n&lt;beans xmlns=<em>\"http:\/\/www.springframework.org\/schema\/beans\"<\/em>\nxmlns:xsi=<em>\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/em>\nxmlns:aop=<em>\"http:\/\/www.springframework.org\/schema\/aop\"<\/em>\nxsi:schemaLocation=<em>\"http:\/\/www.springframework.org\/schema\/beans<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/aop<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd \"<\/em>&gt;\n\n&lt;bean id=<em>\"obj\"<\/em> class=<em>\"edu.example.aop.AdviceExample\"<\/em>&gt;&lt;\/bean&gt;\n&lt;bean id=<em>\"adviceExecutor\"<\/em>\nclass=<em>\"edu.example.aop.BeforeAdviceExecutor\"<\/em>&gt;&lt;\/bean&gt;\n\n&lt;bean id=<em>\"proxy\"<\/em>\nclass=<em>\"org.springframework.aop.framework.ProxyFactoryBean\"<\/em>&gt;\n&lt;property name=<em>\"target\"<\/em> ref=<em>\"obj\"<\/em>&gt;&lt;\/property&gt;\n&lt;property name=<em>\"interceptorNames\"<\/em>&gt;\n&lt;list&gt;\n&lt;value&gt;adviceExecutor&lt;\/value&gt;\n&lt;\/list&gt;\n&lt;\/property&gt;\n&lt;\/bean&gt;\n&lt;\/beans&gt;<\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<p>We are going to run this code before an actual logic<\/p>\n\n\n\n<p>Here is our main logic<\/p>\n\n\n\n<p>Let&#8217;s also create a ThrowsAdvice Example. Here is class with logic:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> edu.example.aop;\n<strong>public<\/strong> <strong>class<\/strong> Validator {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> validate(<strong>int<\/strong> age)<strong>throws<\/strong> Exception{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if<\/strong>(age&lt;18){&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>throw<\/strong> <strong>new<\/strong> ArithmeticException(\"Not Valid Age\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>else<\/strong>{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Age is good\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}\n<\/pre>\n\n\n\n<p>As we already said, we need to implement ThrowsAdvice for this type of interceptor:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> edu.example.aop;\n\n<strong>import<\/strong> org.springframework.aop.ThrowsAdvice;&nbsp;&nbsp;\n\n<strong>public<\/strong> <strong>class<\/strong> ThrowAdviceExecutor <strong>implements<\/strong> ThrowsAdvice {&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> afterThrowing(Exception ex){&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Exception occured, let's do something\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}\n<\/pre>\n\n\n\n<p>The configuration file will look very similar to the previous:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;\n&lt;beans xmlns=<em>\"http:\/\/www.springframework.org\/schema\/beans\"<\/em>\nxmlns:xsi=<em>\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/em>\nxmlns:aop=<em>\"http:\/\/www.springframework.org\/schema\/aop\"<\/em>\nxsi:schemaLocation=<em>\"http:\/\/www.springframework.org\/schema\/beans<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/aop<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd \"<\/em>&gt;\n\n&lt;bean id=<em>\"obj\"<\/em> class=<em>\"edu.example.aop.Validator\"<\/em>&gt;&lt;\/bean&gt;\n&lt;bean id=<em>\"adviceExecutor\"<\/em>\nclass=<em>\"edu.example.aop.ThrowAdviceExecutor\"<\/em>&gt;&lt;\/bean&gt;\n\n&lt;bean id=<em>\"proxy\"<\/em>\nclass=<em>\"org.springframework.aop.framework.ProxyFactoryBean\"<\/em>&gt;\n&lt;property name=<em>\"target\"<\/em> ref=<em>\"obj\"<\/em>&gt;&lt;\/property&gt;\n&lt;property name=<em>\"interceptorNames\"<\/em>&gt;\n&lt;list&gt;\n&lt;value&gt;adviceExecutor&lt;\/value&gt;\n&lt;\/list&gt;\n&lt;\/property&gt;\n&lt;\/bean&gt;\n&lt;\/beans&gt;<\/pre>\n\n\n\n<p>Here are Spring AOP dependencies for your pom.xml in the case of maven project:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;parent&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;2.2.6.RELEASE&lt;version&gt;\n&lt;\/parent&gt;\n&nbsp;&nbsp;\n&lt;dependencies&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;dependency&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;spring-boot-starter-aop&lt;\/artifactId&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/dependency&gt;\n&lt;\/dependencies&gt;<\/pre>\n\n\n\n<p>Now it is recommended to use Spring AspectJ AOP implementation with the Spring Framework. The old-style Spring 1.2 DTD-based AOP implementation is not so comfortable, because Spring AspectJ AOP provides for developers more control. It is easier to use.<\/p>\n\n\n\n<p>We can use&nbsp; Spring AOP AspectJ implementation by one of the two ways. The first way is by annotation. The second way is by XML configuration (schema based).<\/p>\n\n\n\n<p>Implementation of the AspectJ AOP provides this list of annotations:<\/p>\n\n\n\n<p>@Aspect is for the declaratio of the class as an aspect.<\/p>\n\n\n\n<p>@Pointcut is for the creation of the pointcut expression.<\/p>\n\n\n\n<p>For advices we need to use the annotations given below:<\/p>\n\n\n\n<p>@Before annotation is for the before advice. It will be executed before calling the actual method.<\/p>\n\n\n\n<p>@After declares the after advice. It will be executed after calling the actual method and before returning the result.<\/p>\n\n\n\n<p>@AfterReturning annotation is for after returning advice. It will be executed after calling the actual method and before returning the result. It is important that you can get the result value in this type of the advice.<\/p>\n\n\n\n<p>@Around annotation is for the around advice. It will be executed before and after calling the actual method.<\/p>\n\n\n\n<p>@AfterThrowing annotation is used to declare the throws advice. It will be executed if the actual method throws an exception.<\/p>\n\n\n\n<p>Let&#8217;s look at an example with the annotations. We have a Spring boot application:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> edu.example.aop;\n\n<strong>import<\/strong> org.springframework.context.ApplicationContext;\n<strong>import<\/strong> org.springframework.context.support.ClassPathXmlApplicationContext;\n\n<strong>public<\/strong> <strong>class<\/strong> AopApplicationRunner {\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ApplicationContext context =\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>new<\/strong> ClassPathXmlApplicationContext(\"aop-config.xml\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User user = (User) context.getBean(\"user\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(user);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user.throwSomeMysticException();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p><strong><em>Configuration file aop-config.xml is here:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=<em>\"1.0\"<\/em> encoding=<em>\"UTF-8\"<\/em>?&gt;\n&lt;beans xmlns=<em>\"http:\/\/www.springframework.org\/schema\/beans\"<\/em>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:xsi=<em>\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/em>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:aop=<em>\"http:\/\/www.springframework.org\/schema\/aop\"<\/em>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xsi:schemaLocation=<em>\"http:\/\/www.springframework.org\/schema\/beans<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/aop<\/em>\n<em>&nbsp;&nbsp;&nbsp;&nbsp;http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd \"<\/em>&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;aop:aspectj-autoproxy\/&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- Definition for user bean --&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;bean id=<em>\"user\"<\/em> class=<em>\"edu.example.aop.User\"<\/em>&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=<em>\"name\"<\/em>&nbsp; value=<em>\"Mary\"<\/em> \/&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=<em>\"surname\"<\/em>&nbsp; value=<em>\"Luck\"<\/em> \/&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;property name=<em>\"age\"<\/em>&nbsp; value=<em>\"35\"<\/em> \/&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/bean&gt;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- Definition for logging aspect --&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;bean id=<em>\"logging\"<\/em> class=<em>\"edu.example.aop.Logging\"<\/em>\/&gt;\n\n&lt;\/beans&gt;<\/pre>\n\n\n\n<p>There is one class User that we initialize in this configuration file too. Let&#8217;s look at this class:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> edu.example.aop;\n\n<strong>public<\/strong> <strong>class<\/strong> User {\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>private<\/strong> String name;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>private<\/strong> String surname;\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>private<\/strong> Integer age;\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> String getName() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> name;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> setName(String name) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>this<\/strong>.name = name;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> String getSurname() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> surname;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> setSurname(String surname) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>this<\/strong>.surname = surname;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> Integer getAge() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> age;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> setAge(Integer age) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>this<\/strong>.age = age;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> throwSomeMysticException(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"We have some mystic exception here:\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>throw<\/strong> <strong>new<\/strong> ClassCastException();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;@Override\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> String toString() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return<\/strong> \"User:\\n\" +\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"Name: \" + name + '\\n' +\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"Surname: \" + surname + '\\n' +\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"Age: \" + age + \"\\n\";\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p><strong><em>We are going to create aspects for logging:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>package<\/strong> edu.example.aop;\n\n<strong>import<\/strong> org.aspectj.lang.annotation.*;\n\n@Aspect\n<strong>public<\/strong> <strong>class<\/strong> Logging {\n\n&nbsp;&nbsp;&nbsp;&nbsp;@Pointcut(\"execution(* edu.example.aop.*.*(..))\")\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> selectAllMethodsAvaliable() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;@Before(\"selectAllMethodsAvaliable()\")\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> beforeAdvice() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Now we are going to initiate user's profile.\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;@After(\"selectAllMethodsAvaliable()\")\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> afterAdvice() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"User's profile has been initiated.\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;@AfterReturning(pointcut = \"selectAllMethodsAvaliable()\", returning = \"someValue\")\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> afterReturningAdvice(Object someValue) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"Value: \" + someValue.toString());\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;@AfterThrowing(pointcut = \"selectAllMethodsAvaliable()\", throwing = \"e\")\n&nbsp;&nbsp;&nbsp;&nbsp;<strong>public<\/strong> <strong>void<\/strong> inCaseOfExceptionThrowAdvice(ClassCastException e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<strong><em>out<\/em><\/strong>.println(\"We have an exception here: \" + e.toString());\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n<\/pre>\n\n\n\n<p><strong><em>The output will be:<\/em><\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Now we are going to initiate user&#8217;s profile.<br>User&#8217;s profile has been initiated.<br>Value: User:<br>Name: Mary<br>Surname: Luck<br>Age: 35<\/p>\n\n\n\n<p>User:<br>Name: Mary<br>Surname: Luck<br>Age: 35<\/p>\n<\/blockquote>\n\n\n\n<p>Now we are going to initiate user&#8217;s profile.<br>We have some mystic exception here:<br>User&#8217;s profile has been initiated.<br>We have an exception here: java.lang.ClassCastException<\/p>\n\n\n\n<p>Let&#8217;s summarize why AOP is useful. First of all, it is quick code reuse. We can reuse the code of aspects across many classes without touching much of the existing code. We can enhance numerous classes without repeating code, just by using simple annotation.<\/p>\n\n\n\n<p>Also, it helps to deal with third-party code. For example, we need to inject shared behavior into a function that is using in core components. Unfortunately, we can&#8217;t alter the third-party code&#8217;s behavior. Even in the case of open-source code we need to understand and adapt it to our needs. The AOP allows us to decorate the needed behavior without touching the third-party code.<\/p>\n\n\n\n<p>If you apply AOP your code corresponds to the single-responsibility principle. You can use aspects for authentication, logging, tracing, error handling, and the like.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enterprise Architecture Benefits of Spring AOP<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Clean Separation of Concerns with Spring AOP<\/h3>\n\n\n\n<p>By isolating cross-cutting responsibilities such as logging, validation, and auditing from core business logic, development teams can maintain a clearer architectural structure. This approach improves code readability, shortens onboarding time for new engineers, and makes long-term maintenance easier because infrastructure-related behavior can evolve independently of functional requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Observability and Performance with Spring AOP<\/h2>\n\n\n\n<p>In production environments, centralized monitoring is essential for maintaining system reliability. Using aspects to capture execution times, request traces, and operational metrics enables teams to gain visibility into application behavior without embedding diagnostic logic across multiple layers. This method supports faster root-cause analysis and aligns well with modern DevOps and SRE practices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Security Enforcement Using Spring AOP<\/h2>\n\n\n\n<p>Security policies often need to be applied consistently across services and modules. Defining authentication, authorization, and data-handling rules as reusable aspects ensures that new features automatically comply with organizational standards. This reduces the likelihood of overlooked checks and strengthens overall application resilience against common threats.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing and Maintainability in Spring AOP<\/h2>\n\n\n\n<p>Because aspects are managed as standard components within the application context, they can be easily enabled, disabled, or mocked during testing. This flexibility allows teams to verify business logic in isolation while still validating cross-cutting behaviors through targeted integration tests, resulting in higher confidence during releases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scaling Enterprise Systems with Spring AOP<\/h2>\n\n\n\n<p>As applications grow in size and complexity, modular extension points become increasingly valuable. By using aspects to introduce new policies, compliance rules, or operational features, organizations can adapt to changing requirements without extensive refactoring. This supports continuous improvement while preserving system stability and architectural integrity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Spring AOP Matters in Enterprise Development<\/h2>\n\n\n\n<p>Spring AOP provides a practical and scalable way to manage cross-cutting concerns such as security, logging, performance monitoring, and compliance without cluttering business logic. By integrating seamlessly with the Spring ecosystem, it helps teams build modular, testable, and maintainable systems that can evolve with changing technical and organizational requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aspect-Oriented Programming is a paradigm that allows adding additional behavior to existing code without its modification. That additional code usually is needed for multiple types and objects. It can be a logic for transaction management, logging, or security. It is calling cross-cutting concerns. Spring framework has its own library to make aspects possible (Spring AOP). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4082,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[1126,1080,1124,1125],"class_list":["post-4075","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-aop-advices","tag-spring-aop","tag-terminology","tag-types-of-aop"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4075"}],"version-history":[{"count":1,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4075\/revisions"}],"predecessor-version":[{"id":34405,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4075\/revisions\/34405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4082"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}