【Spring】- Spring AOP Advice通知
Audience.java:觀眾:通知類
package com.zhiwei.pojo; public class Audience { public void takeSeat(){ System.out.println("MethodBeforeAdvice:Take Seat!"); } public void turnOffPhone(){ System.out.println("MethodBeforeAdvice:turn Off Phone!"); } /* * xml配置返回值 */ public void applaud(Object res){ System.out.println("AfterReturning:applaud,returning="+res); } public void demandRefund(Object e){ System.out.println("AfterThrowing:demandRefund,Exception="+e); } public void goHone(){ System.out.println("After:go Home!"); } }
Singer:歌手:目標類
package com.zhiwei.pojo;
//演員:歌手:目標物件
public class Singer implements Performer {
@Override
public void show() {
System.out.println("targetMethod:I am a Singer!");
}
@Override
public String showName() {
return "Job:Singer!";
}
}
Performer:表演者:介面定義Singer的屬性和方法
package com.zhiwei.pojo;
public interface Performer {
public void show();
public String showName();
}
配置:
<?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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 配置目標物件:切面 --> <bean id="singer" class="com.zhiwei.pojo.Singer" /> <!-- 配置通知 --> <bean id="audience" class="com.zhiwei.pojo.Audience" /> <!-- 通過配置xml將pojo暴露成切面 --> <aop:config> <aop:aspect ref="audience"><!-- 配置切面 --> <aop:pointcut expression="execution(* com.zhiwei.pojo.Performer.showName())" id="advice"/><!-- 配置切入點 --> <aop:before method="takeSeat" pointcut-ref="advice"/><!-- 配置通知 --> <aop:before method="turnOffPhone" pointcut-ref="advice"/><!-- 配置方法 --> <!-- 接收返回值:res,必須和通知方法的形參名一致 --> <!--after-returning:目標方法返回值之後執行 --> <aop:after-returning method="applaud" pointcut-ref="advice" returning="res"/> <aop:after-throwing method="demandRefund" pointcut-ref="advice" throwing="e"/> <!-- 最終執行 --> <aop:after method="goHone" pointcut-ref="advice"/> </aop:aspect> </aop:config> </beans>
測試類:
package com.zhiwei.pojo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestPojo {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("com/zhiwei/pojo/applicationContext.xml");
Performer singer= (Performer) ac.getBean("singer");
singer.showName();;
}
}
結果:
相關推薦
【Spring】- Spring AOP Advice通知
浪費了“黃金五年”的Java程式設計師,還有救嗎? >>>
【redis】spring boot利用redis的Keyspace Notifications實現消息通知
客戶 無效 handler mage extend width psu 消息通知 queue 前言 需求:當redis中的某個key失效的時候,把失效時的value寫入數據庫。 github: https://github.com/vergilyn/RedisSampl
【轉載】Spring AOP詳解 、 JDK動態代理、CGLib動態代理
rto 工廠 第一個 lec 僅支持 sel clas sleep gpo 原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspect Oriented Programing的簡稱,面向切面
【Java】 Spring 框架初步學習總結(一)簡單實現 IoC 和 AOP
1.0 其中 表示 只需要 第一篇 否則 info fin pojo Spring 是一個開源的設計層面的輕量級框架,Spring 的好處網上有太多,這裏就不在贅述。 IoC 控制反轉和 AOP 面向切面編程是 Spring 的兩個重要特性。 IoC(Inver
【小家Spring】Spring AOP的多種使用方式以及神一樣的AspectJ-AOP使用介紹
相關閱讀 【小家java】java5新特性(簡述十大新特性) 重要一躍 【小家java】java6新特性(簡述十大新特性) 雞肋升級 【小家java】java7新特性(簡述八大新特性) 不溫不火 【小家java】java8新特性(簡述十大新特性) 飽受讚譽 【小家java】java9
【spring】Spring aop的實現原理
本文轉載自https://www.cnblogs.com/lcngu/p/5339555.html。 Spring aop的實現原理 簡介 前段時間寫的java設計模式--代理模式,最近在看Spring Aop的時候,覺得於代理模式應該有密切的聯絡,於是決定了解下Sprin
【Spring】:aop的實現原理
銜接前篇文章:https://blog.csdn.net/hxcaifly/article/details/85061330 前言 前段時間寫的java設計模式–代理模式,最近在看Spring Aop的時候,覺得於代理模式應該有密切的聯絡,於是決定了解下Spring A
【框架】[Spring]AOP攔截-三種方式實現自動代理
這裡的自動代理,我講的是自動代理bean物件,其實就是在xml中讓我們不用配置代理工廠,也就是不用配置class為org.springframework.aop.framework.ProxyFactoryBean的bean。 總結了一下自己目前所學的知識
【轉載】Spring AOP
6.3.7. 例子 讓我們來看看在 Section 6.2.7, “例子” 提過併發鎖失敗重試的例子,如果使用schema對這個例子進行重寫是什麼效果。 因為併發鎖的關係,有時候business services可能會失敗(例如,死鎖失敗)。 如果重新嘗試一下,很有可能就會
【知識庫】--spring aop 動態代理--inner private protected 方法失效(212)
私有方法或者保護的方法無效! AspectJ pointcut for annotated PRIVATE methods Due to the proxy-based nature of Spring's AOP framework, protected methods
【原創】Spring-AOP代理類繼承介面..
Computer.java package org.rockie; public class Computer implements PcInterf{ private String pcName="rockie007"; private int pcPrice=5000;
【redis】spring boot利用redis的Keyspace Notifications實現訊息通知
前言 需求:當redis中的某個key失效的時候,更新key對應資料在資料庫的狀態 1、修改redis.co
【Spring】Spring MVC原理及配置詳解
進行 return sub sca scrip uil 線程安全 松耦合 必須 1.Spring MVC概述: Spring MVC是Spring提供的一個強大而靈活的web框架。借助於註解,Spring MVC提供了幾乎是POJO的開發模式,使得控制器的開發和測試更加簡
【轉】Spring boot 打成jar包問題總結
alt date function get .post stack 問題 chan property http://www.cnblogs.com/xingzc/p/5972488.html 1、Unable to find a single main class
【轉】Spring MVC系列(五)之自定義數據綁定---HandlerMethodArgumentResolver
開閉 src pat 獲取參數 mvc .net 定義 開閉原則 淺析 介紹 前面幾節我們介紹了Spring MVC的幾種常見的數據綁定的方法,可以靈活地獲取用戶請求中的參數,例如@PathVariable,@ModelAttribute,@RequestPar
【spring】spring boot的配置文件application.yml
odi ini druid convert use pac map location leg spring: datasource:# 本地庫 url: jdbc:mysql://192.168.18.7:3306/shiptrackersmart?useUni
【spring】Spring Boot:定制自己的starter
cat 交互 miss factor 如何 指定 boot.s 倉庫 dmi 概念 在學習Spring Boot的過程中,接觸最多的就是starter。可以認為starter是一種服務——使得使用某個功能的開發者不需要關註各種依賴庫的處理,不需要具體的配置信息,由
【MVC】Spring MVC常用配置
solver errors 自動裝箱 property byte mco 驗證 c基礎 form表單 一、SpringMVC基礎入門,創建一個HelloWorld程序 1.首先,導入SpringMVC需要的jar包。 2.添加Web.xml配置文件中關於Spring
【java】spring項目中 對entity進行本類間的克隆
tor mini cti false display des private rac 重寫 方法1: 【使用spring自帶BeanUtils實現克隆】 【要求:需要被克隆的類實現Cloneable接口並且重寫clone()方法】 》例子: 》》實體: package
【轉】spring cloud eureka 參數配置
lint 客戶 就會 大於 health 微服務 表示 安全 穩定 eureka.client.registry-fetch-interval-seconds 表示eureka client間隔多久去拉取服務註冊信息,默認為30秒,對於api-gateway,如果要迅速獲