使用aop需要在网上下载两个jar包:
- aopalliance.jar
- aspectjweaver.jar
为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将刚才下载好的jar添加进去
前置增强实例
编写TimeHandler.java
package com.example.spring; public class TimeHandler {
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
}
}
编写HelloWorld.java
package com.example.spring; public class HelloWorld {
public void printHello(){
System.out.println("Hello Aop.");
}
}
编写配置文件
<?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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 ,method:增强类()里面的方法(beforTime())作为前置-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:before method="beforTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>
编写Application.java
package com.example.spring; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\aop.xml");
//得到配置创建的对象
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.printHello();
}
}
运行输出
前置增强:CurrentTime = 1510134673408
Hello Aop.
可以看到,打印Hello Aop.之前会先打印当前的时间CurrentTime = 1510132664923。
后置增强实例
修改TimeHandler.java
package com.example.spring; import org.aspectj.lang.ProceedingJoinPoint; public class TimeHandler {
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
} public void afterTime()
{
System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis());
}
}
修改配置文件aop.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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<!--<aop:before method="beforTime" pointcut-ref="pointcut1"/>--> <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:after method="afterTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>
运行输出
Hello Aop.
后置增强:CurrentTime = 1510134850754
环绕增强实例
修改TimeHandler.java
package com.example.spring; import org.aspectj.lang.ProceedingJoinPoint; public class TimeHandler {
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
} public void afterTime()
{
System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis());
} public void aroundTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//方法之前
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis()); //执行被增强的方法
proceedingJoinPoint.proceed(); //方法之后
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis());
} }
修改aop.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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<!--<aop:before method="beforTime" pointcut-ref="pointcut1"/>--> <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<!--<aop:after method="afterTime" pointcut-ref="pointcut1"/>--> <!-- 配置环绕增强类型 method:增强类()里面的方法(aroundTime())作为环绕通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:around method="aroundTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>
运行输出
环绕增强:CurrentTime = 1510135559066
Hello Aop.
环绕增强:CurrentTime = 1510135559074
之后就不用修改源程序,只需通过调整配置文件,就可以调整程序的逻辑。
修改配置文件
<?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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 配置aop操作-->
<aop:config>
<!-- 2.1 配置切入点-->
<!-- 设置切入点id为pointcut1 -->
<aop:pointcut id="pointcut1" expression="execution(* com.example.spring.HelloWorld.*(..))"/> <!-- 2.2 配置切面-->
<aop:aspect ref="timeHandler">
<!-- 配置前置增强类型 method:增强类()里面的方法(beforTime())作为前置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:before method="beforTime" pointcut-ref="pointcut1"/> <!-- 配置后置增强类型 method:增强类()里面的方法(afterTime())作为后置通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:after method="afterTime" pointcut-ref="pointcut1"/> <!-- 配置环绕增强类型 method:增强类()里面的方法(aroundTime())作为环绕通知-->
<!-- pointcut-ref设置为切入点的id:pointcut1 -->
<aop:around method="aroundTime" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>
运行输出
前置增强:CurrentTime = 1510190036105
环绕增强:CurrentTime = 1510190036105
Hello Aop.
环绕增强:CurrentTime = 1510190036122
后置增强:CurrentTime = 1510190036122