目录结构
代码
java
package cn.edu.sdau.aop;
public interface ArithmeticCalculator {
public int add(int i, int j);
public int sub(int i, int j);
public int mul(int i, int j);
public int div(int i, int j);
}
package cn.edu.sdau.aop;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
package cn.edu.sdau.aop;
import org.aspectj.lang.JoinPoint;
import java.util.Arrays;
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Object [] args = joinPoint.getArgs();
System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
}
}
package cn.edu.sdau.aop;
import org.aspectj.lang.JoinPoint;
import java.util.Arrays;
public class VlidationAspect {
public void validateArgs(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
//取得参数.
Object [] args = joinPoint.getArgs();
System.out.println("-->validate:" + Arrays.asList(args));
if (args != null && args.length > 0) {
for(int i=0;i<args.length;++i){
if(((Integer)args[i]).intValue()<0){
System.out.println("警告:方法"+methodName+"()第"+i+"参数为负数:" +args[i]);
}
}
}
}
}
配置(applicationContext_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: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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="abcd" class="cn.edu.sdau.aop.ArithmeticCalculatorImpl"></bean>
<!-- 配置切面类的一个bean -->
<bean id="logging" class="cn.edu.sdau.aop.LoggingAspect"></bean>
<bean id="xyz" class="cn.edu.sdau.aop.VlidationAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切面及通知 -->
<aop:aspect ref="logging" order="2">
<aop:before method="beforeMethod"
pointcut="execution(* cn.edu.sdau.aop.ArithmeticCalculator.*(..))" />
<aop:after method="afterMethod"
pointcut="execution(* cn.edu.sdau.aop.ArithmeticCalculator.*(..))" />
</aop:aspect>
<!-- 配置第2个切面及通知 -->
<aop:aspect ref="xyz" order="1">
<aop:before method="validateArgs"
pointcut="execution(* cn.edu.sdau.aop.ArithmeticCalculator.*(..))" />
</aop:aspect>
</aop:config>
</beans>
运行App
package cn.edu.sdau;
/**
* Hello world!
*
*/
import cn.edu.sdau.aop.ArithmeticCalculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//(1)创建IoC容器
ApplicationContext ctx1 = new ClassPathXmlApplicationContext("applicationContext_xml.xml");
//(2)从容器IoC中获取对象(在类中已经采用注释)
ArithmeticCalculator ac = (ArithmeticCalculator) ctx1.getBean("abcd");
//(3)使用对象
int result = ac.add(10, 20);
System.out.println("result:" + result);
result = ac.div(-21, 3);
System.out.println("result:" + result);
}
}
aop示例
java
package cn.edu.sdau;
public class A {
private String a1;
private String a2;
public void mA1(){
System.out.println("我是A类的方法mA1(),被运行!");
for(int i = 0; i < 100000000; i++);
}
public void mA2(){
System.out.println("我是A类的方法mA2(),被运行!");
for(int i = 0; i < 100000000; i++);
}
public A() {}
public A(String a1, String a2) {
this.a1 = a1;
this.a2 = a2;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getA2() {
return a2;
}
public void setA2(String a2) {
this.a2 = a2;
}
}
package cn.edu.sdau;
public class B {
private String b1;
private String b2;
public void mB1(){
System.out.println("我是B类的方法mB1(),被运行!");
double i=0;
while(i<10e+8){
i=i+1;
}
//for(long i = 0; i <300000000; i++);
}
public void mB2(){
System.out.println("我是B类的方法mB2(),被运行!");
double i=0;
while(i<10e+8){
i=i+1;
}
//for(int i = 0; i < 250000000; i++);
}
public B() {}
public B(String b1, String b2) {
this.b1 = b1;
this.b2 = b2;
}
public String getB1() {
return b1;
}
public void setB1(String b1) {
this.b1 = b1;
}
public String getB2() {
return b2;
}
public void setB2(String b2) {
this.b2 = b2;
}
}
package cn.edu.sdau;
public class C {
private String c1;
private String c2;
public void mC1(){
System.out.println("我是C类的方法mC1(),被运行!");
for(int i = 0; i < 300000000; i++);
}
public void mC2(){
System.out.println("我是C类的方法mC2(),被运行!");
for(int i = 0; i < 300000000; i++);
}
public C() {}
public C(String c1, String c2) {
this.c1 = c1;
this.c2 = c2;
}
public String getC1() {
return c1;
}
public void setC1(String c1) {
this.c1 = c1;
}
public String getC2() {
return c2;
}
public void setC2(String c2) {
this.c2 = c2;
}
}
package cn.edu.sdau;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.aspectj.lang.JoinPoint;
public class X {
public void mX1(JoinPoint joinPoint) {
// 连接点对象做参数,利用该参数,获取目标对象的方法
String methodName = joinPoint.getSignature().getName();
String className = joinPoint.getTarget().getClass().getSimpleName();
System.out.println("我是前置通知,在"+className+"类方法:" + methodName + "()运行前被切入!");
// 获取当前系统的时间,并转换为yyyy年MM月dd日 HH:mm:ss格式,并显示
Calendar startTimeNow = Calendar.getInstance();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String startTime = fmt.format(startTimeNow.getTime());
System.out.println(className+"类方法:" + methodName + "(),运行开始时间:" + startTime);
}
public void mX2(JoinPoint joinPoint) {
// 连接点对象做参数,利用该参数,获取目标对象的方法
String methodName = joinPoint.getSignature().getName();
String className = joinPoint.getTarget().getClass().getSimpleName();
System.out.println("我是后置通知,在"+className+"类方法:" + methodName + "()运行前被切入!");
// 获取当前系统的时间,并转换为yyyy年MM月dd日 HH:mm:ss格式,并显示
Calendar endTimeNow = Calendar.getInstance();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String endTime = fmt.format(endTimeNow.getTime());
System.out.println(className+"类方法:" + methodName + "(),运行结束时间:" + endTime);
}
}
配置(spring.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:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd">
<!-- 配置 bean -->
<bean id="a" class="cn.edu.sdau.A"></bean>
<bean id="b" class="cn.edu.sdau.B"></bean>
<bean id="c" class="cn.edu.sdau.C"></bean>
<bean id="x" class="cn.edu.sdau.X"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切面及通知 -->
<aop:aspect ref="x">
<aop:before method="mX1"
pointcut="execution(* cn.edu.sdau.*.*())"/>
<aop:after method="mX2"
pointcut="execution(* cn.edu.sdau.*.*())"/>
</aop:aspect>
</aop:config>
</beans>
运行
package cn.edu.sdau;
/**
* Hello world!
*
*/
import cn.edu.sdau.aop.ArithmeticCalculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//(1)创建IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//(2)从容器IoC中获取对象
A a = (A) ctx.getBean("a");
B b = (B) ctx.getBean("b");
C c = (C) ctx.getBean("c");
//(3)使用对象
a.mA1();
b.mB2();
a.mA2();
b.mB1();
c.mC1();
}
}