spring aop的annotation配置 博客分类: springaopannotation springaopannotation
程序员文章站
2024-03-22 16:27:40
...
首先在applicationContext.xml启动@AspectJ的支持,即添加:
然后Advice所使用的Bean需要在配置文件定义,添加后如下:
ServiceAdvice.java内容如下:
<aop:aspectj-autoproxy/>
然后Advice所使用的Bean需要在配置文件定义,添加后如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.apache.derby.jdbc.EmbeddedDriver</value> </property> <property name="url"> <value>jdbc:derby:f:/zwh/mydb2</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.DerbyDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="annotatedClasses"> <list> <value>zwh.ssh.maven.po.User</value> </list> </property> </bean> <context:component-scan base-package="zwh.ssh.maven"></context:component-scan> <bean class="zwh.ssh.maven.aop.ServiceAdvice"></bean> <aop:aspectj-autoproxy/> </beans>
ServiceAdvice.java内容如下:
@Aspect public class ServiceAdvice { static Logger log = Logger.getLogger(ServiceAdvice.class); @Before("execution(* zwh.ssh.maven.service.impl.*.*(..))") public void before(JoinPoint jp) { log.info("before:" + "在方法" + jp.getSignature().getName() + "之前执行"); log.info("before:" + "方法的参数" + Arrays.toString(jp.getArgs())); log.info("before:" + "方法的目标对象" + jp.getTarget()); } @AfterReturning(returning="rvt", pointcut="execution(* zwh.ssh.maven.service.impl.*.*(..))") public void afterReturning(JoinPoint jp, Object rvt) { log.info("afterReturning:" + jp.getSignature().getName() + "方法返回" + rvt); } @AfterThrowing(throwing="ex", pointcut="execution(* zwh.ssh.maven.service.impl.*.*(..))") public void afterThrowing(JoinPoint jp, Throwable ex) { log.info("afterThrowing:" + jp.getSignature().getName() + "抛出异常" + ex); } @After("execution(* zwh.ssh.maven.service.impl.*.*(..))") public void after() { log.info("after:" + "在方法之后执行"); } @Around("execution(* zwh.ssh.maven.service.impl.*.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { log.info("around:" + "在方法" + pjp.getSignature().getName() + "之前执行"); Object[] args = pjp.getArgs(); log.info("around:" + "方法的参数" + Arrays.toString(args)); Object rvt = pjp.proceed(args); log.info("around:" + "方法" + pjp.getSignature().getName() + "的返回值" + rvt); return rvt; } }
上一篇: [leetcode]20.Valid Parentheses
下一篇: Jquery validate 对于多个同名控件的处理 博客分类: javascriptweb Jqueryvalidate
推荐阅读
-
spring aop的annotation配置 博客分类: springaopannotation springaopannotation
-
Spring 4.0.6+Quartz-2.2.1 定时器的配置 博客分类: javaspring JavaspringQuartz定时任务
-
spring-mvc.xml的配置 博客分类: SSH框架 springmvcxml
-
spring 3.0 aop jar包配置 博客分类: spring sping3jar
-
spring 声明式事务管理与oracle的inactive会话。 博客分类: 我的文章 OracleSpring配置管理SQLDAO
-
ActiveMQ4.1 +Spring2.0的POJO JMS方案(下)[整理版] 博客分类: ActiveMQ专栏 JMSActiveMQSpringBean配置管理
-
基于SCHEMA的AOP配置 博客分类: 原创 AOPXML
-
spring aop的原理 博客分类: Java AOPSpringBean正则表达式配置管理
-
Spring Aop(十六)——编程式的自定义Advisor 博客分类: Spring SpringAopAdvisor编程自定义
-
Spring读取properties属性文件配置项遇到的问题 博客分类: Java-Spring spring