spring学习笔记(14)--AOP Annotation

1.LogInterceptor.java

package org.sh.spring.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogInterceptor {
	@Before("execution(public * org.sh.spring.impl..*.*(..))")
	public void before() {
		System.out.println("method start...");
	}
	
	@After("execution(public * org.sh.spring.impl..*.*(..))")
	public void after() {
		System.out.println("method start...");
	}
}


execution(public * org.sh.spring.impl..*.*(..))

表示 org.sh.spring.impl中包括子包中的任何类的任何返回类型的public型方法

2.测试:

@Test
	public void testSave() {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		IUserDAO ud = (UserServices)ctx.getBean("userservice");
		User u = new User();
		ud.save(u);
	}

测试结果:
method start...
user saved
method done...

3.用@PointCut来实现

LogInterceptor.java

package org.sh.spring.aop;

import org.aspectj.lang.annotation.After;
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 LogInterceptor {
	@Pointcut("execution(public * org.sh.spring.impl..*.*(..))")
	public void myMethod(){
		
	}
	@Before("myMethod()")
	public void before() {
		System.out.println("method start...");
	}
	
	@After("myMethod()")
	public void after() {
		System.out.println("method start...");
	}
}
相当于用一个方法的名字来命名

@AfterThrowing

@AfterThrowing("myMethod()")
	public void afterThrowing() {
		System.out.println("method Throwing...");
	}
测试结果

method start...
user saved
method done...

method Throwing...

@Around

@Around("myMethod()")
	public void around(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("method around start...");
		pjp.proceed();
		System.out.println("method around end...");
	}

测试结果:

method start...
method around start...
user saved
method done...
method Throwing...

遇到的问题

在将org.sh.spring.impl改为org.sh.spring.services时,程序报错:

java.lang.ClassCastException: $Proxy13 cannot be cast to org.sh.spring.Services.UserServices

原因:

因为我的类是实现了接口的 所以在实例化是一定要用父接口才行将

@Test
	public void testSave() {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		UserServices = (UserServices)ctx.getBean("userservice");
		User u = new User();
		ud.save(u);
	}

改为:

@Test
	public void testSave() {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		IUserDAO ud = (IUserDAO)ctx.getBean("userservice");
		User u = new User();
		ud.save(u);
	}

再进行测试就正确了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值