編寫第二個Spring程序——AOP實現

第二個Spring程序 AOP范例

1、新建maven工程
2、在pom.xml文件導入相關jar包
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.6.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version><scope>runtime</scope></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.6</version></dependency>
3、在 Packge【service】下創建 【ProductService】類:
package service;public class ProductService {public void doSomeService(){System.out.println("doSomeService");}
}
4、在 xml 文件中裝配該 bean:
<bean name="productService" class="service.ProductService" />
5、在【TestSpring】中編寫測試代碼,運行:
package test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Source;
import service.ProductService;public class TestSpring {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});ProductService productService = (ProductService) context.getBean("productService");productService.doSomeService();}
}

運行結果

doSomeService
6、在 Packge【aspect】下準備日志切面 【LoggerAspect】類:
package aspect;import org.aspectj.lang.ProceedingJoinPoint;public class LoggerAspect {public Object log(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("start log:" + joinPoint.getSignature().getName());Object object = joinPoint.proceed();System.out.println("end log:" + joinPoint.getSignature().getName());return object;}
}
7、在 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"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean name="productService" class="service.ProductService" /><bean id="loggerAspect" class="aspect.LoggerAspect"/><!-- 配置AOP --><aop:config><!-- where:在哪些地方(包.類.方法)做增加 --><aop:pointcut id="loggerCutpoint"expression="execution(* service.ProductService.*(..)) "/><!-- what:做什么增強 --><aop:aspect id="logAspect" ref="loggerAspect"><!-- when:在什么時機(方法前/后/前后) --><aop:around pointcut-ref="loggerCutpoint" method="log"/></aop:aspect></aop:config>
</beans>
8、再次運行 TestSpring 中的測試代碼,代碼并沒有改變,但是在業務方法運行之前和運行之后,都分別輸出了日志信息:
start log:doSomeService
doSomeService
end log:doSomeService

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/535650.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/535650.shtml
英文地址,請注明出處:http://en.pswp.cn/news/535650.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux高負載下徹底優化mysql數據庫

同時在線訪問量繼續增大 對于1G內存的服務器明顯感覺到吃力嚴重時甚至每天都會死機 或者時不時的服務器卡一下 這個問題曾經困擾了我半個多月MySQL使用是很具伸縮性的算法&#xff0c;因此你通常能用很少的內存運行或給MySQL更多的被存以得到更好的性能。 安裝好mysql后&#x…

Java注釋說明以及IDEA中的快捷鍵

一、單行注釋 說明&#xff1a;單行注釋 一般注釋少量的代碼或者說明內容 格式&#xff1a;//注釋的內容 IDEA中的快捷鍵&#xff1a;使用Ctrl /&#xff0c; 添加行注釋&#xff0c;再次使用&#xff0c;去掉行注釋 二、多行注釋 說明&#xff1a;多行注釋 一般注釋大量的…

redhat系統雙網卡綁定

Redhat Linux的網絡配置&#xff0c;基本上是通過修改幾個配置文件來實現的&#xff0c;雖然也可以用ifconfig來設置IP&#xff0c;用route來配置默認網關&#xff0c;用hostname來配置主機名&#xff0c;但是重啟后會丟失。 1.相關的配置文件: /ect/hosts 配置主機名和IP地址…

JDK源碼解析之java.util.Iterator和java.lang.Iterable

在Java中&#xff0c;我們可以對List集合進行如下幾種方式的遍歷&#xff1a;第一種就是普通的for循環&#xff0c;第二種為迭代器遍歷&#xff0c;第三種是for each循環。后面兩種方式涉及到Java中的iterator和iterable對象&#xff0c;接下來我們通過源碼來看看這兩個對象的區…

為了讓你的網頁能在更多的服務器上正常地顯示,還是加上“SET NAMES UTF8”吧

Repinted:http://blog.csdn.net/class1/archive/2006/12/30/1469298.aspx 為了讓你的網頁能在更多的服務器上正常地顯示&#xff0c;還是加上“SET NAMES UTF8”吧(可以根據你的喜歡選擇相應的編碼,如gb2312)&#xff0c;即使你現在沒有加上這句也能正常訪問。 先說MySQL的字…

WebLogic11g 安裝配置規范

目錄 1 文檔控制... 3 1.1 修改記錄... 3 1.2 分發者... 3 1.3 審閱記錄... 3 1.4 相關文檔... 3 2 安裝準備... 4 2.1 安裝前需要開發單位提供的信息... 4 2.2 本地磁盤空間配置規范... 4 2.3 版本要求規范... 4 2.4 weblogic部署配置規范... 5 2.4.1操作系統要求.…

JDK源碼解析之java.util.ListIterator

