spring中的简单AOP
来源:互联网 发布:雨滴软件 编辑:程序博客网 时间:2024/06/05 14:35
- applicationContext.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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><bean id="messageService" class="com.zhiyou100.spring.service.impl.MessageServiceImpl"/> <bean id="serviceProxy" class="com.***.spring.proxy.ServiceProxy"/><aop:config> <aop:pointcut expression="execution(public * com.***..service..*.*(..))" id="defaultPointCut"/> <aop:aspect ref="serviceProxy"> <aop:before method="beforeDelete" pointcut-ref="defaultPointCut"/> <aop:after method="afterDelete" pointcut-ref="defaultPointCut"/> </aop:aspect> </aop:config> </beans>
2.定义一个接口 IMessageService
public interface IMessageService { public String getInfo(); public boolean remove(String mid);}
3.接口的实现类MessageServiceImpl
public class MessageServiceImpl implements IMessageService { @Override public String getInfo() { return "www.baidu.com"; } @Override public boolean remove(String mid) { Logger.getLogger(IMessageService.class).info("删除id:"+mid); return false; }}
4.定义一个描述AOP程序处理的结构类
public class ServiceProxy { public void beforeDelete(){ Logger.getLogger(ServiceProxy.class).info("删除前执行操作"); } public void afterDelete(){ Logger.getLogger(ServiceProxy.class).info("删除后执行操作"); }}
5.测试类
public class TestMessageService { private static ApplicationContext ctx = null; static{//静态代码块优先于所有的代码块,目的是为了静态属性初始化 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testGetInfo() { IMessageService msgService = ctx.getBean("messageService",IMessageService.class); Logger.getLogger(TestMessageService.class).info(msgService.remove("90")); }}
6.程序运行结果
(使用log4j输出)
INFO [com.***.spring.proxy.ServiceProxy] - 删除前执行操作INFO[com.***.spring.service.IMessageService] - 删除id:90INFO [com.***.spring.proxy.ServiceProxy] - 删除后执行操作INFO[com.***.spring.test.TestMessageService] - false
7.pom.xml中需要相应的架包支持
这个工程在maven下写的
阅读全文
0 0
- spring中的简单AOP
- spring中的aop简单编程
- AOP在spring中的简单实现
- Spring中的简单实现AOP小例子
- 简单认识 Spring AOP
- spring Aop简单示例
- spring 简单Aop
- Spring AOP 简单实例
- spring aop 简单实例
- Spring Aop 简单例子
- Spring aop简单示例
- spring aop简单示例
- Spring AOP 简单使用
- spring aop简单编程
- spring aop 简单例子
- 简单 spring AOP 配置文件
- spring aop 简单介绍
- Spring aop简单示例
- unity C#中,用BinaryWrite和BinaryReader来存取数据
- opencv_traincascade 训练自己的分类器
- ElasticSearch 5.0.0 安装部署常见错误或问题
- 【shell脚本】逐行处理文本文件
- iOS 模块详解—「Runloop 面试、工作」看我就
- spring中的简单AOP
- 1、初识socke
- 在myeclipse利用ant构建时遇到The path xxx appears to be part of Subversion 1.7 (SVNKit 1.4) or greater解决办法
- 加密算法
- bootstrap的按钮特性
- 枚举-enumeration
- 使用git和github管理自己的项目---基础操作学习
- Imagerloader
- 视频播放器