DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring AOP

Spring AOP

Artur Mkrtchyan user avatar by
Artur Mkrtchyan
·
May. 18, 11 · Interview
Like (0)
Save
Tweet
Share
15.12K Views

Join the DZone community and get the full member experience.

Join For Free

 Let's define some well known problems

  • Some programming tasks cannot be neatly encapsulated in objects, but must be scattered throughout the code
  • Examples:
    • Logging (tracking program behavior to a file)
    • Profiling (determining where a program spends its time)
    • Tracing (determining what methods are called when)
    • Transaction Management
    • Security Management
  • The result is crosscuting code--the necessary code “cuts across” many different classes and methods

 Above-mentioned problems are easey solvable via AOP.

 Now let's define some AOP concepts

  • Aspect-oriented programming (AOP) provides for simplified application of cross-cutting concerns
  • Joinpoint - Execution point to target
    • Typically, methods
  • Advice - Action taken at a particular joinpoint.
  • Pointcut - A set of joinpoints specifying where advice should be applied (e.g. Regular expression)
 

 Now it's time to move to Spring AOP

  • Generally, Spring AOP uses the following techniques.
    • Uses Dynamic Proxies if interface available otherwise CGLIB
    • CGLIB creates derived class which proxies requests
  • Less capable than AspectJ
    • does not have field interception
    • only runtime weaving solution is available

 Let's see real Logger example

// Biz Interface
public interface MyBiz {
public void doBizMethod();
}

//Biz Interface Impl
public class MyBizImpl implements MyBiz {
public void doBizMethod() {
System.out.println("Business Method");
}

}
//Around Advice Class
public class MethodLogger implements MethodInterceptor {

public MethodLogger() {}
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
// same with MethodBeforeAdvice
System.out.println("AOP: Before method call!");
try {
// proceed to original method call
Object result = methodInvocation.proceed();
// same with AfterReturningAdvice
System.out.println("AOP: After method call!");
return result;
} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println("AOP: Throw exception!");
throw e;
}
}

}

 Spring Configuration File

<beans xmlns="http://www.springframework.org/schema/beans“ xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:aop=http://www.springframework.org/schema/aop xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<!-- business beans -->
<bean id="myBiz" class="com.attask.seminar.aop.spring.MyBizImpl"/>
<!-- aop -->
<bean id="methodLogger" class="com.attask.seminar.aop.spring.MethodLogger" />
<bean id="myBizProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="myBiz" />
<property name="interceptorNames">
<list> <value>methodLogger</value></list>
</property>
</bean>

</beans>
// Simple Usage
public static void main(String[] args) {
ClassPathXmlApplicationContext ctxt = new ClassPathXmlApplicationContext("SpringBeans.xml");
MyBiz bizInstance = (MyBiz)ctxt.getBean("myBizProxy");
bizInstance.doBizMethod();
}

// Result will be
AOP: Before method call!
Business Method
AOP: After method call!

 Note Try to use advice which is appropriate for your specific case (Before, After, Throws)

 Find attached example under Resources Section.

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Most Popular Frameworks for Building RESTful APIs
  • Apache Kafka Is NOT Real Real-Time Data Streaming!
  • Tackling Technology Challenges for Social Networking Applications With a Distributed SQL Database
  • What Are SLOs, SLIs, and SLAs?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: