Weaving in AOP
- It is an essential aspect of Spring's Aspect-Oriented Programming (AOP) framework.
- Spring provides a way to weave aspects into your application at runtime.
- Spring AOP supports both proxy-based and AspectJ-based weaving.
1. Proxy-Based AOP Example: In this example, we'll use Spring's proxy-based AOP to log method calls in a service class.
Step-1: Create a Spring Configuration file i.e. config.xml in the main class. This will be used to configure the Spring component and AOP aspects.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- bean definitions here -->
<context:component-scan
base-package="com.aop.aopexample.service" />
<aop:aspectj-autoproxy />
<bean class="com.aop.aopexample.aspect.Aspect" />
</beans>
Step-2: Create a Service Class.
package com.aop.aopexample.service;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public void doSomething() {
System.out.println("Inside doSomethingMethod!!");
}
public void doAnotherThing() {
System.out.println("Inside doAnotherThingMethod!!");
}
}
Step-3: Create an Aspect Class.
package com.aop.aopexample.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Before(value = "execution(* com.aop.aopexample.service.MyService.*(..))")
public void printBefore() {
System.out.println("Please Signin");
}
}
Step-4: The Main Application Class
package com.aop.aopexample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aop.aopexample.service.MyService;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/aop/aopexample/config.xml");
MyService myObject = context.getBean(MyService.class);
myObject.doSomething();
myObject.doAnotherThing();
}
}
Step-5: Now Run the main class i.e. App.java
Here the Before Advice Execute first then the doSomething() method After that again Before Advice will be executed then doAnotherThing() method.
Output:
Please Signin
Inside doSomethingMethod!!
Please Signin
Inside doAnotherThingMethod!!
2. AspectJ-based AOP Example: For AspectJ-based AOP, you need to use AspectJ annotations and enable AspectJ weaving. If you need more powerful weaving capabilities, you can use AspectJ with Spring. Below is the Java-based example:
Step-1: Create a Spring Configuration file i.e. config.class. This will be used to configure the Spring component and AOP aspects.
package com.aop.aopexample.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.aop.aopexample.aspect.LoggingAspect;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.aop.aopexample.service")
public class AppConfig {
@Bean
public LoggingAspect myAspect() {
return new LoggingAspect();
}
}
Step-2: Create a Service Class.
package com.aop.aopexample.service;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public void doSomething() {
System.out.println("Inside doSomethingMethod!!");
}
public void doAnotherThing() {
System.out.println("Inside doAnotherThingMethod!!");
}
}
Step-3: Create an Aspect Class.
package com.aop.aopexample.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Before(value = "execution(* com.aop.aopexample.service.MyService.*(..))")
public void printBefore() {
System.out.println("Please Signin");
}
}
Step-4: The Main Application Class
package com.aop.aopexample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.aop.aopexample.config.AppConfig;
import com.aop.aopexample.service.MyService;
public class App {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myObject = context.getBean(MyService.class);
myObject.doSomething();
myObject.doAnotherThing();
}
}
Step-5: Now Run the main class i.e. App.java
Here the Before Advice Execute first then the doSomething() method after that again Before Advice will be executed then doAnotherThing() method.
Output:
Please Signin
Inside doSomethingMethod!!
Please Signin
Inside doAnotherThingMethod!!
Key Points:
- The output for AspectJ-based AOP is the same as the proxy-based AOP because we use the same aspect logic.
- The key difference is that the weaving occurs at runtime with AspectJ's capabilities in AspectJ-based AOP.
- Both proxy-based and AspectJ-based AOP can achieve similar functionality, but AspectJ offers more advanced weaving capabilities and is suitable for complex scenarios.
- Proxy-based AOP is simpler to set up and is sufficient for many use cases.