當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring进行面向切面编程的一个简单例子
生活随笔
收集整理的這篇文章主要介紹了
Spring进行面向切面编程的一个简单例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
一、eclipse新建java項目取名SpringTest
?
二、導入sping包到構建路徑
還需要aspectjweaver.jar
三、創建java類(當然先要創建各種包)
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?自動生成的方法存根System.out.println(Hello);}@Overridepublic?void?sayChinaHello()?{//?TODO?自動生成的方法存根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("接下去調用sayHello()......");}@AfterReturning("hellopoint()")public?void?afterhello()?{System.out.println("函數sayHello()執行結束......");}/*Pointcut?for?sayChinaHello*/@Pointcut("execution(*?*.sayChinaHello())")public?void?helloChinapoint()?{}@Before("helloChinapoint()")public?void?beforehelloChina()?{System.out.println("接下去調用sayChinaHello()......");}@AfterReturning("helloChinapoint()")public?void?afterhelloChina()?{System.out.println("函數sayChinaHello()執行結束......");} }四、配置文件
在src目錄下創建resources目錄,并在其下建立hello-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="helloService"?class="com.zjptcc.wxw.spring.hello.HelloServiceImpl"><property?name="Hello"><value>Hello,china!</value></property><property?name="ChinaHello"><value>你好,中國歡迎您!</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>五、測試
測試程序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?自動生成的方法存根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();}}?
運行結果如下:
六、利用配置文件實現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("接下去,調用info()顯示人名......");}public?void?after_info()?{System.out.println("調用info()結束......");}public?void?after_get()?{System.out.println("調用getName()結束......");}}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>測試
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?自動生成的方法存根ctx?=?new?ClassPathXmlApplicationContext("resources/person-beans.xml");PersonService?p?=?(PersonService)?ctx.getBean("personService");p.info();System.out.println(p.getName());}}運行
參考:
Spring AOP 詳解
spring的JavaConfig方式及xml配置文件混用的例子
最最簡單的spring及AOP實例
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
轉載于:https://my.oschina.net/u/2245781/blog/597209
總結
以上是生活随笔為你收集整理的Spring进行面向切面编程的一个简单例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国家哀悼日将网站全部变成灰色的代码
- 下一篇: IOS scrollView 知识点