`
pengchua
  • 浏览: 150672 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Spring 2.0中 AOP的编程

阅读更多
 
Spring 2.0 AOP的编程:
方式一:
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>
这样配置文件就就只有一个<aop:aspectj-autoproxy />很简单。
出现的问题解决:
问题1Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.util.ClassUtils.<clinit>(ClassUtils.java:67)   at org.springframework.core.io.DefaultResourceLoader.<init>(DefaultResourceLoader.java:52) at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:184) at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:80) at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:58)   at
需要加上:commons-logging.jar log4j-1.2.11.jar
 
问题2Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [text.xml]; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
    at java.lang.Class.forName0(Native Method)
 
需要加上:aspectjweaver.jar
 
问题3Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logBean' defined in class path resource [text.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
Caused by: org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
需要加上:cglib-2.1.3.jar
 
 
问题4xception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logBean' defined in class path resource [text.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type
Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/Type
    at net.sf.cglib.core.TypeUtils.parseType(TypeUtils.java:180)
需要加上:asm.jar
 
 

分享到:
评论
6 楼 x492016665 2012-08-21  
非常感谢楼主.
5 楼 wwjjkk 2010-11-01  
调了半天,不知道错在哪里,原来是少jar包,太感谢了
4 楼 icoo1985 2009-06-15  
太感谢楼主了,在网上搜索的半天,终于在您这里看到答案了,问题解决了
3 楼 lujiawu12 2008-12-12  
不错  
楼主的精神可嘉!
2 楼 lkjust08 2008-11-19  
问题3:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logBean' defined in class path resource [text.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
Caused by: org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
需要加上:cglib-2.1.3.jar


LZ能否说一下这个错误的原因???
1 楼 fuyou0104 2008-01-29  
天天让人投入

相关推荐

    Spring2.0宝典 源码

    全书分22章,内容涵盖了Spring的核心机制、依赖注入、资源访问、AOP框架、事务框架、整合Hibernate、DAO支持、JDBC支持、MVC框架、整合第三方表现层技术、整合第三方MVC框架、远程访问支持、EJB访问和实现、Spring对...

    Spring 2.0 开发参考手册

    6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个...

    spring-aop-2.0.jar

    spring-aop.jar 各个版本,免费下载 Spring 的面向切面编程,提供 AOP(面向切面编程)的实现。 如果下载不了,关注我,评论区联系我。

    struts2.1.6+spring2.0+hibernate3.2常用配置包

    另外三大框架的添加顺序也需要注意下,我的添加顺序是 struts2.1.6、spring2.0、hibernate3.2 spring版本差异(包方面)没有研究过,大致雷同,也应该保持版本一致,即在同一个稳定发行包中下载的包。 以下包后面跟...

    spring.net中文手册在线版

    17.6.2.在.NET 2.0中执行回调 17.6.3. .NET 1.1 17.6.4.AdoTemplate方法指南 17.7.异常翻译 17.8.参数管理 17.8.1. IDbParametersBuilder 17.8.2. IDbParameters 17.9. Mapping DBNull values 17.10. Basic data ...

    Springframework开发参考手册chm格式

    chm,一份对Spring特性的参考指南,内容涵盖Spring概述、使用场景、Spring2.0新特性、面向切面编程、中间层、WEB层、校验,数据绑定,BeanWrapper,与属性编辑器、使用Spring进行面向切面编程(AOP 资源太大,传百度...

    ssh整合出现的一些问题的解决

    Ignoring namespace handler [org.springframework.scripting.config.LangNamespaceHandler]: handler class not found ...Spring2.0 Hibernate3.1.x/Hibernate3.2 在使用Spring的AOP编程时,会用到这几个lib:

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....

    Spring Framewor开发手册

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. ...

    spring2.5.chm帮助文档(中文版)

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    SpringAll_wuyouzhuguli.tar.gz

    Spring Boot 2.0 WebFlux编程 Spring Boot WebFlux增删改查样例 二、Spring Boot & Shiro教程 Spring Boot Shiro用户认证 Spring Boot Shiro Remember Me Spring Boot Shiro权限控制 Spring Boot Shiro Redis Spring...

    spring chm文档

    6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个...

    Spring中文帮助文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    Spring Framework 开发参考 chm 手册.rar

    chm 手册,一份对Spring特性的参考指南,内容涵盖Spring概述、使用场景、Spring2.0新特性、面向切面编程、中间层、WEB层、校验,数据绑定,BeanWrapper,与属性编辑器、使用Spring进行面向切面编程(AOP)、使用ORM...

    Spring API

    6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个切入点...

    Spring3+Hibernate4+Struts2 jar包 SSH框架

    spring-aop-3.2.0.RC2.jar spring-aspects-3.2.0.RC2.jar spring-beans-3.2.0.RC2.jar spring-context-3.2.0.RC2.jar spring-context-support-3.2.0.RC2.jar spring-core-3.2.0.RC2.jar spring-expression-3.2.0.RC2...

    第24次课-1 Spring与Hibernate的整合

    无论是编程式事务还是声明式事务,Spring都提供一致的编程模型。 24.3 Spring对Hibernate的简化 24.3.2 简化的具体表现 Spring对Hibernate的简化包括: 统一的异常处理机制。不再强制开发者在持久层捕捉异常,持久层...

    spring发展史与优势

    Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 SpringMVC 和持久层 Spring JDBC ...

    javaeye热点阅读

    1.17 2 Spring2.0用注解实现事务管理 1.18 Struts2教程2:处理一个form多个submit 2. Ruby 2.1 Ruby on Rails环境下工作流的实现 2.2 让Ruby On Rails走进企业开发 2.3 让Ruby On Rails走进嵌入式开发2.4 用Ruby...

    Spring3+Hibernate4+Struts2 jar包

    驱动器 D 中的卷没有标签。 卷的序列号是 0004-19C1 D:\spring 3.2.0+hibernate 4.1.6+struts2整合需要的jar包 的目录 2013/05/11 15:26 &lt;DIR&gt; . 2013/05/11 15:26 &lt;DIR&gt; .. 2013/03/21 18:20 445,288 antlr-...

Global site tag (gtag.js) - Google Analytics