SpringAOP如何在java項(xiàng)目中使用

這篇文章將為大家詳細(xì)講解有關(guān)Spring AOP如何在java項(xiàng)目中使用 ,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供碧江網(wǎng)站建設(shè)、碧江做網(wǎng)站、碧江網(wǎng)站設(shè)計(jì)、碧江網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、碧江企業(yè)網(wǎng)站模板建站服務(wù),十余年碧江做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

一、什么是AOP

  AOP(Aspect Oriented Programming)面向切面編程不同于OOP(Object Oriented Programming)面向?qū)ο缶幊蹋珹OP是將程序的運(yùn)行看成一個(gè)流程切面,其中可以在切面中的點(diǎn)嵌入程序。

  舉個(gè)例子,有一個(gè)People類,也有一個(gè)Servant仆人類,在People吃飯之前,Servant會(huì)準(zhǔn)備飯,在People吃完飯之后,Servant會(huì)進(jìn)行打掃,這就是典型的面向切面編程.

  其流程圖為:

  Spring AOP如何在java項(xiàng)目中使用

二、Spring AOP實(shí)現(xiàn):

1、People類:

public class People {

 public void eat() {
 System.out.println(“happyheng開始吃飯啦");
 }

 public void play(){
 
 }
}

Servant類:

@Aspect
public class Servant {

 /**
 * 在吃飯之前
 */
 @Before("execution(** com.happyheng.entity.People.eat(..))")
 public void prepareFood(){
 System.out.println("準(zhǔn)備食物");
 }

 /**
 * 在吃飯之后
 */
 @After("execution(** com.happyheng.entity.People.eat(..))")
 public void clean(){
 System.out.println("打掃");
 }

}

其中的 @Before是指執(zhí)行前,@After是指執(zhí)行方法后獲取方法拋出異常后,@AfterReturning是指在執(zhí)行方法后調(diào)用,@AfterThrowing是指方法拋出異常后調(diào)用。

2、在applicationContext.xml中進(jìn)行配置:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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-3.0.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.happyheng" />
<aop:aspectj-autoproxy />
<!--注意Aspect的bean必須在Spring中注冊(cè),否則不會(huì)生效,Spring會(huì)用這個(gè)bean進(jìn)行攔截-->
<bean class="com.happyheng.aop.Servant"></bean>
<bean id="happyheng" class="com.happyheng.entity.People"></bean>
</beans>

3、在main中使用:

 public static void main(String[] args) {
 ApplicationContext ctx = new ClassPathXmlApplicationContext(APPLICATION_XML);
 
 People happyheng = (People)ctx.getBean("happyheng");
 happyheng.eat();
 }

關(guān)于Spring AOP如何在java項(xiàng)目中使用 就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

新聞標(biāo)題:SpringAOP如何在java項(xiàng)目中使用
文章鏈接:http://www.muchs.cn/article18/ihpidp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站全網(wǎng)營(yíng)銷推廣微信小程序、Google、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)