Java-基于注解的面向AOP(切面)编程
来源:互联网 发布:大数据查询平台 编辑:程序博客网 时间:2024/05/21 07:05
面向切面编程
自定义切面
package cn.csdn.service;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * 切面 * */@Aspectpublic class MyInterceptor {@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")private void anyMethod() {}//声明一个切入点@Before("anyMethod() && args(name)")public void doAccessCheck(String name) {System.out.println("前置通知:"+ name);}@AfterReturning(pointcut="anyMethod()",returning="result")public void doAfterReturning(String result) {System.out.println("后置通知:"+ result);}@After("anyMethod()")public void doAfter() {System.out.println("最终通知");}@AfterThrowing(pointcut="anyMethod()",throwing="e")public void doAfterThrowing(Exception e) {System.out.println("例外通知:"+ e);}@Around("anyMethod()")public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {//if(){//判断用户是否在权限System.out.println("进入方法");Object result = pjp.proceed();System.out.println("退出方法");//}return result;}}
Dao类:
package cn.csdn.service;public interface PersonService {public void save(String name);public void update(String name, Integer id);public String getPersonName(Integer id);}
DaoImpl类:
package cn.csdn.service.impl;import cn.csdn.service.PersonService;public class PersonServiceBean implements PersonService {public String getPersonName(Integer id) {System.out.println("我是getPersonName()方法");return "xxx";}public void save(String name) {throw new RuntimeException("我爱例外");//System.out.println("我是save()方法");}public void update(String name, Integer id) {System.out.println("我是update()方法");}}
bean.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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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.csdn.service.MyInterceptor"/> <bean id="personService" class="cn.csdn.service.impl.PersonServiceBean"></bean></beans>
测试类Test:
package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.csdn.service.PersonService;public class SpringAOPTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void interceptorTest(){ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");PersonService personService = (PersonService)cxt.getBean("personService");personService.save("xx");}}
- Java-基于注解的面向AOP(切面)编程
- spring实战-基于注解的面向切面编程(AOP)
- 面向切面编程(AOP)应用,日志切面,基于注解
- Spring面向切面编程--AOP,AspectJ,基于注解方式。
- Spring 面向切面(AOP)编程,注解
- spring 基于Aspect和注解的切面编程(aop)
- AOP-面向切面的编程
- 面向切面的AOP编程
- spring framework面向切面编程示例(基于注解):spring-aop
- 使用java的Proxy实现AOP(面向切面编程)
- Java面试之面向切面编程AOP的原理,
- Java实现AOP面向切面编程的实例教程
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- AOP,面向切面编程
- AOP面向切面编程
- 面向切面编程 (AOP)
- AOP--面向切面编程
- AOP面向切面编程
- MySQL 优化(必看 必看)
- 第四章 连接管理
- 如何同时启动多个Tomcat服务器
- HDU1276(模拟)
- Fucked Java.
- Java-基于注解的面向AOP(切面)编程
- Linux 系统命令及其使用详解(大全)
- 问题一百五十:正整数解
- 【语言-C++】查找两串字符中 最长相等字符串
- 博弈总结
- USB调试默认修改
- C++ 全局变量的声明和定义
- 笔记1-2: 将标准输入复制到标准输出
- 隐马尔科夫模型(HMM)及其扩展