spring-aop(依赖,配置,实现)
布局
依赖项引入
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
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:context="http://www.springframework.org/schema/context"
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.xsd">
</bans>
userMapper
package com.a2003.dao;
public interface UserMapper {
void addUser();
void deleteUser();
void updateUser();
void selectUser();
}
UserMapperImpl
package com.a2003.dao.impl;
import com.a2003.dao.UserMapper;
public class UserMapperImpl implements UserMapper {
@Override
public void addUser() {
System.out.println("调用了addUser");
}
@Override
public void deleteUser() {
System.out.println("调用了daleteUser");
}
@Override
public void updateUser() {
System.out.println("调用了updateUser");
}
@Override
public void selectUser() {
System.out.println("调用了selectUser");
}
}
XmlAdvice(注解方法可用)
package com.a2003.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class XmlAdvice {
@Pointcut("execution(* com.a2003.dao.impl.UserMapperImpl.*(..))")
//前置通知
@Before("pointcut()")
void before(JoinPoint joinPoint){
System.out.println("这是前置控制");
System.out.println("目标:"+joinPoint.getTarget());
System.out.println("被植入增强处理的目标方法:"+joinPoint.getSignature());
}
//后置通知
@Before("pointcut()")
void after(){
System.out.println("后置通知!!!");
}
//环绕通知
@Around("pointcut()")
Object around(ProceedingJoinPoint point ){
Object object = null;
System.out.println("环绕之前的内容!!!");
try {
object = point.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
System.out.println("环绕之后的内容!!!");
return object;
}
//异常通知
@AfterThrowing("pointcut()")
void afterExpection(){
System.out.println("异常通知!!!");
}
//返回通知
@AfterReturning("pointcut()")
void afterReturning(JoinPoint joinPoint){
System.out.println("只是返回通知(方法不一场的时候使用)");
System.out.println("被植入增强处理的目标方法:"+joinPoint.getSignature());
}
}
MyTest
import com.a2003.dao.UserMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);
userMapper.selectUser();
}
}
applicationContext
<?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:context="http://www.springframework.org/schema/context"
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.xsd">
<bean id="userMapper" class="com.a2003.dao.impl.UserMapperImpl" > </bean>
<bean id="xmlAdvice" class="com.a2003.aspect.XmlAdvice"></bean>
<!--<aop:config proxy-target-class="true">
<!–切入点–>
<aop:pointcut id="pointcut" expression="execution(* com.a2003.dao.impl.UserMapperImpl.*(..))"/>
<!–指定切面 –>
<aop:aspect ref="xmlAdvice">
<aop:before method="before" pointcut-ref="pointcut"></aop:before><!–前置通知–>
<aop:after method="after" pointcut-ref="pointcut"></aop:after><!–后置通知–>
<aop:after-throwing method="afterExpection" pointcut-ref="pointcut"></aop:after-throwing><!–异常通知–>
<aop:around method="around" pointcut-ref="pointcut"></aop:around><!–环绕通知–>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut"></aop:after-returning><!–返回通知–>
</aop:aspect>
</aop:config>--><!--aop寻常方法实现-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy><!--注解方法实现-->
</beans>
运行