Java/Spring/Pointcut
All Kinds Of Pointcut
File: context.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="test" class="bean.TestBean2">
<property name="simpleBean" ref="simple"/>
</bean>
<bean id="simple" class="bean.SimpleBean"/>
<aop:config>
<aop:advisor advice-ref="tx-advice"
pointcut="SystemPointcuts.testBeanExecution()"/>
</aop:config>
<bean id="transactionManager" class="NoopTransactionManager"/>
<tx:advice id="tx-advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
File: Main.java
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;
import bean.SimpleBean;
import bean.TestBean2;
public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml");
TestBean2 testBean = (TestBean2) ac.getBean("test");
SimpleBean simpleBean = (SimpleBean) ac.getBean("simple");
testBean.work();
testBean.stop();
simpleBean.sayHello();
simpleBean.x("a", "b");
}
}
final class SystemPointcuts {
private SystemPointcuts() {
}
@Pointcut("execution(* bean..*.*(..)) &&" +
"!execution(* bean..*.set*(..)) &&" +
"!execution(* bean..*.get*())")
public void serviceExecution() { }
@Pointcut("within(bean.TestBean2)")
public void within1() { }
@Pointcut("execution(* bean.TestBean2.*(..))")
public void testBeanExecution() { }
@Pointcut("execution(* bean.TestBean2.*(..))")
private void testBeanExec() { }
@Pointcut("execution(* bean..*.*(..))")
public void inProsringPackage() { }
@Pointcut("within(bean..*)")
private void withinProSpringPackage() { }
@Pointcut("execution(* bean.TestBean2.*(..)) &&" +
"within(bean..*)")
public void same1() { }
@Pointcut("execution(* bean.TestBean2.*(..)) &&" +
"withinProSpringPackage()")
public void same2() { }
@Pointcut("testBeanExec() && withinProSpringPackage()")
public void same3() { }
@Pointcut("within(bean.TestBean2)")
public void fromTestBeanExecution() { }
@Pointcut("this(bean.SimpleBean)")
public void onlyFromSimpleBean() { }
@Pointcut("target(bean.SimpleBean)")
public void onlyToSimpleBean() { }
@Pointcut("args(String, String)")
public void onlyTwoStringArguments() { }
// @Pointcut("bean(test)")
public void beanName() { }
}
class NoopTransactionManager extends AbstractPlatformTransactionManager {
protected Object doGetTransaction() throws TransactionException {
return new Object();
}
protected void doBegin(Object object, TransactionDefinition transactionDefinition) throws TransactionException {
System.out.println("Begin");
}
protected void doCommit(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException {
System.out.println("Commit");
}
protected void doRollback(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException {
System.out.println("Rollback");
}
}
Use NameMatchMethodPointcut
File: Main.java
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
public class Main {
public static void main(String[] args){
MyClass target = new MyClass();
NameMatchMethodPointcut pc = new NameMatchMethodPointcut();
pc.addMethodName("foo");
pc.addMethodName("bar");
Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAdvice());
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);
MyClass proxy = (MyClass)pf.getProxy();
proxy.foo();
proxy.foo(1);
proxy.bar();
proxy.yup();
}
}
class SimpleAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("start: " + invocation.getMethod().getName());
Object retVal = invocation.proceed();
System.out.println("end");
return retVal;
}
}
class MyClass {
public void foo() {
System.out.println("foo");
}
public void foo(int x) {
System.out.println("foo " + x);
}
public void bar() {
System.out.println("bar");
}
public void yup() {
System.out.println("yup");
}
}
Use NameMatchMethodPointcutAdvisor
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="afterBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="MtBean">
<property name="firstName" value="AAA"/>
</bean>
</property>
<property name="interceptorNames">
<list>
<idref bean="endMatchPointcutAdvisor"/>
</list>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
<bean id="endMatchPointcutAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"
singleton="false">
<property name="advice" ref="concurrencyThrottleInterceptor"/>
<property name="mappedName" value="showValues"/>
</bean>
<bean id="concurrencyThrottleInterceptor"
class="org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor"
singleton="false">
<property name="concurrencyLimit" value="5"/>
</bean>
</beans>
File: Main.java
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StopWatch;
public class Main {
public static void main(String[] args) throws Exception {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
MtBean testBean = (MtBean) beanFactory
.getBean("afterBean");
testBean.showValues();
}
}
class MtBean {
private String firstName;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void showValues() {
System.out.println("First name: " + this.firstName);
}
}