首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring学习-spring引语实现AOP

2012-07-01 
spring学习----spring注解实现AOPSpring-----注解方法使用AOP先建立项目,在spring的xml文件中加入spring A

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" name="code">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;/** * 切面 * */@Aspectpublic 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 hierarchy2010-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@2736862010-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

热点排行