静态正则表达式方法匹配切面

/ Spring / 没有评论 / 312浏览

在上一篇中我们重写StaticMethodMatcherPointcutAdvisor类的matches方法,通过方法名定义了切点,但在这种方式创建的切点有很大的弊端,不方便。因为如果目标类中有多个方法时,就不太适用了。spring为了更方便我们使用切面,于是提供了支持正则表达式的实现类RegexpMethodPointcutAdvisor类,因为该类支持正则表达式,基本可以满足我们匹配目标方法的所有规则,所以我们在使用此类时,基本不用重写此类。下面我们我们看一下通过RegexpMethodPointcutAdvisor类可以实现我们上一篇中的需求。

public class UserService{

public void login(String username, String password) {
System.out.println(String.format("用户登陆: username: %s password: %s", username, password));
}

public void register(String username, String nickName, String password) {
System.out.println(String.format("用户注册: username: %s nickName: %s password: %s", username, nickName, password));
}

}
public class UserServiceBeforeAdvice implements MethodBeforeAdvice{

@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(String.format("spring增强: %s", method.getName()));
TransactionManager.beginTransaction();
}
}
<bean id="userServiceTarget" class="com.jilinwula.spring.aop.UserService"/>
<bean id="userServiceBeforeAdvice" class="com.jilinwula.spring.aop.UserServiceBeforeAdvice"/>
<bean id="regexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
p:advice-ref="userServiceBeforeAdvice">
<property name="patterns">
<list>
<value>.*reg.*</value>
</list>
</property>
</bean>
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="regexpMethodPointcutAdvisor"
p:target-ref="userServiceTarget"
p:proxyTargetClass="true"/>
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.register("admin","吉林乌拉","jilinwula");
userService.login("admin","jilinwula");
}
spring增强: register
开启事物
用户注册: username: admin nickName: 吉林乌拉 password: jilinwula
用户登陆: username: admin password: jilinwula

这时我们看输出只增强了register方法,而login方法则没有增强,如果我们想要增强login方法则可以采用以下配置:

<bean id="userServiceTarget" class="com.jilinwula.spring.aop.UserService"/>
<bean id="userServiceBeforeAdvice" class="com.jilinwula.spring.aop.UserServiceBeforeAdvice"/>
<bean id="regexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
p:advice-ref="userServiceBeforeAdvice">
<property name="patterns">
<list>
<value>.*reg.*</value>
<value>.*log.*</value>
</list>
</property>
</bean>
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="regexpMethodPointcutAdvisor"
p:target-ref="userServiceTarget"
p:proxyTargetClass="true"/>
spring增强: register
开启事物
用户注册: username: admin nickName: 吉林乌拉 password: jilinwula
spring增强: login
开启事物
用户登陆: username: admin password: jilinwula

上述功能还可以采用另一种方式配置:

<bean id="userServiceTarget" class="com.jilinwula.spring.aop.UserService"/>
<bean id="userServiceBeforeAdvice" class="com.jilinwula.spring.aop.UserServiceBeforeAdvice"/>
<bean id="regexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
p:advice-ref="userServiceBeforeAdvice">
<property name="patterns">
<list>
<value>.*</value>
</list>
</property>
</bean>
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="regexpMethodPointcutAdvisor"
p:target-ref="userServiceTarget"
p:proxyTargetClass="true"/>
spring增强: register
开启事物
用户注册: username: admin nickName: 吉林乌拉 password: jilinwula
spring增强: login
开启事物
用户登陆: username: admin password: jilinwula