spring中的简单AOP

来源:互联网 发布:雨滴软件 编辑:程序博客网 时间:2024/06/05 14:35
  1. 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下写的

原创粉丝点击