在竖向处理过程中,插入独立的处理。
使得2种处理同时进行
以方法为单位
-
在某些方法开始时候,自动插入一些操作
创建工程 spring Maven 做个例子 -
首先需要配置pom.xml
通过此标签实现AOP的基本功能
Aspectj: AOP的具体实装framework ,Eclipse Foundation的工程都有
例子(使用xml配置bean)
在Bean类中调用addData方法时,会自动调用addDataBefore方法
- 创建Bean,和接口类
package jp.tuyano.spring.aop1;
public interface IMyBean<T> {
public void setDataObject(T obj);
public T getDataObject();
public void addData(Object obj);
public String toString();
}
package jp.tuyano.spring.aop1;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class MyBean1 implements IMyBean<List<String>> {
private List<String> data = new ArrayList<String>();
private Date date = Calendar.getInstance().getTime();
@Override
public void setDataObject(List<String> obj) {
data = obj;
}
@Override
public List<String> getDataObject() {
return data;
}
@Override
public void addData(Object obj) {
data.add(obj.toString());
}
@Override
public String toString() {
String result = "MyBean1 [data=";
for(String s : data){
result += s + ", ";
}
SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
result += "date=" + fm.format(date) + "]";
return result;
}
}
- 创建MyBeanAspect
在addData方法调用时,会自动调用此方法
package jp.tuyano.spring.aop1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
public class MyBeanAspect {
public void addDataBefore(JoinPoint joinPoint) {
System.out.println("*addData before...*");
String args = "args: \"";
for(Object ob : joinPoint.getArgs()){
args += ob + "\" ";
}
System.out.println(args);
}
}
- 创建bean.xml,配置bean
aop:aspect ref指定被插入方法的id
aop:pointcut 设置插入点 expression 当~方法执行时,设置插入点id
aop:アドバイス 设置在执行方法的什么时候插入aspect方法,比如:
aop:before 设置插入方法设置在before, method为addDataBefore,插入点为上面设置的id
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<aop:config>
<aop:aspect id="mybeanaspect1" ref="aop1">
<aop:pointcut expression="execution(* jp.tuyano.spring.aop1.IMyBean.addData(..))" id="p1"/>
<aop:before method="addDataBefore" pointcut-ref="p1"/>
</aop:aspect>
</aop:config>
<bean id="bean1" class="jp.tuyano.spring.aop1.MyBean1"></bean>
<bean id="aop1" class="jp.tuyano.spring.aop1.MyBeanAspect"></bean>
</beans>
- 主函数
在主函数中调用2次addData方法,
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<aop:config>
<aop:aspect id="mybeanaspect1" ref="aop1">
<aop:pointcut expression="execution(* jp.tuyano.spring.aop1.IMyBean.addData(..))" id="p1"/>
<aop:before method="addDataBefore" pointcut-ref="p1"/>
</aop:aspect>
</aop:config>
<bean id="bean1" class="jp.tuyano.spring.aop1.MyBean1"></bean>
<bean id="aop1" class="jp.tuyano.spring.aop1.MyBeanAspect"></bean>
</beans>
使用config.java配置
- 删除bean.xml
- 重写MyBeanAspec
package jp.tuyano.spring.aop1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyBeanAspect {
// 指定插入点 ,且指定在方法addData执行之前插入
@Before("execution(* jp.tuyano.spring.aop1.IMyBean.addData(..))")
public void addDataBefore(JoinPoint joinPoint) {
System.out.println("*addData before...*");
String args = "args: \"";
for(Object ob : joinPoint.getArgs()){
args += ob + "\" ";
}
System.out.println(args);
}
}
在这里插入代码片
- 配置
package jp.tuyano.spring.aop1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
public class MyBeanConfig {
@Bean
public MyBean1 bean1() {
return new MyBean1();
}
@Bean
public MyBeanAspect aop1() {
return new MyBeanAspect();
}
}