本文重要介绍spring的AOP编程的主要配置.
1.定义接口文件TestServiceInter.java和TestServiceInter1.java文件
package com.aoptest;
public interface TestServiceInter {
public void sayHello();
}
package com.aoptest;
public interface TestServiceInter1 {
public void sayBye();
}
2.创建接口首先类TestService.java
package com.aoptest;
public class TestService implements TestServiceInter,TestServiceInter1 {
private String name;
@Override
public void sayHello() {
System.out.println(name+",Hello!!");
}
@Override
public void sayBye() {
System.out.println(name+",Bye!!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.aoptest;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] arg1, Object arg2)throws Throwable {
System.out.println("logging……………"+method.getName());
}
}
4.配置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="testService" class="com.aoptest.TestService" >
<property name="name" >
<value>siege</value>
</property>
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.aoptest.MyMethodBeforeAdvice">
</bean>
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 配置代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>com.aoptest.TestServiceInter</value>
<value>com.aoptest.TestServiceInter1</value>
</list>
</property>
<!--把通知织入代理对象 -->
<property name="interceptorNames" value="myMethodBeforeAdvice"></property>
<!-- 配置被代理对象 -->
<property name="target" ref="testService"></property>
</bean>
</beans>
5.编写测试类App.java
package com.aoptest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext ac=(ApplicationContext) new ClassPathXmlApplicationContext("com/aoptest/beans.xml");
TestServiceInter test=(TestServiceInter) ac.getBean("proxyFactoryBean");//直接获取代理对象
test.sayHello();
((TestServiceInter1)test).sayBye();
}
}
测试结果如下:
logging……………sayHello
siege,Hello!!
logging……………sayBye
siege,Bye!!
其详细步骤如下:
1.定义接口
2.编写被代理对象(即目标对象)
3.编写通知对象(前置通知目标方法)
4.配置beans.xml
- 配置被代理对象
- 配置通知
- 配置代理对象:
① 配置代理接口集
②把通知织入代理对象
③配置被代理对象
- 编写测试类
AOP相关的术语概念:
1.切面:要实现的交叉功能,由通知实现
2.通知:切面的实际实现
3.连接点:插入切面的地点,指方法调用
4.目标对象:被代理的对象
5.代理:即代理对象
6.织入(过程):调用代理对象变发生织入
7.切入点:当织入发生时,连接点变成切入点