姓名:罗秀群    班级:软件151

AOP是OOP的延续,是(Aspect Oriented Programming)的缩写,意思是面向切面编程。要理解AOP首先得弄明白代理的概念。

  AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理、安全检查、缓存、对象池管理等。 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP 代理则可分为静态代理和动态代理两大类,其中静态代理是指使用 AOP 框架提供的命令进行编译,从而在编译阶段就可生成 AOP 代理类,因此也称为编译时增强;而动态代理则在运行时借助于 JDK 动态代理、CGLIB 等在内存中"临时"生成 AOP 动态代理类,因此也被称为运行时增强。

知道这些其他的就是些配置了。

简单的实现annotations和xml对AOP的实现。

首先看下目录结构

MyInterceptor、MyInterceptor2分别是以annotations和xml定义的切面类

package com.service;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyInterceptor {
	@Pointcut("execution (* com.serviceImpl.PersonServiceImpl.*(..))")
	private void myMethod(){};
	
	@Before("myMethod()")
	public void doAccessCheck(){
		System.out.println("before");
	}
	
}
 
package com.service;

public class MyInterceptor2 {
	public void doAccessCheck(){
		System.out.println("before");
	}
}


业务和接口

 
package com.service;

public interface PersonService {
	public void save(String name);
	public void update(String name);
}

 

package com.serviceImpl;

import com.service.PersonService;

public class PersonServiceImpl implements PersonService {

	@Override
	public void save(String name) {
		// TODO Auto-generated method stub
		System.out.println("保存");
	}

	@Override
	public void update(String name) {
		// TODO Auto-generated method stub
		System.out.println("修改");
	}

}


简单做个方法前通知,其他的都一样

<?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: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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<aop:aspectj-autoproxy/>
	
	<bean id="personServiceImpl" class="com.serviceImpl.PersonServiceImpl"></bean>
	<bean id="personInterceptor" class="com.service.MyInterceptor2"></bean>
	
	<aop:config>
		<aop:aspect id="asp" ref="personInterceptor">
			<aop:pointcut id="myCut" expression="execution (* com.serviceImpl.PersonServiceImpl.*(..))"/>
			<aop:before pointcut-ref="myCut" method="doAccessCheck"/>
		</aop:aspect>		
	</aop:config>
</beans>


测试类

 

 
    1. package com.test;  
    2.   
    3.   
    4.   
    5. import org.junit.Test;  
    6. import org.springframework.context.ApplicationContext;  
    7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    8.   
    9. import com.service.PersonService;  
    10.   
    11. public class AopTest {  
    12.       
    13.     @Test  
    14.     public void interceptorTest(){  
    15.         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  
    16.         PersonService ps = (PersonService) ac.getBean("personServiceImpl");  
    17.         ps.save("aa");  
    18.     }  
    19. }