日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring进行面向切面编程的一个简单例子

發(fā)布時(shí)間:2025/3/17 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring进行面向切面编程的一个简单例子 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

一、eclipse新建java項(xiàng)目取名SpringTest

?

二、導(dǎo)入sping包到構(gòu)建路徑

還需要aspectjweaver.jar

三、創(chuàng)建java類(當(dāng)然先要?jiǎng)?chuàng)建各種包)

IHelloService.java

package?com.zjptcc.wxw.spring.hello;public?interface?IHelloService?{public?void?sayHello();public?void?sayChinaHello();}

HelloServiceImpl.java

package?com.zjptcc.wxw.spring.hello;public?class?HelloServiceImpl?implements?IHelloService?{private?String?Hello;private?String?ChinaHello;@Overridepublic?void?sayHello()?{//?TODO?自動(dòng)生成的方法存根System.out.println(Hello);}@Overridepublic?void?sayChinaHello()?{//?TODO?自動(dòng)生成的方法存根System.out.println(ChinaHello);}public?String?getHello()?{return?Hello;}public?void?setHello(String?hello)?{Hello?=?hello;}public?String?getChinaHello()?{return?ChinaHello;}public?void?setChinaHello(String?chinaHello)?{ChinaHello?=?chinaHello;}}

SimpleHelloBean.java

package?com.zjptcc.wxw.spring.hello;public?class?SimpleHelloBean?{public?void?sayHello(){System.out.println("Hello?World!");} }

?

AopTest.java

package?com.zjptcc.wxw.spring.hello;import?org.aspectj.lang.annotation.AfterReturning; import?org.aspectj.lang.annotation.Aspect; import?org.aspectj.lang.annotation.Before; import?org.aspectj.lang.annotation.Pointcut;@Aspect public?class?AopTest?{/*Pointcut?for?sayHello*/@Pointcut("execution(*?*.sayHello())")public?void?hellopoint()?{}@Before("hellopoint()")public?void?beforehello()?{System.out.println("接下去調(diào)用sayHello()......");}@AfterReturning("hellopoint()")public?void?afterhello()?{System.out.println("函數(shù)sayHello()執(zhí)行結(jié)束......");}/*Pointcut?for?sayChinaHello*/@Pointcut("execution(*?*.sayChinaHello())")public?void?helloChinapoint()?{}@Before("helloChinapoint()")public?void?beforehelloChina()?{System.out.println("接下去調(diào)用sayChinaHello()......");}@AfterReturning("helloChinapoint()")public?void?afterhelloChina()?{System.out.println("函數(shù)sayChinaHello()執(zhí)行結(jié)束......");} }

四、配置文件

在src目錄下創(chuàng)建resources目錄,并在其下建立hello-beans.xml內(nèi)容如下:

<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"?xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:annotation-config/>?<aop:aspectj-autoproxy?/><bean?id="helloService"?class="com.zjptcc.wxw.spring.hello.HelloServiceImpl"><property?name="Hello"><value>Hello,china!</value></property><property?name="ChinaHello"><value>你好,中國(guó)歡迎您!</value></property></bean><bean?id="SimpleHelloBean"?class="com.zjptcc.wxw.spring.hello.SimpleHelloBean"></bean><bean?id="aopTest"?class="com.zjptcc.wxw.spring.hello.AopTest"?/></beans>

五、測(cè)試

測(cè)試程序TestHelloService.java如下:

package?com.zjptcc.wxw.spring.test;import?org.springframework.context.ApplicationContext; import?org.springframework.context.support.ClassPathXmlApplicationContext;import?com.zjptcc.wxw.spring.hello.IHelloService; import?com.zjptcc.wxw.spring.hello.SimpleHelloBean;public?class?TestHelloService?{/***?@param?args*/private?static?ApplicationContext?ctx;public?static?void?main(String[]?args)?{//?TODO?自動(dòng)生成的方法存根ctx?=?new?ClassPathXmlApplicationContext("resources/hello-beans.xml");//用接口IHelloService?helloWorld?=?(IHelloService)?ctx.getBean("helloService");helloWorld.sayHello();helloWorld.sayChinaHello();System.out.println("------------------------------------------------------------------------------------");//用類SimpleHelloBean?SimpleHello?=?(SimpleHelloBean)?ctx.getBean("SimpleHelloBean");SimpleHello.sayHello();}}

?

運(yùn)行結(jié)果如下:

六、利用配置文件實(shí)現(xiàn)AOP

java類

package?com.zjptcc.wxw.spring.person; public?class?PersonService?{private?String?name;public?void?setName(String?name)?{this.name?=?name;}public?void?info(){System.out.println("此人姓名為:"+name);}public?String?getName(){return?name;} } package?com.zjptcc.wxw.spring.person;public?class?PersonAop?{public?void?before_info()?{System.out.println("接下去,調(diào)用info()顯示人名......");}public?void?after_info()?{System.out.println("調(diào)用info()結(jié)束......");}public?void?after_get()?{System.out.println("調(diào)用getName()結(jié)束......");}}

resources/person-beans.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"?xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:annotation-config?/><aop:aspectj-autoproxy?/><bean?id="personService"?class="com.zjptcc.wxw.spring.person.PersonService"><property?name="name"><value>露茜</value></property></bean><!--定義切面?--><bean?id="personaop"?class="com.zjptcc.wxw.spring.person.PersonAop"?/><aop:config><aop:aspect?ref="personaop"><aop:pointcut?id="infopoint"?expression="execution(*?*.info(..))"?/><aop:before?pointcut-ref="infopoint"?method="before_info"?/><aop:after?pointcut-ref="infopoint"?method="after_info"?/></aop:aspect><aop:aspect?ref="personaop"><aop:pointcut?id="getpoint"?expression="execution(*?*.getName(..))"?/><aop:after?pointcut-ref="getpoint"?method="after_get"?/></aop:aspect></aop:config></beans>

測(cè)試

package?com.zjptcc.wxw.spring.test;import?org.springframework.context.ApplicationContext; import?org.springframework.context.support.ClassPathXmlApplicationContext;import?com.zjptcc.wxw.spring.person.PersonService;public?class?TestPersonService?{/***?@param?args*/private?static?ApplicationContext?ctx;public?static?void?main(String[]?args)?{//?TODO?自動(dòng)生成的方法存根ctx?=?new?ClassPathXmlApplicationContext("resources/person-beans.xml");PersonService?p?=?(PersonService)?ctx.getBean("personService");p.info();System.out.println(p.getName());}}

運(yùn)行

參考:

Spring AOP 詳解

spring的JavaConfig方式及xml配置文件混用的例子

最最簡(jiǎn)單的spring及AOP實(shí)例

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

轉(zhuǎn)載于:https://my.oschina.net/u/2245781/blog/597209

總結(jié)

以上是生活随笔為你收集整理的Spring进行面向切面编程的一个简单例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。