注释配置
http://blog.csdn.net/initphp/article/details/8198962
xml配置
AOP面向切面 是在方法中的前,后什么的位置调某些方法,往往是在有实质作用的方法的前后什么的输出日志文件什么的信息,而实质作用的方法那里不会有任何迹象
像记录信息这种就叫切面
往往将正经的有作用的为类,切面也为类,都会在bean.xml中以<bean></bean>的方式配置
然后在下面配<aop:config proxy-target-class="true"></aop>的信息
在其中 现配切入点(在哪个方法进行切入) <aop:pointcut expression="execution(* springaopfront.Calculator.add(..))" id="pointcut"/>
此处为在springaopfront.Calculator的add方法且此方法不定返回值和参数,切入,切入id为pointcut
然后是对切面文件的aop声明,order为同为切面,切入的先后1早于2
before、afer等为切入类型,在方法执行前,后。。。。method为切面中的方法,即在方法前后什么的是调用logging这个切面中的哪个方法,pointcut-ref为切面id
<aop:aspect ref="logging" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-returning method="afterreturning" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterMethod" pointcut-ref="pointcut" throwing="e"/>
<!-- <aop:around method="beforeMethod" pointcut-ref="pointcut"/> -->
</aop:aspect>
<aop:aspect ref="always" order="1">
<aop:before method="always" pointcut-ref="pointcut"/>
</aop:aspect>
--------------------------------------------------------------------------------------------
具体实现:
CalcularorImp:实质性方法的接口:
package springaopfront;
public interface CalcularorImp {
int add(int a,int b);
int div(int x,int y);
}
Calculator:实质性方法
package springaopfront;
public class Calculator implements CalcularorImp {
public int add(int a, int b) {
int result=0;
result=a+b;
return result;
}
public int div(int x, int y) {
int result=0;
try {
result=x/y;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
切面1:
package springaopfront;
public class Logginga {
public void always()
{
System.out.println("always save------");
}
}
切面2:
package springaopfront;
import org.aspectj.lang.JoinPoint;
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint)
{
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs();
System.out.println("the Method "+methodName+"begin with"+args);
}
public void afterMethod(JoinPoint joinPoint)
{
String methodName=joinPoint.getSignature().getName();
System.out.println("end of "+methodName);
}
public void afterreturning(JoinPoint joinPoint, Object result)
{
System.out.println("CLAP! CLAP!! "+result);
}
}
测试方法
package springaopfront;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
CalcularorImp cal=(Calculator) ctx.getBean("Calculator");
int result=cal.add(1, 2);
System.out.println("result:"+result+"====");
result=cal.div(1000, 100);
System.out.println("result:"+result+"====");
}
}
配置xml
//aop Spring通用
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="logging" class="springaopfront.LoggingAspect">
</bean>
<bean id="always" class="springaopfront.Logginga">
</bean>
<bean id="Calculator" class="springaopfront.Calculator">
</bean>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* springaopfront.Calculator.add(..))"
id="pointcut"/>
<aop:aspect ref="logging" order="2">
//前置条件
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
//后置条件
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
//后返回条件
<aop:after-returning method="afterreturning" pointcut-ref="pointcut" returning="result"/>
// 异常
<aop:after-throwing method="afterMethod" pointcut-ref="pointcut" throwing="e"/>
//环绕(包括前面几种条件,都会显示)
<!-- <aop:around method="beforeMethod" pointcut-ref="pointcut"/> -->
</aop:aspect>
<aop:aspect ref="always" order="1">
<aop:before method="always" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>