java学习spring框架之aop编程

aop编程就是切片编程,比如在一个项目中,有很多地方需要记录日志,如果像普通方法就是在每个地方都写一遍记录日志的代码,这样一来是代码重复率太高,二来以后一旦要修改记录日志的代码时就要在很多地方修改,太麻烦。

aop编程的好处就是可以将记录日志的代码抽离出来,写在一个通知类里,然后通过代理来实现自动调用,这样就不必在每个地方都写一遍记录日志的代码了,修改时也只要修改这个通知类就行了,方便简单。具体操作如下:

项目目录:

aop提倡接口编程,所以使用aop时必须使用接口,现在定义一个接口

TestServiceInter.java

package com.hsp.aop;

public interface TestServiceInter {
	
	public void sayHello();
}

接口里只有一个简单的方法 sayHello,然后定义一个类来继承这个接口

Test1Service.java

package com.hsp.aop;

public class Test1Service implements TestServiceInter {
	private String name;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public void sayHello() {
		// TODO Auto-generated method stub
		System.out.println("hi "+name);
	}
	

}

Test1Service类实现了TestServiceInter接口,并重写接口的方法

此处我们只写了一个Test1Service类,实际项目中可能有多个类似的类都继承了TestServiceInter接口,那么就想在调用所有继承了TestServiceInter接口的类的sayHello方法前都进行记录日志的操作怎么办呢,像开头说的可以在每个类的sayHello方法里都写一段记录日志的代码,显然这种方法是不可取的,下面就用aop的方法来实现。

    首先需要一个通知类也就是在这个类里面进行记录日志操作

    MyMethodBeforeAdvice.java  

package com.hsp.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
	/**自定义前置通知类
	 * method:被调用方法的名字
	 * args:给method传递的参数
	 * target:目标对象
	 */
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("记录日志成功");
	
	}
	
}

然后配置下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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
			
<!-- 配置被代理的对象 -->
<bean id="test1Service" class="com.hsp.aop.Test1Service">
<property name="name" value="szy" />
</bean>

<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.hsp.aop.MyMethodBeforeAdvice">
</bean>

<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
		<!-- 被代理的接口集合-->
		<property name="proxyInterfaces">
			<list>
				<value>com.hsp.aop.TestServiceInter</value>
			</list>
		</property>
		
		<!-- 织入通知对象 -->
		<property name="interceptorNames">
			<list>
				<value>myMethodBeforeAdvice</value>
			</list>
		</property>
		
		<!-- 被代理的对象 -->
		<property name="target" ref="test1Service" />
		
</bean>

</beans>

然后新建一个App.java来执行

package com.hsp.aop;

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

public class App {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/aop/beans.xml");
		TestServiceInter tsi = (TestServiceInter) ac.getBean("proxyFactoryBean");
		tsi.sayHello();
		
	}

}

输出结果

记录日志成功
hi szy

猜你喜欢

转载自blog.csdn.net/xxs18326183038/article/details/84872163
今日推荐