AOP
需要導入依賴:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
第一種方式
Spring xml配置方式實現
首先編寫我們的業務接口和實現類
public interface UserService {
public void add();
public void delete();
public void update();
public void search();
}
然后去寫我們的增強類 , 我們編寫兩個 , 一個前置增強 一個后置增強
public class Log implements MethodBeforeAdvice {
//method : 要執行的目標對象的方法
//objects : 被調用的方法的參數
//Object : 目標對象
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被執行了");
}
}
public class AfterLog implements AfterReturningAdvice {
//returnValue 返回值
//method被調用的方法
//args 被調用的方法的對象的參數
//target 被調用的目標對象
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("執行了" + target.getClass().getName()
+"的"+method.getName()+"方法,"
+"返回值:"+returnValue);
}
}
最后去spring的文件中注冊 , 並實現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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注冊bean-->
<bean id="userService" class="com.kuang.service.UserServiceImpl"/>
<bean id="log" class="com.kuang.log.Log"/>
<bean id="afterLog" class="com.kuang.log.AfterLog"/>
<!--aop的配置-->
<aop:config>
<!--切入點 expression:表達式匹配要執行的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--執行環繞; advice-ref執行方法 . pointcut-ref切入點-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
測試
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.search();
}
}
Aop的重要性 : 很重要 . 一定要理解其中的思路 , 主要是思想的理解這一塊 .
Spring的Aop就是將公共的業務 (日志 , 安全等) 和領域業務結合起來 , 當執行領域業務時 , 將會把公共業務加進來 . 實現公共業務的重復利用 . 領域業務更純粹 , 程序猿專注領域業務 , 其本質還是動態代理
方式二:
自定義類實現AOP
第一步 : 寫我們自己的一個切入類
public class DiyPointcut {
public void before(){
System.out.println("---------方法執行前---------");
}
public void after(){
System.out.println("---------方法執行后---------");
}
}
去spring中配置
<!--第二種方式自定義實現-->
<!--注冊bean-->
<bean id="diy" class="com.kuang.config.DiyPointcut"/>
<!--aop的配置-->
<aop:config>
<!--第二種方式:使用AOP的標簽實現-->
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<aop:before pointcut-ref="diyPonitcut" method="before"/>
<aop:after pointcut-ref="diyPonitcut" method="after"/>
</aop:aspect>
</aop:config>
第三種方式:
使用注解實現
@EnableAspectJAutoProxy //等同於在配置文件中配置aop:aspectj-autoproxy/ // 開啟spring對注解AOP的支持
@ComponentScan("aop") // 掃描指定的包,讓包下的注解生效
第一步:編寫一個注解實現的增強類
@Aspect // 聲明只是一個切面
@Component // 注入到容器中
public class AnnotationPointcut {
@Before("execution(* service.UserServiceImpl.*(..))")
public void before(){
System.out.println("---------方法執行前---------");
}
@After("execution(* service.UserServiceImpl.*(..))")
public void after(){
System.out.println("---------方法執行后---------");
}
@Around("execution(* service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("環繞前");
System.out.println("簽名:"+jp.getSignature());
//執行目標方法proceed
Object proceed = jp.proceed();
System.out.println("環繞后");
}
}
第二步:在Spring配置文件中,注冊bean,並增加支持注解的配置
<!--第三種方式:注解實現-->
<bean id="annotationPointcut" class="com.kuang.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>
使用配置類,注冊bean,並增加支持注解的配置
@Configuration
@EnableAspectJAutoProxy // 開啟aop的注解支持
@ComponentScan("aop") // 掃描指定的包,讓包下的注解生效
public class AopConfiguration {
@Bean("userService")
public UserServiceImpl createUserService(){
return new UserServiceImpl();
}
}