🚀 Want to Master Java & Spring?
Join our community for the best coding tutorials!
🔴 SUBSCRIBE TO RAM N JAVAAdvanced Spring AOP: Around Advice with Regex
Welcome to this beginner-friendly guide! Today, we are exploring how to use Spring AOP to target specific methods across multiple classes using regular expressions. This is a powerful way to keep your code clean and organized.
What is BeanNameAutoProxyCreator?
In many Spring applications, you want to apply logic (advice) to many beans at once. Instead of creating a proxy for each bean manually, the BeanNameAutoProxyCreator does the work for you. You just tell it which bean names to look for!
Setting Up the Project
In this example, we have two main services:
- EmployeeService: Manages employee data like name, age, and address.
- CustomerService: Manages customer data like name, age, and address.
We also have our AroundAdvice class, which implements the MethodInterceptor interface. This class contains the logic that will run before and after our methods.
Configuring the Advisor with Regex
We use the RegexpMethodPointcutAdvisor to define exactly which methods should be intercepted. By using a pattern like .*age.*, we can tell Spring to only apply our advice to methods that have the word "age" in them, such as getEmployeeAge and getCustomerAge.
How the Magic Happens
In the XML configuration, we link everything together. The BeanNameAutoProxyCreator identifies classes ending in "Service", and the RegexpMethodPointcutAdvisor filters those classes for specific method names. When you run the app, you'll see the advice executing only for the age-related methods!
🎥 Recommended Tutorials from Ram N Java
Check out these other videos to continue your learning journey:
Click here to watch on Youtube:
https://www.youtube.com/watch?v=MEORCeIwrNY&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| How to apply AroundAdvice using RegexpMethodPointcutAdvisor and BeanNameAutoProxyCreator |
| How to apply AroundAdvice using RegexpMethodPointcutAdvisor and BeanNameAutoProxyCreator |
| How to apply AroundAdvice using RegexpMethodPointcutAdvisor and BeanNameAutoProxyCreator |
| How to apply AroundAdvice using RegexpMethodPointcutAdvisor and BeanNameAutoProxyCreator |
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ram.core</groupId>
<artifactId>SpringDemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringDemo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring 5 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
EmployeeService.java
public class EmployeeService
{
public String getEmployeeName()
{
System.out.println("Get Employee Name");
return "Dave";
}
public int getEmployeeAge()
{
System.out.println("Get Employee Age");
return 28;
}
public String getEmployeeAddress()
{
System.out.println("Get Employee Address");
return "A111, East Street";
}
}
CustomerService.java
public class CustomerService
{
public String getCustomerName()
{
System.out.println("Get Customer Name");
return "Peter";
}
public int getCustomerAge()
{
System.out.println("Get Customer Age");
return 38;
}
public String getCustomerAddress()
{
System.out.println("Get Customer Address");
return "B111, North Street";
}
}
AroundAdvice.java
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor
{
public Object invoke(MethodInvocation mi) throws Throwable
{
Object obj;
System.out.println("AroundAdvice applied before actual method = "
+ mi.getMethod().getName());
obj = mi.proceed();
System.out.println("obj = " + obj);
System.out.println("AroundAdvice applied after actual method = "
+ mi.getMethod().getName());
return obj;
}
}
applicationContext.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="employeeService" class="com.ram.service.EmployeeService"></bean>
<bean id="customerService" class="com.ram.service.CustomerService"></bean>
<bean id="aroundAdvice" class="com.ram.Advice.AroundAdvice"></bean>
<bean id="employeeAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>.*Age.*</value>
</list>
</property>
<property name="advice" ref="aroundAdvice" />
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>employeeAdvisor</value>
</list>
</property>
</bean>
</beans>
App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ram.service.CustomerService;
import com.ram.service.EmployeeService;
public class App
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println("---------------------------------------");
EmployeeService employeeService = context
.getBean("employeeService", EmployeeService.class);
employeeService.getEmployeeName();
System.out.println("----------------------------------------");
employeeService.getEmployeeAge();
System.out.println("----------------------------------------");
employeeService.getEmployeeAddress();
CustomerService customerService = context
.getBean("customerService", CustomerService.class);
System.out.println("----------------------------------------");
customerService.getCustomerName();
System.out.println("----------------------------------------");
customerService.getCustomerAge();
System.out.println("----------------------------------------");
customerService.getCustomerAddress();
}
}
Output
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@736e9adb: startup date [Mon Oct 29 09:00:07 IST 2018]; root of context hierarchy
Oct 29, 2018 9:00:08 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
---------------------------------------
Get Employee Name
----------------------------------------
AroundAdvice applied before actual method = getEmployeeAge
Get Employee Age
obj = 28
AroundAdvice applied after actual method = getEmployeeAge
----------------------------------------
Get Employee Address
----------------------------------------
Get Customer Name
----------------------------------------
AroundAdvice applied before actual method = getCustomerAge
Get Customer Age
obj = 38
AroundAdvice applied after actual method = getCustomerAge
----------------------------------------
Get Customer Address
Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/SpringDemo_BeanNameAutoProxyCreator.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/javaee/tree/master/Spring_2018/SpringDemo_BeanNameAutoProxyCreator
Bitbucket Link:
https://bitbucket.org/ramram43210/spring/src/7d2a171064f15a9cee482e820ec0271fa30b23b8/Spring_2018/SpringDemo_BeanNameAutoProxyCreator/?at=master
See also:
No comments:
Post a Comment