码迷,mamicode.com
首页 > Web开发 > 详细

The prefix "aop" for element "aop:aspectj-autoproxy" is not bound.

时间:2016-08-01 00:25:18      阅读:1583      评论:0      收藏:0      [点我收藏+]

标签:

       在使用spring  aop的时候报错了:The prefix "aop" for element "aop:aspectj-autoproxy" is not bound.原因是有一些地址没有被引入:

     技术分享

     以下是一个完整配置AOP的例子。

     首先是文件项目结构:

技术分享

SpringAOPTest.java

import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAOPTest {
    @Test public void interceptorTest(){
        ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext-Test.xml");
        UserService userService = (UserService)cxt.getBean("userService");
        List list =new ArrayList();
        list =userService.queryUserList();
        System.out.println("sd:"+list.size());
    }
}

applicationContext-Test.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
     
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    
    <!-- 通过配置的方式,定义bean  -->
    <bean id="userService" class="test.UserServiceImpl"></bean>

    <!-- 启用@AspectJ风格的切面声明 --> 
    <aop:aspectj-autoproxy/> 
    
    <!-- 通过配置的方式,定义日志切面bean  -->
    <bean id="aspetbean" class="test.LogInterceptor"/>

   <aop:config>
     <aop:aspect id="asp" ref="aspetbean">
        <!--定义切入点-->  
        <aop:pointcut id="mycut" expression="execution(* test.**.*(..))" />
        <!-- 第一个*代表所有的返回值类型 第二个*代表所有的类 第三个*代表类所有方法 最后一个..代表所有的参数。 -->
        <!--定义通知(Advice)-->  
        <aop:before pointcut-ref="mycut"  method="doAccessCheck" /> <!-- 前置通知  -->
        <aop:after-returning pointcut-ref="mycut"  method="doAfterReturning" /> <!-- 后置通知  -->
        <aop:after-throwing pointcut-ref="mycut"  method="doAfterThrowing" /> <!-- 例外通知  -->
        <aop:after pointcut-ref="mycut"  method="doAfter" /> <!-- 最后通知  -->
        <aop:around pointcut-ref="mycut"  method="doBasicProfiling" /> <!-- 环绕通知  -->
     </aop:aspect>
  </aop:config>
</beans>

LogInterceptor.java

import org.aspectj.lang.ProceedingJoinPoint;
/**
 * 切面 
 * 例子:日志记录
 */
public class LogInterceptor {    
    public void doAccessCheck() {
        System.out.println("前置通知");
    }
    public void doAfterReturning() {
        System.out.println("后置通知");
    }
    public void doAfter() {
        System.out.println("最终通知");
    }
    public void doAfterThrowing() {
        System.out.println("例外通知");
    }
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("进入方法");
        Object result = pjp.proceed();
        System.out.println("退出方法");
        return result;
    }
}

User.java

public class User {
    private String id;
    private String name;
    private String sex;
    private int age;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }   
}

UserService.java

import java.util.List;
public interface UserService {
    public User queryUserById(int id);    
    public List<User> queryUserList();
    
}

UserServiceImpl.java

import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements UserService {    
    /**
     * 通过id查询用户信息
     */
    @Override
    public User queryUserById(int id) {
        System.out.println("=========进入queryUserById方法==========");
        User user = null;
        if(id==1){
            user = new User();
            user.setAge(18);
            user.setId("1");
            user.setName("张三");
            user.setSex("1");    
        }
        return user;
    }  
    /**
     * 查询用户列表
     */
    @Override
    public List<User> queryUserList() {
        System.out.println("=========进入queryUserList方法==========");
        User user1 = new User();
        user1.setAge(18);
        user1.setId("1");
        user1.setName("张三");
        user1.setSex("1");

        User user2 = new User();
        user2.setAge(18);
        user2.setId("1");
        user2.setName("张三");
        user2.setSex("1");

        ArrayList<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        return userList;
    }
}

运行结果:

技术分享

 

The prefix "aop" for element "aop:aspectj-autoproxy" is not bound.

标签:

原文地址:http://www.cnblogs.com/gugibv/p/5724289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!