首先在applicationContext.xml启动@AspectJ的支持,即添加:
<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;
}
}
分享到:
相关推荐
Spring_Annotation_AOP
NULL 博文链接:https://bijian1013.iteye.com/blog/2142528
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
NULL 博文链接:https://tianhei.iteye.com/blog/978969
自己动手做的一个spring aop 的小demo,主要是annotation标签的配置联系,比较简单.
aop入门。
@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: ...
NULL 博文链接:https://tuoxinquyu.iteye.com/blog/1465187
spring-aop-4.0.4.RELEASE.jar com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aspects-4.1.2.RELEASE.jar ...
spring-aop实践项目,基于annotation方式的面向切面实践小项目
在Spring AOP代理过的对象上支持dubbo annotation配置事物失效问题-附件资源
详细介绍spring aop中9种切入点表达式的写法execute、within、this、target、args、@target、@within、@annotation、@args
基于Log4j+SpringAOP+Annotation的可注解日志切面组件 使用方式 @Log(logAfter = true, logBefore = true) public returntype methodName(params) 测试类com.liam.aop.aspect.test.AopLogAnnotationTest 测试输出 ...
NULL 博文链接:https://dyygusi.iteye.com/blog/1995858
在微服务流行的当下,在使用SpringCloud/Springboot框架开发中,AOP使用的非常广泛,尤其是@Aspect注解方式当属最流行的,不止功能强大,性能也很优秀,还很舒心!所以本系列就结合案例详细介绍@Aspect方式的切面的...