Spring AOP切面实现:示例

来源:互联网 发布:淘宝怎么搜有图评价 编辑:程序博客网 时间:2024/04/28 14:08

引入jar包:

aspectjrt-1.7.4.jar

aspectjweaver.1.7.1.jar

aopalliance-1.0.jar

下载地址:

http://mvnrepository.com/artifact/org.aspectj/aspectjrt

http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsdhttp://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"><context:annotation-config /><!-- spring扫描注解的配置 --><context:component-scan base-package="com.learn" /><aop:aspectj-autoproxy/><bean id="Learn" class="com.learn.spring.Learn" /></beans>

注意添加:

xmlns:aop="http://www.springframework.org/schema/aop"http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

切面类:

package com.learn.aspect;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Aspect@Componentpublic class LearnAspect {@Pointcut("execution(public * com.learn.spring..*.*(..))")public void pointcut(){}@Around("pointcut()")public Object around(ProceedingJoinPoint jp) throws Throwable{System.out.println("around");return jp.proceed();}@Before("pointcut()")public void before(){System.out.println("before");}}
java类:

package com.learn.spring;public class Learn {public void show(){System.out.println("Now is ShowTime");}}
测试类:

package com.learn.aspect;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.learn.spring.Learn;public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Learn l = (Learn)context.getBean("Learn");l.show();}}
输出结果:

aroundbeforeNow is ShowTime

问题:

1、报错:java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor

解决:缺少aopalliance-1.0.jar包

2、报错:java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut addHander

原因:JDK不匹配。如:用JDK1.7,导入aspectjrt.1.6和apectjweaver.1.6 jar包,就会报错。

解决:更换jar包



0 0
原创粉丝点击