Spring的AOP配置以及常见错误(内附代码)

首先我们可以新建一个包,目录结构如下:

User类:

package com.hqj.aop;
public class User {
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

User类接口:

package com.hqj.aop;
public interface UserDao {
    public void save(User user);
}

User类接口类实现:

package com.hqj.aop;
public class UserDaoImpl implements UserDao {
    public void save(User user) {
        System.out.println("User is saved successfully");
    }
}

LogInterceptor类:

public class LogInterceptor {
    public void before() {
        System.out.println("method state");
    }

    public void after() {
        System.out.println("method after");
    }

    public void AfterReturning() {
        System.out.println("method AfterReturning");
    }

    public void AfterThrowing() {
        System.out.println("method AfterThrowing");
    }
}

AOP XML文件:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    ">
    <bean id="user" class="com.hqj.aop.User">
        <property name="username" value="Tim"></property>
        <property name="password" value="123"></property>
    </bean>
    <!-- 配置Bean -->
    <bean id="UserDaoImpl" class="com.hqj.aop.UserDaoImpl"></bean>
    <!-- 配置切面的Bean -->
    <!-- 也就是要记录的数据的地方 (比如错误日志) -->
    <bean id="logInterceptor" class="com.hqj.aop.LogInterceptor"></bean>
    <!-- 配置AOP -->
    <aop:config proxy-target-class="true">
        <!-- 配置切点表达式 -->
        <aop:pointcut expression="execution(* com.hqj.aop.UserDao.save(..))"
            id="servicePointcut" />
        <!-- 配置切面以及通知(指向切面,比如错误日志) -->
        <aop:aspect id="logAspect" ref="logInterceptor">
            <!-- method写切面中(比如错误日志)的方法名称 -->
            <aop:before method="before" pointcut-ref="servicePointcut" />
            <aop:after method="after" pointcut-ref="servicePointcut" />
        </aop:aspect>
    </aop:config>
</beans>

测试MAIN函数:

package com.hqj.aop;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");
        UserDaoImpl UDI = (UserDaoImpl) ctx.getBean("UserDaoImpl");
        User user = (User) ctx.getBean("user");
        UDI.save(user);
    }
}

运行结果:

常见错误:

com.sun.proxy.$Proxy2 cannot be cast to com.hqj.aop.UserDaoImpl
    at com.hqj.aop.Main.main(Main.java:12)

解决办法:

<tx:annotation-driven transaction-manager="transactionManager"
                                       proxy-target-class="true"/>
  注意:proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。
        即使你未声明 proxy-target-class="true" ,但运行类没有继承接口,spring也会自动使用CGLIB代理。高版本spring自动根据运行类选择 JDK 或 CGLIB 代理package com.hqj.aop;

时间: 2024-09-01 08:36:01

Spring的AOP配置以及常见错误(内附代码)的相关文章

CentOS FastDFS配置之常见错误

FastDFS 常见问题 Q:/fdfs_trackerd: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory A:export LD_LIBRARY_PATH=/usr/loal/lib(libevent的安装路径,可以通过find来查找) Q:Command 'ifconfig' is available in '

XmlDocument创建xml,返回的xml没有隔行显示!总是在一行中!高手帮忙,内附代码!!!

问题描述 最近做一个程序,需要动态生成一个xml文档.然后写在textbox中输出.但是无论怎样程序动态创建的xml总是没有隔行显示.而是以一行显示所有xml文档.存入到一个文档中,在用dom.load();返回的innerxml和outerxml都有隔行显示.但是程序动态生成的总是没有,那位高手帮忙解决一下!先谢了!下边是代码,那位高手帮帮忙啊?publicvoidCreateXmlTest(){XmlDocumentxmlDoc=newXmlDocument();//加入XML的声明段落Xm

网页中显示xml怎样加隔行,每个节点按照格式化的节点输出?内附代码

问题描述 <?xmlversion="1.0"encoding="UTF-8"?><root><root1>000</root1></root>例如后端返回如下一个字符串!在网页中显示为<?xmlversion="1.0"encoding="UTF-8"?><root><root>000</root1></root

Hadoop-2.8.0集群搭建、hadoop源码编译和安装、host配置、ssh免密登录、hadoop配置文件中的参数配置参数总结、hadoop集群测试,安装过程中的常见错误

25.集群搭建 25.1 HADOOP集群搭建 25.1.1集群简介 HADOOP集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起 HDFS集群: 负责海量数据的存储,集群中的角色主要有NameNode / DataNode YARN集群: 负责海量数据运算时的资源调度,集群中的角色主要有 ResourceManager /NodeManager 25.1.2服务器准备 本案例使用虚拟机服务器来搭建HADOOP集群,所用软件及版本: ü Vmware 11.

Spring中Aspectj和Schema-based AOP混用引起的错误

         前几天要在项目中增加一个新功能用来监控某些模块的运行情况,自然就想到了使用Spring的AOP来实现.之前已经有类似的AOP代码,使用的是Schema-based形式配置的,也就是在Spring的ApplicationContext.xml中加入了: <bean id="handlerBeanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanName

企业路由器配置常见错误的处理方法

对于企业网络运维人员来说企业路由器配置是最基础的工作, 所以必定要对其有所了解.这里我们就来简单介绍一下.了解一些关于企业路由器配置方面的知识还是很有帮助的,这里主要讲解了企业路由器配置中一般错误的解决办法,通常说来,企业在进行路由器的基本配置时,主要注意的地方就是广域网端.局域网端及公共服务器三个方面.但在实际配置操作中, 往往企业管理员会对企业路由器配置操作.结构原理造成一些认识上的不足,从而导致因企业路由器配置不善而带来整个网络性能的瓶颈.在本文中,就一些较常见的对路由器认识.企业路由器配

Spring新特性的AOP配置头去那里找呢??

问题描述 spring2-3新特性aop配置,要求beas头的配置要改这个头要去那里找?支持那些版本呢??给个地址呗? 解决方案 解决方案二:以spring2.5为例:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/

关于spring自定义标签配置错误

问题描述 关于spring自定义标签配置错误 解决方案 解决方案二: 解决方案三: 解决方案四: 解决方案五: 希望大婶们帮帮忙,已经困扰我两天了.. 解决方案六: spring自定义标签 解决方案七: 解决方案八: spring.handler里配置了吗?

spring的aop是否和struts2.xml里的param配置冲突

问题描述 spring的aop是否和struts2.xml里的param配置冲突 用aop控制权限,然后把struts2所有action用spring代理创建,action里有个pagesize属性,有getset方法,在struts2里配置了一个action,其中用param 给pagesize一个默认值15,aop里是如果没权限就response跳转了,但是实际情况却是每次调那个action,都会先去执行两次getPageSize(),然后第二次response跳转就报java.lang.I