Spring AOP — Pointcut | Code Factory

Spring Tutorial Index Page: Link

  • We use Pointcut class to apply condition.
  • To implement Pointcut we have 2 classes.
    - StaticMethodMatcherPointcut
    - NameMetchMethodPointcut
  • * We can add target in AOP using setTarget() method and service using setInterceptorNames() method. In case if we have Pointcut then it is not possible to add Service and Pointcut both in AOP. Then we need to add Service and Pointcut to Advisor and then add Advisor into an AOP. Advisor is combination of Pointcut and Advice.
  • We have 2 Advisor
    - DefaultPointcutAdvisor
    - RegexpMethodPointcutAdvisor
  • StaticMethodMatcherPointcut Example

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Other… -> Java Project
  • Create AOP-Programmatic6 project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    - aopalliance.jar
    - aspectjrt.jar
    - aspectjweaver.jar
    - cglib-nodep-X.X.jar
    - commons-logging-X.X.jar
    - spring-aop-X.X.X.jar
    - spring-beans-X.X.X.jar
    - spring-context-X.X.X.jar
    - spring-core-X.X.X.jar
    - aspectjlib-X.X.X.jar
    - spring-aspects-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- target -->
<bean id="b" class="com.codeFactory.business.Bank" />
<!-- service -->
<bean id="lbs" class="com.codeFactory.service.LogBeforeService" />
<!-- pointcut -->
<bean id="dp" class="com.codeFactory.service.DepositPointcut" />

<!-- advisor -->
<bean id="dpa" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="lbs"></property>
<property name="pointcut" ref="dp"></property>
</bean>

<!-- proxy -->
<bean id="pfb" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="b" />
<property name="interceptorNames">
<list>
<value>dpa</value>
</list>
</property>
</bean>

</beans>
package com.codeFactory.service;import java.lang.reflect.Method;import org.springframework.aop.support.StaticMethodMatcherPointcut;/**
* @author code.factory
*
*/
public class DepositPointcut extends StaticMethodMatcherPointcut {
/* When deposit method is called then service will run.
* For O/P check console */
@Override
public boolean matches(Method m, Class c) {
return m.getName().equals("deposit")? true : false;
}
}
package com.codeFactory.service;import java.lang.reflect.Method;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
import com.codeFactory.business.Bank;/**
* @author code.factory
*
*/
public class LogBeforeService implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object object) throws Throwable {
Log l = LogFactory.getLog(Bank.class);
l.info("Deposit method");
}
}
package com.codeFactory.business;/**
* @author code.factory
*
*/
public class Bank {
private int amount = 1000;
private String accNo = "SBI12345";

public int deposit(int amount, String accNo) {
System.out.println("Bank.deposit()");
if(accNo.equals(this.accNo)) {
this.amount += amount;
} else {
throw new AccnoNotFoundException();
}
return this.amount;
}

public int findBalance(String accNo) {
System.out.println("Bank.findBalance()");
if(accNo.equals(this.accNo)) {
return this.amount;
} else {
throw new AccnoNotFoundException();
}
}
}
package com.codeFactory.business;/**
* @author code.factory
*
*/
public class AccnoNotFoundException extends RuntimeException {
@Override
public String toString() {
return "AccNo invalid";
}
}
package com.codeFactory.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.business.Bank;/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
Bank bankProxy = (Bank) context.getBean("pfb");

/* uncomment below code to run service */
/*int amt = bankProxy.deposit(150, "SBI12345");
System.out.println(amt);*/

System.out.println(bankProxy.findBalance("SBI12345"));
}
}
Bank.findBalance()
1000

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to handle Enums in Laravel without external packages

Performance benchmarking of a Airship TreasureMap Setup

How to install ODOO on AWS?

YSL.IO 💎- Win $6000 and Double your sYSL!

The Mythical Rose Month

Why you need to move from Bitbucket to Github

Setup Rancher 2x on AWS EC2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Three Deployment Patterns

An Overview Of Enterprise Application Integration (EAI)

Spring MVC and Architecture

Rest-Assured — API Testing