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

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

生活随笔

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

javascript

Spring 从零開始-05

發(fā)布時(shí)間:2024/4/17 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 从零開始-05 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最終能到Spring的AOP編程了,AOP的概念特別的多。所以須要你在開(kāi)始之前有點(diǎn)了解,然后通過(guò)代碼慢慢學(xué)習(xí)!
— 切面(Aspect):一個(gè)關(guān)注點(diǎn)的模塊化,這個(gè)關(guān)注點(diǎn)實(shí)現(xiàn)可能另外橫切多個(gè)對(duì)象。事務(wù)管理是J2EE應(yīng)用中一個(gè)非常好的橫切關(guān)注點(diǎn)樣例。方面用Spring的Advisor或攔截器實(shí)現(xiàn)。


— 連接點(diǎn)(Joinpoint):程序運(yùn)行過(guò)程中明白的點(diǎn),如方法的調(diào)用或特定的異常被拋出。


— 通知(Advice):在特定的連接點(diǎn),AOP框架運(yùn)行的動(dòng)作。

各種類型的通知包含“around”、“before”和“throws”通知。通知類型將在以下討論。

很多AOP框架包含Spring都是以攔截器做通知模型。維護(hù)一個(gè)“圍繞”連接點(diǎn)的攔截器鏈。
— 切入點(diǎn)(Pointcut):指定一個(gè)通知將被引發(fā)的一系列連接點(diǎn)的集合。AOP框架必須同意開(kāi)發(fā)人員指定切入點(diǎn)。比如。使用正則表達(dá)式。


— 引入(Introduction):加入方法或字段到被通知的類。Spring同意引入新的接口到不論什么被通知的對(duì)象。

比如,你能夠使用一個(gè)引入使不論什么對(duì)象實(shí)現(xiàn)IsModified接口,來(lái)簡(jiǎn)化緩存。
— 目標(biāo)對(duì)象(Target Object):包含連接點(diǎn)的對(duì)象,也被稱作被通知或被代理對(duì)象。


— AOP代理(AOP Proxy):AOP框架創(chuàng)建的對(duì)象,包含通知。在Spring中。AOP代理能夠是JDK動(dòng)態(tài)代理或CGLIB代理。


— 織入(Weaving):組裝方面來(lái)創(chuàng)建一個(gè)被通知對(duì)象。這能夠在編譯時(shí)完畢(比如使用AspectJ編譯器)。也能夠在運(yùn)行時(shí)完畢。

Spring和其它純Java AOP框架一樣,在運(yùn)行時(shí)完畢織入。


頭非常暈是不是,沒(méi)關(guān)系。先記住,然后說(shuō)說(shuō)為什么使用AOP編程,AOP-Aspect Oriented Programming面向切面編程,既然是編程,那么就說(shuō)明Spring僅僅是AOP的一種實(shí)現(xiàn)方式。

舉個(gè)樣例,比方你買了房子是不是每一個(gè)月都要交電費(fèi),那你是不是這樣實(shí)現(xiàn)

public class House{基礎(chǔ)方法();交電費(fèi)();交水費(fèi)();防小偷();... }

這樣買個(gè)房子的代碼竟然須要關(guān)心非常多其它方法,并且基本上與你正常的業(yè)務(wù)無(wú)關(guān),假設(shè)哪天又添加了其它的功能,是不是又要改代碼。所以我們希望將多余的方法拿出,在須要的時(shí)候再織入。

Spring就給我們提供了這種功能。


Spring對(duì)AOP的支持能夠使用xml編寫(xiě)、使用注解還有AspectJ(當(dāng)然還有經(jīng)典的使用代理,這個(gè)方案比較復(fù)雜所以就不說(shuō)了)。
先說(shuō)xml

public class Check {public void CheckSecurity(){System.out.println("----checkSecurity----");} }

上面的代碼就是我們須要織入的代碼

