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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://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; @Aspectpublic 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 { @Testpublic void interceptorTest(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService)ctx.getBean("personService"); personService.save("xxx"); }}
转载于:https://www.cnblogs.com/live365wang/archive/2011/08/06/2129415.html
Spring 注解AOP 入门相关推荐
- Spring框架——AOP入门笔记以及个人总结
注:作者本人也是初学者,所以本文有些总结性见解可能存在问题,但是多数问题都是在上网查询过资料后总结的,如果有逻辑或者原理上的错误,或者见解不同,欢迎在评论区讨论!!! 目录 Spring的AOP 1. ...
- Spring 注解AOP
1.导包 2.编写切面类,在切面类上直接使用AOP注解 packagestar.july.e_spring_aop_annotation; importorg.aspectj.lang.Proceed ...
- icop java,java基于spring注解AOP的异常处理的方法
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- 基于spring注解AOP的异常处理
一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...
- Spring Boot————AOP入门案例及切面优先级设置
看了这篇文章,如果你还是不会用AOP来写程序,请你打我!! =.=||| 引言 Spring AOP是一个对AOP原理的一种实现方式,另外还有其他的AOP实现如AspectJ等. AOP意为面向切面编 ...
- Spring注解开发入门教程
注解开发: 什么是驱动注解? 注解启动时使用注解的形式替代xml配置,将繁杂的spring配置文件从工程中彻底消除掉,简化书写 注解驱动的弊端 为了达成注解驱动的目的,可能会将原先很简单的书写,变的更 ...
- Spring注解开发学习笔记
1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- spring的AOP配置之@注解方式
AOP配置(注解) spring的AOP概念在spring的AOP配置之XML方式这骗博客中详细介绍了,这篇博客就直接开始介绍AOP配置需要那些注解 AOP注解详解 @Aspect 标志为一个切面类 ...
最新文章
- JAX-WS Web 服务开发调用和数据传输分析
- 从AI、加密货币到火星任务,一种更强大、更稳定的存储设备
- C#连接Oracle数据库的方法(Oracle.DataAccess.Client也叫ODP.net)
- linux shutter截图,Ubuntu中安装Shutter截图工具
- HTML encoding of foreign language characters
- Python String 方法详解
- 华为云ROMA,联接企业应用的现在与未来
- Spring Boot 案例:连接后台数据库实现用户登录
- 一个很好用的JavaScript的文件上传插件plupload
- sql 将8位字符串转换成日期型
- mysql 5.7.14 在 windows 下的配置
- Modifying a Dynamic Library Without Changing the Source Code
- win7系统激活工具
- 计算机论文题目_基于java的毕业设计题目_50例
- vue3 通过naive-ui 使用xicons
- 面试官:测试计划和测试方案有什么区别?
- 数据库和 MIDP,第一部分:了解记录管理系统
- Python3 字符串小练习
- 我的电脑数据执行保护设置不了须是计算机管理员,比使用boot.ini文件。我该怎么办?
- 抖音爆火李峋同款爱心代码,简单附带教程,还有烟花代码,手残党也能学会!!