Weaving Example
Weaving is a core concept of Springs Aspect-Oriented Programming (AOP).
It is the process of applying aspects (cross-cutting concerns) to your main application logic at specified join points.
Spring supports
Step 2: Service class
Spring supports
- Proxy-based weaving (built into Spring AOP)
- AspectJ-based weaving (using AspectJ framework for more powerful weaving)
1. Proxy-Based AOP Example
In Spring, proxy-based weaving creates proxies for your beans at runtime, and advice (like logging) is applied via these proxies.
Step 1: Spring config file (config.xml)
In Spring, proxy-based weaving creates proxies for your beans at runtime, and advice (like logging) is applied via these proxies.
Step 1: Spring config file (config.xml)
<?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:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.aop.aopexample.service" /> <aop:aspectj-autoproxy /> <bean class="com.aop.aopexample.aspect.LoggingAspect" /></beans>
Step 2: Service class
package com.aop.aopexample.service;
import org.springframework.stereotype.Service;
@Servicepublic class MyService { public void doSomething() { System.out.println("Inside doSomethingMethod!!"); }
public void doAnotherThing() { System.out.println("Inside doAnotherThingMethod!!"); }}
Step 3: 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@Componentpublic class LoggingAspect { @Before("execution(* com.aop.aopexample.service.MyService.*(..))") public void printBefore() { System.out.println("Please Signin"); }}
Step 4: Main application
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 service = context.getBean(MyService.class); service.doSomething(); service.doAnotherThing(); }}
Output
Please Signin
Inside doSomethingMethod!!
Please Signin
Inside doAnotherThingMethod!!
2. AspectJ-based AOP Example
For more advanced scenarios, Spring can integrate with AspectJ. We use Java-based configuration instead of XML.
Step 1: Spring config class
For more advanced scenarios, Spring can integrate with AspectJ. We use Java-based configuration instead of XML.
Step 1: Spring config class
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: Service class
package com.aop.aopexample.service;
import org.springframework.stereotype.Service;
@Servicepublic class MyService { public void doSomething() { System.out.println("Inside doSomethingMethod!!"); }
public void doAnotherThing() { System.out.println("Inside doAnotherThingMethod!!"); }}
Step 3: 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@Componentpublic class LoggingAspect { @Before("execution(* com.aop.aopexample.service.MyService.*(..))") public void printBefore() { System.out.println("Please Signin"); }}
Step 4: Main application
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 service = context.getBean(MyService.class); service.doSomething(); service.doAnotherThing(); }}
Output
Please Signin
Inside doSomethingMethod!!
Please Signin
Inside doAnotherThingMethod!!
Key Differences & Points
Proxy-Based | AspectJ-Based | |
---|---|---|
Config | XML (<aop:aspectj-autoproxy>) | Java (@EnableAspectJAutoProxy) |
Weaving | Done at runtime by Spring proxy | Uses AspectJ, can do compile-time, load-time or runtime weaving |
Use case | Simple cross-cutting (logging, transactions) | More advanced scenarios, fine-grained control |
Both achieve same effect here (logging before method execution).
AspectJ is more powerful and supports weaving beyond Spring-managed beans.