spring aop的annotation配置

3 篇文章 0 订阅
1 篇文章 0 订阅
首先在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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值