zoukankan      html  css  js  c++  java
  • Spring 注解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:context="http://www.springframework.org/schema/context"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    						http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    	<aop:aspectj-autoproxy />
    	<bean id="myInterceptor" class="cn.itcast.service.MyInterceptor" />
    	<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" />
    </beans>
    

     

    业务Bean,接口就不贴了

    package cn.itcast.service.impl;

    import cn.itcast.service.PersonService;

    public class PersonServiceBean implements PersonService {

    public String getPersonName(Integer id) {
    System.out.println(
    "我是getPersonName()方法");
    return "XXX";
    }

    public void save(String name) {
    System.out.println(
    "我是save()方法");
    }

    public void update(String name, Integer id) {
    System.out.println(
    "我是update()方法");
    }

    }

    实现AOP的类 

    package cn.itcast.service;

    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;

    @Aspect
    public class MyInterceptor {
    @Pointcut(
    "execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
    private void anyMethod() {}

    @Before(
    "anyMethod()")
    public void doAccessCheck(String userName) {
    System.out.println(
    "前置通知");
    }
    }

    测试方法

    package junit.test;

    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import cn.itcast.service.PersonService;

    public class SpringAOPTest {
    @Test
    public void interceptorTest(){
    ApplicationContext ctx
    = new ClassPathXmlApplicationContext("beans.xml");
    PersonService personService
    = (PersonService)ctx.getBean("personService");
    personService.save(
    "xxx");
    }
    }

  • 相关阅读:
    clr via c# 定制特性
    clr via c# delegate
    clr via c# Array2
    clr from c# 字符 ,字符串 和 文本处理
    clr via c# 接口
    clr via c# 泛型
    clr via c# 事件
    clr via c# 参数和属性
    程序设计入门——C语言 第6周编程练习 2 完数(5分)
    程序设计入门——C语言 第6周编程练习 1 分解质因数(5分)
  • 原文地址:https://www.cnblogs.com/live365wang/p/2129415.html
Copyright © 2011-2022 走看看