訊息稱臺積電 3nm 量產獲重大突破,今年 8 月投片
阿新 • • 發佈:2022-04-12
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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"View Code> <!-- 用到的都先註冊上--> <bean id="userService" class="com.ljm.service.UserService_"/> <bean id="log" class="com.ljm.log.Log"/> <bean id="afterLog" class="com.ljm.log.Afterlog"/> <!-- 配置aop 需要匯入aop 的約束--> <aop:config> <!-- 切入點[可多個](在哪裡執行) expression是個表示式(格式固定) execution(要執行的位置) 任意訪問修飾符 類名 方法名(.*) 引數(..)--> <aop:pointcut id="pointcut" expression="execution(* com.ljm.service.UserService_.*(..))"/> <aop:pointcut id="pointcut1" expression="execution(* com.ljm.service.UserService_.add(..))"/> <!-- 執行環繞增加 把log類切入到pointcut --> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut1"/> </aop:config> </beans>
介面(略)
實現類(略)
前置
public class Log implements MethodBeforeAdvice { @Override //method 需要執行目標物件的方法 //objects 相關引數 ==args //o 目標物件 ==target //自動呼叫 public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("前置 "+o.getClass().getName()+" 的 "+method.getName()+" 方法正在執行 "); } }View Code
後置
public class Afterlog implements AfterReturningAdvice { @Override //returenValue 返回值 //method 需要執行目標物件的方法 //objects 相關引數 ==args //o 目標物件 ==target public void afterReturning(Object returenValue, Method method, Object[] objects, Object o1) throws Throwable { System.out.println(" after 執行了"+method.getName()+"方法 返回的結果為"+returenValue); } }View Code
測試
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //動態代理的是介面 UserService是介面 UserService userService = (UserService)context.getBean("userService"); userService.add(); userService.delete(); }View Code
方法2:自定義類(視訊推薦)
相對簡單,但功能有限
public class DiyPointCut { public void before(){ System.out.println("=====前面執行==="); } public void after(){ System.out.println("=====後面執行==="); } }View Code
XML相關
<bean id="diy" class="com.ljm.diy.DiyPointCut"/> <aop:config> <!-- 自定義切面 red需要引入的類--> <aop:aspect ref="diy"> <!-- 切入點--> <aop:pointcut id="point" expression="execution(* com.ljm.service.UserService_.*(..))"/> <!-- 通知 使用的方法是DiyPointCut中的的before 在切入點point處生效--> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config>View Code
方法3:使用註解實現(除錯程式碼不變)
在pom中匯入依賴
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.4</version> </dependency>View Code
自定義相關類
@Aspect public class AnotationPointCut { @Before("execution(* com.ljm.service.UserService_.*(..))") public void before(){ System.out.println("方法執行前-註解實現,Before"); } @After("execution(* com.ljm.service.UserService_.*(..))") public void after(){ System.out.println("方法執行後-註解實現,After"); } //在環繞增強中,可以給個引數,代表獲取處理的點 @Around("execution(* com.ljm.service.UserService_.*(..))") public void around(ProceedingJoinPoint jp){ System.out.println("環繞前"); // jp.getSignature(); System.out.println("簽名=執行方法="+jp.getSignature()); try { Object proceed = jp.proceed(); //執行方法 } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("環繞後"); } }View Code
配置XML中
<!-- 方式三--> <bean id="diy01" class="com.ljm.diy.AnotationPointCut"/> <!-- 開啟註解支援 JDK(預設false) true的話就用cglib實現 --> <aop:aspectj-autoproxy proxy-target-class="false"/>View Code
執行順序