AOP切面编程一

本文介绍如何使用Spring AOP实现业务层的日志记录功能。首先定义了业务层接口与实现,接着通过AOP定义了前置和后置通知处理类,并在Spring配置文件中配置了切入点和通知。最后通过测试类验证了AOP功能的有效性。

第一步:定义业务层接口与实现

1.定义接口

package group.esperanto.service;

public interface IMessageService {
    public boolean doRemove(String mid);
}

2.定义实现类

package group.esperanto.service.impl;

import org.apache.log4j.Logger;
import group.esperanto.service.IMessageService;

public class MessageServiceImpl implements IMessageService {

	@Override
	public boolean doRemove(String mid) {
		Logger.getLogger(IMessageService.class).info("[业务层]---执行删除ID = "+mid);
		return false;
	}

}

第二步:定义AOP程序处理类

package group.esperanto.proxy;

import org.apache.log4j.Logger;

public class ServiceProxy {
     public void BeforeInvoke(){
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Before]"); 
     }
     public void AfterInvoke(){
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Before]"); 
     }
}

第三步:在applicationContext中进行配置

<?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:task="http://www.springframework.org/schema/task"  
	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
		http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-4.1.xsd ">
     <bean id="serviceProxy" class="group.esperanto.proxy.ServiceProxy" />
     <bean id="messageServiceImpl" class="group.esperanto.service.impl.MessageServiceImpl"/>
     <!-- 针对AOP的处理进行配置 -->
     <aop:config>
        <!-- 定义业务操作切入点 -->
       <aop:pointcut expression="execution(* group.esperanto.service..*.*(..))" id="messagePoint"/>
        <!-- 定义具体操作到切入点的方法上 -->
       <aop:aspect ref="serviceProxy">
             <!-- 引用外部定义好的切入点 -->
           <aop:before method="beforeInvoke" pointcut-ref="messagePoint"/>
              <!-- 内部定义切入点 -->
           <aop:after method="afterInvoke" pointcut="execution(* group.esperanto.service..*.*(..))"/>
       </aop:aspect>
     </aop:config>
</beans>

第四步:测试调用

package group.esperanto.test;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import group.esperanto.service.IMessageService;

public class Task {
	public static void main(String[] args) {
	  ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
      IMessageService messageService = ctx.getBean("messageServiceImpl",IMessageService.class);
      Logger.getLogger(Task.class).info(messageService.doRemove("110"));
	}
}

 

转载于:https://my.oschina.net/u/3647851/blog/1507152

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值