ListIterator是一個功能更加強大的迭代器接口, 它繼承于Iterator接口,只能用于各種List類型的訪問。可以通過調用listIterator()方法產生一個指向List開始處的ListIterator, 還可以調用listIterator(n)方法創建一個一開始就指向列表索引為n的元素處的ListIterator。 一、源碼解…

VsFTP出現500 OOPS: cannot change directory的解決辦法

cannot change directory:/home/*** ftp服務器連接失敗,錯誤提示:500 OOPS: cannot change directory:/home/*******500 OOPS: child died解決方法:在終端輸入命令&#xff1a;setsebool ftpd_disable_trans 1 service vsftpd restart就&#xff2f;&#xff2b;了&#xff01;…

Oracle的reman命令

list命令&#xff1a; list backupset summary 列出概要信息 list backupset by file list archivelog all 列出所有歸檔日志 list backupset tag 00列出標簽信息 list backupset 8 列出8號…

Ubuntu root賬號的使用

第一次安裝好Ubuntu后&#xff0c;root帳號不能用。在安裝期間創建的第一個用戶對系統有管理權&#xff0c;通過“sudo”能象root運行程序.使用時僅需它的普通用戶密碼。例如: sudo apt-get update  如果你希望像傳統 UNIX 樣式使用root帳號。你能通過輸入 sudo passwd root …

JDK源碼解析之Java.util.Collection

Collection是單例集合的頂層接口&#xff0c;它表示一組對象&#xff0c;這些對象也稱為Collection的元素&#xff0c;JDK 不提供此接口的任何直接實現&#xff0c;它提供更具體的子接口&#xff08;如Set和List&#xff09;實現 一、源碼解析 1、接口定義 public interface …

Vim 命令操作

vim命令操作命令模式dd 編輯模式 末行模式 1.地址定界&#xff1a; startpos,endpos #:特定的第#行&#xff0c;例如S即第5行;:當前行;$:最后一行; #,#:指定行范圃,左側起始行&#xff0…

JDK源碼解析之Java.util.Collections

java.util.Collections 是一個包裝類。它包含有各種有關集合操作的靜態多態方法。此類不能實例化&#xff0c;就像一個工具類,服務于Java的Collection框架。 一、源碼解析 1、不可實例化 private Collections() {}Collections是util包中一個不可實例化的類。 2、優化參數 pri…

ubuntu下安裝jdk

安裝1.5 sudo apt-get install sun-java5-jdk sudo update-alternatives --config java sudo update-alternatives --config javac 安裝1.6 sudo apt-get install sun-java6-jdk sudo update-alternatives --config java sudo update-alternatives --config javac 轉載:http:/…

使用validate驗證數據庫

驗證數據備份集是不是可以用來做恢復和數據文件是否損壞、壞塊 三種方式&#xff1a; 1.validate validate database ;validate tablespace users; validate datafile 1; validate archivelog all validate datafile 1 block 10; validate backupset 28; db…

JDK源碼解析之java.util.AbstractCollection

AbstractCollection類提供了collection的實現類應該具有的基本方法&#xff0c;具有一定的普適性&#xff0c;可以從大局上了解collection實現類的主要功能。 java.util.AbstractCollection這個類提供了對接口Collection骨骼級的實現。 一、源碼解析 1、iterator():返回一個迭…

溝通linux與windows的wine

據Netcraft網站調查&#xff0c;現在互聯網上的主機有75&#xff05;以上采用Linux作為操作系統。作為服務器操作系統&#xff0c;Linux已經站穩了腳步&#xff0c;可是在桌面 操作系統上&#xff0c;還是微軟的“瘟到死”一支獨秀。這倒不是說Linux不好&#xff0c;很大原因我…

備份spfil、控制文件等

delete backup&#xff1b; delete backupset delete noprompt backup backup keep forver database 永久保存恢復目錄中支持此命令 show parameter control 備份spfile backup spfile backup current contrlfile configure controlfile autoback …

日常問題——阿里云服務器ssh經常一段時間就斷掉解決辦法

#vim /etc/ssh/sshd_config 找到下面兩行 #ClientAliveInterval 0 #ClientAliveCountMax 3 去掉注釋&#xff0c;改成 ClientAliveInterval 30 ClientAliveCountMax 86400 這兩行的意思分別是 1、客戶端每隔多少秒向服務發送一個心跳數據 2、客戶端多少秒沒有相應&#…

在Ubuntu 8.04 LTS(hardy)下安裝配置nginx和fastcgi方式的php

最近我們(瑞豪開源Xen VPS: http://www.RasHost.com)的一個客戶要求在他的Ubuntu 8.04 VPS上安裝一個高性能的nginx&#xff0c;下面是我的安裝記錄。 由于Ubuntu 804已經包含了nginx&#xff0c;所以根本不要編譯&#xff0c;安裝超簡單&#xff01; 在VPS上修改/etc/apt/so…