Spring AOP切面实现:示例

引入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/beans	
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/task
		http://www.springframework.org/schema/task/spring-task-3.0.xsd
		http://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
@Component
public 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();
	}
}
输出结果:

around
before
Now 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包



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值