spring-aop-aspectj(Schema)-case

基于Schema 配置切面:
1、切点定义的语言依然是AspectJ表达式语言
2、把不用AspectJ相关的注解定义切面,而是通过xml配置,相当于注解的xml表达的aop定义形式。

[b]定义advice增强[/b]

public class LoginMonitorAspectj {
public static final ThreadLocal<Long> time = new ThreadLocal<Long>();

public void before(JoinPoint pjp) {
time.set(System.currentTimeMillis());
System.out.println("invoke before --------------------------------");
System.out.println(pjp.toLongString());
System.out.println(Arrays.toString(pjp.getArgs()));
}
public void afterReturning(JoinPoint pjp,User user) {
System.out.println("invoke afterReturning ---------------------------------");
System.out.println(user.toString());
System.out.println("times : "+(System.currentTimeMillis() - time.get()));
}
public void _exception(JoinPoint pjp,Exception ex) {
System.out.println("invoke _exception ---------------------------------");
System.out.println("times : "+(System.currentTimeMillis() - time.get()));
}
}



[b]业务方法定义[/b]

@Service
public class LoginService implements ILoginService {

public User login(String username, String password) {
User user = new User();
System.out.println("in login method");
user.setPassword("123456");
user.setUsername("admin");
return user;
}

@MethodExecuteTimes
public void regist(String username, String password, String email, int age) {
System.out.println(Arrays.toString(new Object[]{username,password,age}));
}

}

[b]
配置applicationContext.xml[/b]

<context:component-scan base-package="org.job">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<aop:aspectj-autoproxy/>

<bean id="adviceMethods" class="org.job.user.aop.LoginMonitorAspectj"/>

<aop:config>
<aop:aspect ref="adviceMethods">
<aop:before pointcut="execution(* login(..))" method="before"/>
<aop:after-returning pointcut="execution(* login(..))" method="afterReturning" returning="user"/>
<aop:after-throwing pointcut="execution(* login(..))" method="_exception" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>


详细见附件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值