首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring2.0 AOP举例来说

2012-10-10 
spring2.0 AOP举例spring 2.0 aop 配置----解决 CGLIB2 is not availa(2008-11-06 10:22:03)转载标签:杂谈

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" />
    <bean id="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 />
    
    -->
    <bean id="logAspect" />
    <bean id="user" />

</beans>

热点排行