public interface TestInter {public void show();public void hh(); } public class TestService implements TestInter{private String name;@Overridepublic void show() {// TODO Auto-generated method stubSystem.out.println(name);}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic void hh() {// TODO Auto-generated method stub}} public class app1 {public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ac = new ClassPathXmlApplicationContext("com/aop/aop.xml");TestInter testservice = (TestInter) ac.getBean("testsetvice1");testservice.show();}} <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.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="testsetvice1" class="com.aop.TestService" ><property name="name"><value>spirit</value></property></bean><bean id="check" class="com.aop.Check"/><aop:config><aop:aspect id="as" ref="check"><aop:pointcut id="beforepoint" expression="execution(* com.aop.TestService.show(..))" /><aop:before method="CheckSecurity" pointcut-ref="beforepoint" /></aop:aspect> </aop:config> </beans>

xml中定義須要在中完畢配置,aop:aspect定義的是切面也就是我們的check,aop:pointcut定義切入點(diǎn),表達(dá)式expression=”execution(* com.aop.TestService.show(..))”,注意*后面有個(gè)一空格,show()中的..表示接受不論什么參數(shù),aop:before定義的是前置通知。也就是在切入點(diǎn)之前織入通知,還有aop:after后置通知,aop:around圍繞通知,aop:after-throwing當(dāng)拋出異常時(shí)插入通知和aop:after-returning返回時(shí)織入。
當(dāng)中:After advice :當(dāng)某連接點(diǎn)退出的時(shí)候運(yùn)行的通知(不論是正常返回還是異常退出)。ApplicationContext中在里面使用元素進(jìn)行聲明。


After return advice:在某連接點(diǎn)正常完畢后運(yùn)行的通知,不包含拋出異常的情況。

ApplicationContext中在里面使用元素進(jìn)行聲明。


單獨(dú)說(shuō)一下圍繞通知。須要加入?yún)?shù)ProceedingJoinPoint,并運(yùn)行proceed()方法。表示圍繞通知的方法運(yùn)行。

public class Check {public void CheckSecurity(){System.out.println("----checkSecurity----");}public void round(ProceedingJoinPoint jointpoint){try {System.out.println("round before");jointpoint.proceed();System.out.println("round after");} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}} } <aop:config><aop:aspect id="as" ref="check"><aop:pointcut id="beforepoint" expression="execution(* com.aop.TestService.show(..))" /><aop:before method="CheckSecurity" pointcut-ref="beforepoint" /><aop:around method="round" pointcut-ref="beforepoint" /></aop:aspect> </aop:config>

如今我們看到的通知都是沒(méi)有參數(shù),假設(shè)須要加入?yún)?shù),以CheckSecurity為例。

public void CheckSecurity(String value){System.out.println("----checkSecurity----"); // System.out.println("---check method arg "+name+"---");} <aop:config><aop:aspect id="as" ref="check"><aop:pointcut id="beforepoint" expression="execution(* com.aop.TestService.show(String)) and args(trouble)" /><aop:before method="CheckSecurity" arg-names="trouble" pointcut-ref="beforepoint" /><aop:around method="round" pointcut-ref="beforepoint" /></aop:aspect> </aop:config> public class TestService implements TestInter{private String name;@Overridepublic void show(String trouble) {// TODO Auto-generated method stubSystem.out.println(name);}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic void hh() {// TODO Auto-generated method stubSystem.out.println("hh");}}

使用切面還能引入新的功能。
types-matching表示的是原實(shí)現(xiàn)類,記得加+,default-impl是接口的實(shí)現(xiàn)。implement-interface是新實(shí)現(xiàn)類接口定義

<aop:declare-parentstypes-matching="fuckAOP.UserManager+"implement-interface="fuckAOP.NewManager"default-impl="fuckAOP.NewManagerImpl" />

以下說(shuō)一下注解裝配
注解裝配主要使用的是Aspect的語(yǔ)法。

@Aspect public class check {@Pointcut("execution(* com.annotation.User.show(..))")private void showMethod(){}@Before("showMethod()")public void beforeShow(){System.out.println("before show ");}@Around("showMethod()")public void round(ProceedingJoinPoint jointpoint){try {System.out.println("round before");jointpoint.proceed();System.out.println("round after");} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

@Aspect表示注入的切面,@Pointcut注解表示切點(diǎn)。表達(dá)式與xml的方法一致,@Before表示前置通知還有其它如同xml的元素。括號(hào)里表示切點(diǎn),切點(diǎn)已經(jīng)由@Pointcut定義。

public class test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("com/annotation/bean.xml");User user = (User) ac.getBean("user");user.show();} } public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void show(){System.out.println("name is "+this.name);} }

在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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <bean id="user" class="com.annotation.User"> <property name="name"> <value>spirit</value> </property> <property name="age"> <value>23</value> </property> </bean> <bean id="check" class="com.annotation.check"/> </beans>

假設(shè)注入須要參數(shù)的話

@Pointcut("execution(* com.annotation.User.show(String,String)) and args(testArg1,testArg2)")private void showMethod(String testArg1,String testArg2){}@Before("showMethod(testArg1,testArg2)")public void beforeShow(String testArg1,String testArg2){System.out.println("before show ");}

好了差點(diǎn)兒相同就這樣了,老規(guī)矩上代碼。
http://download.csdn.net/detail/wsrspirit/8870455

總結(jié)

以上是生活随笔為你收集整理的Spring 从零開始-05的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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