spring简单的AOP编程小结

使用注解进行AOP编程步骤:
1.配置spring配置文件,启用spring注解AOP编程
2.定义切面点
@Pointcut("execution(* com.xiaogang.service.impl.ServiceImpl.*(..))")
public void anyMethod(){}
3.引用切面点
   @Before(value="anyMethod()")
public void preMethod(){
System.out.println("前置通知");
}


spring配置文件:
<?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">
	
	<!-- 启用spring注解AOP编程 -->
	<aop:aspectj-autoproxy/>
	
	<bean id="daoImpl" class="com.xiaogang.dao.impl.DaoImpl" />
	
	<bean id="serviceImpl" class="com.xiaogang.service.impl.ServiceImpl">
		<property name="daoImpl" ref="daoImpl" />
	</bean>
	
	
	<bean id="MyAddtionalService" class="com.xiaogang.aspectj.MyAddtionalService" />
	
	
</beans>


示例代码:


执行的切面点的方法:

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;


/*
 * 切面:spring中所有的切面点都是方法的调用
 */
@Aspect
public class MyAddtionalService {


	//定义切面点
	@Pointcut("execution(* com.xiaogang.service.impl.ServiceImpl.*(..))")
	public void anyMethod(){}
	
    @Before(value="anyMethod()")
	public void preMethod(){
		System.out.println("前置通知");
	}
    
    @After(value="anyMethod()")
    public void backMethod(){
    	System.out.println("后置通知");
    }
    
    @AfterReturning("anyMethod()")
    public void afterReturn(){
    	System.out.println("最终通知");
    }
    
    @AfterThrowing("anyMethod()")
    public void throwhandler(){
    	System.out.println("异常通知");    	
    }
    
    @Around("anyMethod()")
    public Object around(ProceedingJoinPoint pjp){
    	Object object=null;
    	System.out.println("进入方法");
    	try {
			object=pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
    	System.out.println("退出方法");
    	return object;
    }
    


}




dao层、service层代码省略


测试代码:
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.xiaogang.service.IService;


public class SpringTest {
static ApplicationContext ctx;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ctx = new ClassPathXmlApplicationContext("beans.xml");
	}
	
	@Test
	public void testSave(){
		IService service = (IService)ctx.getBean("serviceImpl");
		service.save();
	}
	
	@Test
	public void testHello(){
		IService service = (IService)ctx.getBean("serviceImpl");
		service.hello();
	}


}



使用xml配置文件进行AOP编程步骤:

spring配置文件:
<?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">
	
	
	<bean id="daoImpl" class="com.xiaogang.dao.impl.DaoImpl" />
	
	<bean id="serviceImpl" class="com.xiaogang.service.impl.ServiceImpl">
		<property name="daoImpl" ref="daoImpl" />
	</bean>
	
	<bean id="MyAddtionalService" class="com.xiaogang.aspectj.MyAddtionalService" />
	
	<aop:config>
	   <aop:aspect id="aspectBean" ref="MyAddtionalService">
	      <!-- 定义切面 -->
		  <aop:pointcut  id="mycut" 
				expression="execution(* com.xiaogang.service.impl.ServiceImpl.*(..))"/>
		  <!-- 定义通知:通知类型和方法名称  切面引用 -->
		  <aop:after method="backMethod" pointcut-ref="mycut"/>
		  <aop:before method="preMethod" pointcut-ref="mycut"/>
		  <aop:after-returning method="afterReturn" pointcut-ref="mycut"/>
		  <aop:around method="around"  pointcut-ref="mycut"/>
		  <aop:after-throwing method="throwhandler"  pointcut-ref="mycut"/>
		</aop:aspect>
	</aop:config>
	
	
</beans>

java代码:
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;


/*
 * 切面:spring中所有的切面点都是方法的调用
 */


public class MyAddtionalService {


	public void preMethod() {
		System.out.println("前置通知");
	}


	public void backMethod() {
		System.out.println("后置通知");
	}


	public void afterReturn() {
		System.out.println("最终通知");
	}


	public void throwhandler() {
		System.out.println("异常通知");
	}


	public Object around(ProceedingJoinPoint pjp) {
		Object object = null;
		System.out.println("进入方法");
		try {
			object = pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("退出方法");
		return object;
	}


}



dao层、service代码略;
测试代码如上。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值