<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kaan Çetin on Medium]]></title>
        <description><![CDATA[Stories by Kaan Çetin on Medium]]></description>
        <link>https://medium.com/@cetinkaan?source=rss-b7f3d3f099e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dClxgSYAtkm_TtBntBu7bg.jpeg</url>
            <title>Stories by Kaan Çetin on Medium</title>
            <link>https://medium.com/@cetinkaan?source=rss-b7f3d3f099e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 22:20:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cetinkaan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Logging methods with custom annotation in Spring AOP]]></title>
            <link>https://medium.com/garantibbva-teknoloji/logging-methods-with-custom-annotation-in-spring-aop-2d4a4da5fbc1?source=rss-b7f3d3f099e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d4a4da5fbc1</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Kaan Çetin]]></dc:creator>
            <pubDate>Tue, 21 Dec 2021 21:04:42 GMT</pubDate>
            <atom:updated>2021-12-23T06:22:47.022Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we’ll learn Spring AOP and create custom annotation for some practical use cases to log our methods for auditing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TIGhrhlAQkbM-mG2IMaKiQ.jpeg" /></figure><h3><strong>Introduction</strong></h3><p><em>Aspect-Oriented Programming</em> (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the <em>aspect</em>. Aspects enable the modularization of concerns such as logging, caching, auditing, transaction management that cut across multiple types and objects.</p><p>To quickly summarize, <strong>it is a way for adding behavior to existing code without modifying that code</strong>.</p><h3>Let’s Code</h3><h4>Dependency</h4><p>We need Spring AOP dependency in our project.</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><h4>Custom Annotation</h4><p>This annotation will be used to log our method message and viewed data for auditing. We are using two parameters for this annotation.</p><p>The value parameter allows our fields to be displayed with the names we want in the log. Readability is important in audit logs</p><p>and showData is for security. We may not want show the data returned from the request to be logged. In this case only the name of the field in the request is shown.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c5849848110ef9de15596c5737bbe74/href">https://medium.com/media/9c5849848110ef9de15596c5737bbe74/href</a></iframe><h4>Aspect</h4><p>Now that we have our annotation, let’s create our aspect. This is just the module that will encapsulate our cross-cutting concern, which is, in our case, logging of method message and returned fields for auditing.</p><p>In Spring AOP, aspects are implemented by using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style). We’ve also included the @Component<em> </em>annotation, as our class also needs to be a Spring bean to be detected.</p><pre>@Aspect<br>@Component<br>public class <strong>Log</strong> {<br>}</pre><h4>Pointcut and Advice</h4><p>To understand pointcut and advice, we must first understand join point.</p><p><em>Join point</em>: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point <em>always</em> represents a method execution.</p><p><em>Advice</em>: action taken by an aspect at a particular join point. Different types of advice include “around,” “before” and “after” advice. Many AOP frameworks, including Spring, model an advice as an <em>interceptor</em>, maintaining a chain of interceptors <em>around</em> the join point.</p><p><em>Pointcut</em>: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.</p><p>There are types of advices;</p><ul><li><em>Before advice</em>: executes before a join point</li><li><em>After returning advice</em>: executed after a join point completes normally</li><li><em>After throwing advice</em>: executed if a method exits by throwing an exception.</li><li><em>After (finally) advice</em>: executed regardless of the means by which a join point exits (normal or exceptional return).</li><li><em>Around advice</em>: surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.</li></ul><p>In this case we use @AfterReturning as per our needs.</p><pre>@AfterReturning(pointcut = @annotation(Logger)&quot;, returning = &quot;object&quot;)<br>private static void log(JoinPoint joinPoint, Object object) throws IllegalAccessException {<br>}</pre><p>Now we have enough knowledge, all we need to do is add some extra logic to our advice. This will be what logs the method message and returned data.</p><p>Let’s some code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa973113bfc070662b348e6627138a16/href">https://medium.com/media/aa973113bfc070662b348e6627138a16/href</a></iframe><p>We’ve not done anything complicated here. We’ve just get method message and check returned data via reflection, then printed as an info log to the console. The reason we use reflection here is to control the fields of the object to be returned and understand how it will be logged. Since these logs are thrown for auditing, we need to specify which fields the user can see and which field’s data can or cannot be seen.</p><p>Now, let’s try annotating a method with @Log<em>, </em>and then executing it to see what happens.</p><h4>Annotation Usage</h4><p>We are creating a simple Employee controller and CRUD methods. We put our <em>@Log</em> annotation above the methods for enable logging and log message.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86652c47637455e426a55d3eda8d26f9/href">https://medium.com/media/86652c47637455e426a55d3eda8d26f9/href</a></iframe><p>And we are creating our Employee entity. We also put our <em>@Log </em>annotation above the fields for logging returned data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecddae00521eefed24169abb3b6f6132/href">https://medium.com/media/ecddae00521eefed24169abb3b6f6132/href</a></iframe><p>Let’s try the code.</p><pre>$ curl --location --request GET &#39;http://localhost:8080/employees/1&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QD0ZW1duUoWnqhfwgeMV8w.png" /><figcaption>console output</figcaption></figure><p>The logging statements are printed in the console.</p><h3>Conclusion</h3><p>In this article, we’ve used Spring Boot AOP to create our custom annotation and developed audit log infrastructure without changing our existing codes. As Garanti BBVA, we made improvements for audit logs with a similar logic, using Spring AOP without changing our existing codes.</p><p>We kept our code simple to understand the logic of AOP. In cases where there is an entity in an entity, we can improve our code by using it by recursive.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d4a4da5fbc1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/garantibbva-teknoloji/logging-methods-with-custom-annotation-in-spring-aop-2d4a4da5fbc1">Logging methods with custom annotation in Spring AOP</a> was originally published in <a href="https://medium.com/garantibbva-teknoloji">Garanti BBVA Teknoloji</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>