Spring拦截指定方法的示例

方式一:在配置文件中使用aop标签配置

<?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:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    					http://code.alibabatech.com/schema/dubbo
    					http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    					http://www.springframework.org/schema/context
       					http://www.springframework.org/schema/context/spring-context-3.2.xsd
       					http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd">
    
  
           <bean id="NaiveWaiter" class="com.uu.anno1.NaiveWaiter"></bean>
           <bean id="asp" class="com.uu.anno1.Asp"></bean>
           <aop:config>
           <aop:aspect ref="asp">
           <aop:pointcut expression="execution(* com.uu.anno1.NaiveWaiter.*(..))" id="pc"/>
           <aop:before method="doo" pointcut-ref="pc"/>
           </aop:aspect>
           </aop:config>
           
</beans>
NaiveWaiter

package com.uu.anno1;

public class NaiveWaiter {
	public void greetTo(String name) {
		System.out.println("greet to " + name + "...");
	}

	public void serveTo(String name) {
		System.out.println("serving " + name + "...");
	}
}
Asp
package com.uu.anno1;

public class Asp {

	public void doo(){
		System.out.println("--doo--");
	}
}
测试代码

package com.uu.anno1;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class U {
public static void main(String[] args) {
	ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-config2.xml");
	NaiveWaiter w = (NaiveWaiter)ac.getBean("NaiveWaiter");
	w.serveTo("999");
}
}
结果

10:58:26.338 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'asp'
--doo--
serving 999...

方式二:使用注解的方式

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:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    					http://code.alibabatech.com/schema/dubbo
    					http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    					http://www.springframework.org/schema/context
       					http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
    <context:component-scan base-package="com.uu"></context:component-scan>
    <!--   
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> 
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
    -->
</beans>
Config2 
下面使用了注解@EnableAspectJAutoProxy

package com.uu.anno;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
public class Config2 {
	@Bean
	public DefaultPointcutAdvisor defaultAdvisor() {
		return new DefaultPointcutAdvisor(new StaticMethodMatcherPointcut() {
			@Override
			public boolean matches(Method method, Class<?> targetClass) {
				System.out.println("------matching------");
				return "say".equals(method.getName());
			}
		},methodBeforeAdvice());
	}
	
	
	@Bean
	public MethodBeforeAdvice methodBeforeAdvice(){
		System.out.println("--methodBeforeAdvice()--");
		return new MethodBeforeAdvice() {
			@Override
			public void before(Method method, Object[] args, Object target)
					throws Throwable {
				System.out.println("--bf--");
			}
		};
	}
}
这里还可以在配置文件里配置下面中的一种
 
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> 
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
是一样的效果
因为DefaultAdvisorAutoProxyCreator和AnnotationAwareAspectJAutoProxyCreator都是BeanPostProcessor,都继承自
AbstractAdvisorAutoProxyCreator,底层通过CGLIB实现动态代理。
同时 @EnableAspectJAutoProxy和上面两者原理是一样的,从下面EnableAspectJAutoProxy的定义可以看到,注解导入了AspectJAutoProxyRegistrar配置类
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
    /**
     * Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
     * to standard Java interface-based proxies. The default is {@code false}.
     */
    boolean proxyTargetClass() default false;
}
Ser
package com.uu.anno;

import org.springframework.stereotype.Service;
@Service
public class Ser {
	@Anno
	public void say() {
		System.out.println("hello world!");
	}
}
测试
package com.uu.anno;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class T {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext xml = new ClassPathXmlApplicationContext(
				"classpath:spring-config.xml");
		Ser ser = (Ser) xml.getBean("ser");
		ser.say();
	}
}

结果

11:21:21.554 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'ser'
------matching------
--bf--
hello world!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值