Spring AOP(面向切面编程)【AOP快速入门】

假设项目中多个模块都需要使用日志管理功能,那么“日志管理”即为一个切面。

使用类Logger实现“日志管理”功能,代码如下:

package log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class Logger {

	public static void log(String msg){
		File file = new File("spring.log");
		try {
			PrintWriter out = new PrintWriter(new FileWriter(file ,true));
			out.println(new Date()+": "+msg);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

上述代码中Logger类的log方法,将日志信息保存到spring.log文件中。

下面使用Spring框架的AOP组件,将日志管理的功能织入到项目中。

1、创建目标对象(Target Object)

目标对象是需要被通知的对象,即需要被切面横切的对象。

目标对象必须是实现了某个接口的对象。(CustomerServiceImpl被作为目标对象,使用日志管理功能)

2、创建通知(Advice)

通知是切面的具体实现。

日志功能需要在方法执行前调用,所以应该使用“before advice”,即在方法调用前被调用的Advice。

package log;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class LogBeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2)
			throws Throwable {
		System.out.println("Invoke LogBeforeAdvice.before");
		Logger.log(arg0.getName());
	}

}

上述代码的LogBeforeAdvice类实现类接口MethodBeforeAdvice,重写了其中的before方法。

before方法中调用了Logger类的log方法,实现了日志功能。


3、在IoC容器中生成代理对象,将通知织入到目标对象中。

在Spring容器中,默认使用JavaSE的动态代理作为AOP代理,具体实现类是ProxyFactoryBean。

Spring的AOP也是基于IoC实现的,需要在IoC容器中配置ProxyFactoryBean的bean实例,用来代理目标对象,将通知织入到目标对象中。


要使用Ioc配置ProxyFactoryBean实例,首先需要了解ProxyFactoryBean中的setter方法,

ProxyFactoryBean有以下几个常用的setter方法:

①、setInterfaces(Class[] interfaces):该方法用来配置代理接口,即目标兑现所实现的接口

CustomerServiceImpl类实现了CutomerService接口,所以代理接口是CutomerService。该方法的参数是数组类型,所以使用<list>元素装配。

②、setInterceptorNames(String[] interceptorNames):该方法用来配置拦截器的名字,即目标对象需要织入的Advice或者Advisor。

在本例中,即LogBeforeAdvice的名字。

③、setTargetName(String targetName):该方法用来配置目标对象的名字,在本例中,即CustomerServiceImpl的bean名字。


4、配置目标对象

要使用AOP编程,首先需要在IoC容器中配置目标对象,本例中,目标对象是CustomerServiceImpl类型实例

	<bean id="service" class="service.CustomerServiceImpl">
		<property name="dao">
			<ref bean="dao"/>
		</property>
	</bean>

5、配置Advice对象

Advice是AOP编程中非常重要的对象,用来封装切面,Advice也必须在IoC容器中管理,如下所示:

	<bean id="logbefore" class="log.LogBeforeAdvice">
	</bean>

6、配置ProxyFactoryBean对象

有了目标对象和Advice失利后,就可以配置代理对象,在Spring AOP中,往往使用ProxyFactoryBean对象作为代理对象,如下所示:

<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="interfaces">
			<list>
				<value>service.CustomerService</value>
			</list>
		</property>
		<property name="targetName">
			<value>service</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>logbefore</value>
			</list>
		</property>
	</bean>
上述配置中,使用<property name="targetName">指定了目标对象为service,即CustomerServiceImpl的bean;

使用<property name="interceptorNames">定义了拦截器,指定LogBeforeAdvice的bean作为拦截器;

使用<property name="interfaces">定义拦截器接口,指定了目标对象的接口为CustomerService接口。


这样一来,Spring的AOP组件将运行期动态生成service的代理对象serviceProxy实例,

该实例也实现了CustomerService接口规范,实现了CustomerService接口中所有方法,而且在service实例中织入了logbefore的功能,

将在每个方法调用前先调用logbefore中的before方法,加入记录日志功能。


使用如下代码测试:

①使用service对象:

	public static void main(String[] args) {
		ApplicationContext ctxt = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerService service = (CustomerService) ctxt.getBean("service");
		System.out.println(service.login("tom", "tom"));
	}
输出结果:

true
②使用serviceProxy对象:

	public static void main(String[] args) {
		ApplicationContext ctxt = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerService serviceProxy = (CustomerService) ctxt.getBean("serviceProxy");
		System.out.println(serviceProxy.login("tom", "tom"));
	}
输出结果:

Invoke LogBeforeAdvice.before
true

可见serviceProxy对象调用login方法是,小调用了LogBeforeAdvice中的before方法,然后调用service中的login方法,加入了日志功能。


使用Spring的AOP组件,可以不用考虑何时用“切面”,何处用“切面”,可以将业务模块和“切面”分别独立实现。

在IoC容器中,通过AOP代理动态进行织入,生成新的代理对象。

AOP很大程度上提高了业务代码的分离性。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值