spring2.0 AOP举例

spring 2.0 aop 配置----解决 CGLIB2 is not availa(2008-11-06 10:22:03)转载标签:杂谈 分类:Java

Spring 2.0 aop 配置----解决 Cannot proxy target class because CGLIB2 is not available.
在lib中添加cglib-nodep-2.1_3.jar 即可!



Spring 2.0中 AOP的编程:
nested exception:该嵌套异常往往是导入包是嵌套造成的,将包remove后再重新导入
方式一:
publicclass User {
publicvoid method() {
System.out.println("in method1");
}
}
publicclass LogBean {
public Object aroundLogCalls(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("before invoke method:"
+ joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("after invoke method:"
+ joinPoint.getSignature().getName());
return object;
}

}
采用在xml配置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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注意上面的四个地址用空格分开 -->
<aop:config>
<!-- expression 表示要执行的匹配表达式,这里匹配所有的public方法,但是去除logger类的所有方法,防止无限调用-->

<aop:pointcut id="loggableCalls"
expression="execution(public * *(..)) "/>


<aop:aspect id="logAspect" ref="logBean">
<aop:around pointcut-ref="loggableCalls"
method="aroundLogCalls" />
</aop:aspect>

</aop:config>
<bean id="logBean" class="LogBean" />
<bean id="user" class="User" />
方式二:
采用标注:
@Aspect
publicclass LogAspect {

@Pointcut("execution(public * *(..))")
publicvoid publicMethods() {
}
@Around("publicMethods()")
public Object aroundLogCalls(ProceedingJoinPoint joinPoint)
throws Throwable {
System.out.println("before invoke method:"
+ joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("after invoke method:"
+ joinPoint.getSignature().getName());
return object;
}
}
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注意上面的四个地址用空格分开 -->

<aop:aspectj-autoproxy />

<!-- 或者使用以下定义

<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

-->
<bean id="logAspect" class="LogAspect" />
<bean id="user" class="User" />

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值