Spring Aop編程的demo

來源:互聯網
上載者:User

標籤:下一步   roles   get   對象   spec   exe   注意   isp   aop   


1: 建立一個普通的bean :Role 屬性 Id,name,添加無參構造,setter getter方法

2:建立一個介面:RoleService,隨便寫一個方法printRole

3:建立一個類RoleServiceImpl,實現RoleService介面,重寫printRole方法

注意 @Component 註解別忘了

4:定義切面類 RoleAspect 添加四個通知方法

注意: 添加@Aspect註解

execution中的參數一定要寫對,例:"execution(* aop.service.RoleServiceImpl.printRole( ..))"

下一步:配置bean

5:見證奇蹟的時刻,code測試代碼

結果:

6:總結

五個步驟:1 封裝pojo對象 
2 寫切點(介面和實作類別,在實作類別上加註解@Component)並重新 服務方法
3 寫切面類 @Aspect 標明 添加 涉及的 通知方法
4 切面配置類@EnableAspectJAutoProxy 使用spring的自動代理 @Configuration 標明類的作用@ComponentScan("aop") 掃描切點所在的包
5 通過ApplicationContext 擷取2中的服務,以構造器的形式注入方法中,再調用方法

Spring Aop編程的demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.