如题,最好是xml和注解版的
------解决方案--------------------
spring aop 使用注解版例子
分类: Spring MVC J2EE Spring 2013-11-10 10:58 10人阅读 评论(0) 收藏 编辑 删除
1
applicationContext.xml里面添加
[html] view plaincopy
<aop:aspectj-autoproxy/>
<bean id="anno_advice" class="com.AnnoAdvice"></bean>
2
看如何实现注解的
[html] view plaincopy
package com;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AnnoAdvice {
@Pointcut("execution(* com..*.add*(..))")
public void addAspect()
{
}
@Before(value="addAspect()")
public void before()
{
System.out.println("anno-before");
}
}
3 测试类
[html] view plaincopy
package com.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.UserDao;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/annotation/applicationContext.xml");
UserDao userDao = context.getBean("userDao",UserDao.class);
userDao.add(null);
}
}
4 @afterreturning的用法
[html] view plaincopy
@AfterReturning(pointcut="addAspect()")
public void back()
{
System.out.println("返回");
}
5 @around的用法
[html] view plaincopy
@Around("addAspect()")
public Object deal(ProceedingJoinPoint point)
{
System.out.println("around start");
Object object=null;
try {
object = point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("around end");
return object;
}