這篇文章主要講解了“AOP的原理是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“AOP的原理是什么”吧!
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),寧武企業(yè)網(wǎng)站建設(shè),寧武品牌網(wǎng)站建設(shè),網(wǎng)站定制,寧武網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,寧武網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
AOP的原理
對(duì)哪些對(duì)象在什么位置攔截做什么 <=> <aop:before method="before" pointcut-ref="txPointcut" />
哪些對(duì)象
<!-- 定義切面,所有的service的所有方法 -->
<aop:pointcut id="txPointcut" expression="execution(* com.masterslave.service.*.*(..))" />
dataSourceAspect 是切面要攔截什么。aop:before就是在攔截對(duì)象的前面位置。method="before"就是使用切面中的方法處理攔截
85 <aop:aspect ref="dataSourceAspect" order="-9999">
86 <aop:before method="before" pointcut-ref="txPointcut" />
87 </aop:aspect>
如上是用配置文件配置的
還有方法是直接用注解方式
注解中也說明了,攔截范圍@Pointcut,在什么位置@Before,做什么@Aspect,具體攔截對(duì)象是誰JoinPoint
@Aspect
public class LoggingAspect {
// @Pointcut("execution(* com.samsung.sdsc.legal..*Impl.*(..)) || execution(* com.samsung.sdsc.legal..*Action.*(..))")
@Pointcut("execution(* com.samsung.sdsc.legal..*Impl.*(..))")
public void serviceMethod() {
}
@Before("serviceMethod()")
public void beforeLogging(JoinPoint thisJoinPoint) {
Class<? extends Object> clazz = thisJoinPoint.getTarget().getClass();
Logger logger = Logger.getLogger(clazz);
String methodName = thisJoinPoint.getSignature().getName();
Object[] arguments = thisJoinPoint.getArgs();
StringBuffer argBuf = new StringBuffer();
StringBuffer argValueBuf = new StringBuffer();
int i = 0;
for (Object argument : arguments) {
String argClassName = "";
if (null == argument) {
argClassName = "Null";
argument = "";
} else {
argClassName = argument.getClass().getSimpleName();
}
if (i > 0) {
argBuf.append(", ");
}
argBuf.append(argClassName + " arg" + ++i);
argValueBuf.append(".arg" + i + " : " + argument.toString() + "\n");
}
if (i == 0) {
argValueBuf.append("No arguments\n");
}
StringBuffer messageBuf = new StringBuffer();
messageBuf.append("before executing " + methodName + "("
+ argBuf.toString() + ") method");
messageBuf
.append("\n-------------------------------------------------------------------------------\n");
messageBuf.append(argValueBuf.toString());
messageBuf
.append("-------------------------------------------------------------------------------");
logger.info(messageBuf);
}
}
感謝各位的閱讀,以上就是“AOP的原理是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)AOP的原理是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站標(biāo)題:AOP的原理是什么
文章來源:http://www.chinadenli.net/article24/gejeje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、營銷型網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容