spring学习----spring注解实现AOP
Spring----- 注解方法使用 AOP
先建立项目 , 在 spring 的 xml 文件中加入 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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="myInterceptor" class="com.liyu.service.MyInterceptor"></bean>
<bean id="personServiceBean" class="com.liyu.service.impl.PersonServiceBean"></bean>
</beans>
再定一个一接口 ,
一个这个接口的实现 .
再就是写这个 AOP 类了 .
package com.liyu.service;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 切面
*
*/
@Aspect
public class MyInterceptor {
@Pointcut("execution (* com.liyu.service.impl.PersonServiceBean.*(..))")
private void anyMethod() {}//声明一个切入点
@Before("anyMethod() && args(name)")
public void doAccsCheck(String name) {
System.out.println("前置通知:"+name);
}
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doAfterReturning(String result) {
System.out.println("后置通知:"+result);
}
@After("anyMethod()")
public void doAfter() {
System.out.println("最终通知:");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:"+e);
}
@Around(("anyMethod()"))
//环绕通知
public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{
System.out.println("进入方法");
Object result=pjp.proceed();
System.out.println("退出方法");
return result;
}
}
测试类:
package com.liyu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.liyu.service.PersonService;
public class AOPtest {
public static void main(String[] args) {
ApplicationContext ctx= new ClassPathXmlApplicationContext("beans.xml");
//MyInterceptor mi=(MyInterceptor)ctx.getBean("myInterceptor");
PersonService ps=(PersonService)ctx.getBean("personServiceBean");
ps.save("sss");
}
}
运行结果 :
2010-6-15 8:27:27 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@147ee05: display name [org.springframework.context.support.ClassPathXmlApplicationContext@147ee05]; startup date [Tue Jun 15 08:27:27 CST 2010]; root of context hierarchy
2010-6-15 8:27:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-15 8:27:28 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@147ee05]: org.springframework.beans.factory.support.DefaultListableBeanFactory@273686
2010-6-15 8:27:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@273686: defining beans [org.springframework.aop.config.internalAutoProxyCreator,myInterceptor,personServiceBean]; root of factory hierarchy
前置通知:sss
进入方法
我是save()方法.
后置通知:null
最终通知:
退出方法
我在这里做练习时出错了
PersonService ps=(PersonService)ctx.getBean("personServiceBean");
这里一定要使用的是那个接口 .
原文地址:http://www.c119.cn/home.php?mod=space&uid=174&do=blog&id=348