准备
使用Maven引入依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zz</groupId>
<artifactId>spring-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>4.1.4.RELEASE</spring.version>
<spring-aspects.version>4.1.4.RELEASE</spring-aspects.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-aspects.version}</version>
</dependency>
</dependencies>
</project>
基于@Aspect注解
首先声明一个接口
public interface Car {
public void drive();
}
添加实现类
@Component( "bus" )
public class Bus implements Car {
@Override
public void drive() {
System.out.println("开公交车啦");
}
}
添加增强类
@Aspect
@Component
public class BusAdvice {
@Pointcut( "execution(* *.drive(..))" )
public void pointCut() {};
@Before( "pointCut()" )
public void before( JoinPoint joinPoint ) {
System.out.println("启动发动机~~");
}
@After( "pointCut()" )
public void after( JoinPoint jointPoint ) {
System.out.println("关闭发动机~~~");
}
}
在Spring的配置文件中启动注解和自动代理。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
">
<!-- 启动注解扫描和自动代理 -->
<context:component-scan base-package="com.zz.spring.aop2"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
Main函数:
public class Main {
public static void main( String[] args ) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext2.xml");
Car car = (Car) ctx.getBean("bus");
car.drive();
}
}
执行结果:
注入形式的Aspcet切面
声明一个接口
public interface Car {
public void drive();
}
添加另外一个实现类
public class AutoMobile implements Car {
@Override
public void drive() {
System.out.println("开汽车啦");
}
}
添加增强类
public class CarAdvice {
public void before( JoinPoint joinPoint ) {
System.out.println("启动发动机~~");
}
public void after( JoinPoint jointPoint ) {
System.out.println("关闭发动机~~~");
}
}
在Spring配置文件中配置AOP功能
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.1.xsd">
<!-- 被代理的Bean -->
<bean id="autoMobile" class="com.zz.spring.aop1.AutoMobile"></bean>
<!-- Advice -->
<bean id="carAdvice" class="com.zz.spring.aop1.CarAdvice"></bean>
<!-- AOP 配置 -->
<aop:config>
<aop:aspect ref="carAdvice">
<aop:pointcut expression="execution(* *.drive(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
Main函数
public class Main {
public static void main( String[] args ) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext1.xml");
Car car = (Car) ctx.getBean("autoMobile");
car.drive();
}
}
执行结果