Spring中的aop

AOP:面向切面编程(Aspect-Oriented Programming)
AOP可以说是对OOP的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右的关系。例如日志功能。日志代码往往水平地散布在所有对象层次中,而与它所散布到的对象的核心功能毫无关系。在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。将程序中的交叉业务逻辑(比如安全,日志,事务等),封装成一个切面,然后注入到目标对象(具体业务逻辑)中去。实现AOP的技术,主要分为两大类:一是采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行;二是采用静态织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码.

以上是比较官方的解释,下面我就说书自己对于Aop的理解,由于自己是菜鸟,读者慎读就行了

具体的编程模式如下:在传统的编程模式就是自上而下的,先在在进行编程的时候,使用了aop的话,将会在service层,添加很多新的公用的功能让你,原来的自上而下就开始在service层开始横向发展了,这就是所谓的面相切面编程。

在面像切面编程的时候,你会发现,好多事务处理都是公共的,所以在这里不得不提到我上次讲的动态代理的思想,下面举例说明,使用了spring中的前置通知和后置通知。

1,通过Spring api来进行实现:

AfterLog.java

package log;

import java.lang.reflect.Method;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;

public class AfterLog implements AfterReturningAdvice{
	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args,
			Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行,返回值是:"+returnValue);
	}
}
Log.java

package log;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class Log implements MethodBeforeAdvice{
	/*
	 * method 被调用的方法对象
	 * args被调用的方法的参数
	 * target被调用的方法的目标对象
	 * */
	@Override
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行");
	}
}

UserService.java 用户公共事务接口
package service;

public interface UserService {
	public void seeHouse();
	public void fareHouse();
}

UserServiceImp.java 接口实现

package service;

public class UserServiceImp implements UserService {

	@Override
	public void seeHouse() {
		System.out.println("看房子");
	}
	@Override
	public void fareHouse() {
		System.out.println("收取中介费");
	}
	
}

beans.xm 配置文件

<?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.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="userService" class="service.UserServiceImp"/>
	<bean id="log" class="log.Log"/>
	<bean id="afterLog" class="log.AfterLog"/>
	<aop:config>
		<!-- 配置一个方法 -->
		<!-- <aop:pointcut expression="execution(* service.UserService.fareHouse())" id="pointcut"/> -->
		<!-- 配置所有方法 -->
		<!-- <aop:pointcut expression="execution(* service.UserService.*())" id="pointcut"/> -->
		<!-- 配置所有方法所有参数 -->
		<aop:pointcut expression="execution(* service.UserService.*(..))" id="pointcut"/>
		<!-- 所有类都要链接这个日志 -->
		<aop:pointcut expression="execution(* service.*.*(..))" id="pointcut"/>
		<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
		<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
	</aop:config>
</beans>
Test.java  测试类

package Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;

public class TestAop {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
		UserService userService=(UserService)context.getBean("userService");
		userService.fareHouse();
		//userService.seeHouse();
	}
}

输出结果;

service.UserServiceImp的fareHouse方法被执行
收取中介费
service.UserServiceImp的fareHouse方法被执行,返回值是:null

上述例子,就是在原来的事务上面加上了一个日志功能,然后使用了aop进行管理,在进行事务处理,也就是执行userService.fareHouse();的时候,动态调用log日志进行打印信息。以上是使用spring自带API进行设置的,下面我们通过自定义来进行做:,

2,自定义实现:改变的只有log.java和beans.xml。其他的不变。

log.java

package log;

public class Log {
	public void before() {
		System.out.println("----------before------------");
	}
	public void after() {
		System.out.println("----------after------------");
	}
}
beans.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:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="userService" class="service.UserServiceImp"/>
	<bean id="log" class="log.Log"/>
	<aop:config>
		<aop:aspect ref="log">
			<aop:pointcut expression="execution(*  service.UserServiceImp.*())" id="pointcut"/>
			<aop:before method="before" pointcut-ref="pointcut"/>
			<aop:after method="after" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>
</beans>
Test2.java

package Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;

public class TestAop {
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
		UserService userService=(UserService)context.getBean("userService");
		userService.fareHouse();
		//userService.seeHouse();
	}
}
输出结果:

----------before------------
收取中介费
----------after------------

3,通过注解实现:

Log.java

package log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class Log {
	@Before("execution(* service.UserServiceImp.*(..))")
	public void before() {
		System.out.println("----------before------------");
	}
	@After("execution(* service.UserServiceImp.*(..))")
	public void after() {
		System.out.println("----------after------------");
	}
	@Around("execution(* service.UserServiceImp.*(..))")
	public Object aroud(ProceedingJoinPoint jp) throws Throwable {
		System.out.println("环绕前");
		System.out.println("签名:"+jp.getSignature());
		Object result=jp.proceed();
		System.out.println("环绕后");
		return result;
	}
}
beans.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:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="userService" class="service.UserServiceImp"/>
	<bean id="log" class="log.Log"/>
	<aop:aspectj-autoproxy/>
</beans>
Test.java

package Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;

public class TestAop {
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
		UserService userService=(UserService)context.getBean("userService");
		userService.fareHouse();
		//userService.seeHouse();
	}
}
输出结果:

环绕前
签名:void service.UserService.fareHouse()
----------before------------
收取中介费
环绕后
----------after------------

以上就是aop的实现方式,实现大致就这样,看的时候,记得吧代理和IOC看完再看aop这是层次递增的,当初因为初学就分开写了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值