spring重點

1、首先明白什麼叫依賴注入、控制反轉,及其作用

控制反轉(Inversion of Control ,Ioc)
所謂控制反轉就是應用本身不負責依賴對象的創建及維護,依賴對象的創建及維護由外部容器來負責。這樣控制權就由應用轉移到了外部容器,控制權的轉移就是所謂反轉。

   依賴注入(Dependency Injection)
所謂依賴注入就是指:在運行期間,有外部容器動態地將依賴對象注入到組件中(構造方法和set方法)

好處:   

1.降低組件之間的耦合度,實現軟件各層之間的解耦.
2.可以使容器提供衆多服務如事務管理消息服務處理等等。當我們使用容器管理事務時,開發人員就不需要手工 控制事務,也不需要處理複雜的事務傳播
3.容器提供單例模式支持,開發人員不需要自己編寫實現代碼.
4.容器提供了AOP技術,利用它很容易實現如權限攔截,運行期監控等功能
5.容器提供衆多的輔佐類,使這些類可以加快應用的開發.如jdbcTemplate HibernateTemplate

個人理解依賴注入完全實現了面向接口編程,只需定義方法,無需實現,首先面向接口編程的好處就是統一編碼風格。由容器創建對象,可以說是低內聚,高耦合。增加了代碼的可維護性。

2、spring對於編程人員最重要的是寫xml配置文件和使用註解那麼我這裏給幾個簡單的案例

依賴注入配置xml

  1. <bean id="person" class="com.itheima12.spring.di.xml.setter.Person">
  2. <!--
  3. property就是一個bean的屬性
  4. name就是用來描述屬性的名稱
  5. value就是值,如果是一般類型(基本類型和String)
  6. -->
  7. <property name="pid" value="1"></property>
  8. <property name="name" value="狗蛋"></property>
  9. <!--
  10. spring容器內部創建的student對象給Person的student對象賦值了
  11. -->
  12. <property name="student">
  13. <ref bean="student"/>
  14. </property>
  15. <property name="lists">
  16. <list>
  17. <value>list1</value>
  18. <value>list2</value>
  19. <ref bean="student"/>
  20. </list>
  21. </property>
  22. <property name="sets">
  23. <set>
  24. <value>set1</value>
  25. <value>set2</value>
  26. <ref bean="student"/>
  27. </set>
  28. </property>
  29. <property name="map">
  30. <map>
  31. <entry key="m1">
  32. <value>map1</value>
  33. </entry>
  34. <entry key="m2">
  35. <ref bean="student"/>
  36. </entry>
  37. </map>
  38. </property>
  39. <property name="properties">
  40. <props>
  41. <prop key="p1">prop1</prop>
  42. <prop key="p2">prop2</prop>
  43. </props>
  44. </property>
  45. <property name="objects">
  46. <list>
  47. <value>obj1</value>
  48. <ref bean="student"/>
  49. </list>
  50. </property>
  51. </bean>
構造器注入配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="person" class="com.itheima12.spring.di.xml.constructor.Person">
  7. <!--
  8. constructor-arg指的是構造器中的參數
  9. index 角標 從0開始
  10. value 如果一般類型,用value賦值
  11. ref 引用類型賦值
  12. -->
  13. <constructor-arg index="0" value="asdfsafd"></constructor-arg>
  14. <constructor-arg index="1" ref="student"></constructor-arg>
  15. </bean>
  16. <bean id="student" class="com.itheima12.spring.di.xml.constructor.Student"></bean>
  17. </beans>
註解配置xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  9. <!--
  10. component:把一個類放入到spring容器中,該類就是一個component
  11. 在base-package指定的包及子包下掃描所有的類
  12. -->
  13. <context:component-scan base-package="com.itheima12.spring.scan">
  14. </context:component-scan>
  15. </beans>

好了這裏就主要介紹了這些,還有很多遇到時在看文檔吧,百度吧(包括單例,多例,事務 ,aop,繼承,初始化銷燬(

  1. <bean id="helloWorld" class="com.itheima12.spring.initdestroy.HelloWorld"
  2. init-method="init"
  3. destroy-method="destroy"></bean>

),懶加載,別名,工廠方法創建對象等等,都可以看案例。

3、我們來學習aop

   切面(Aspect):其實就是共有功能的實現。如日誌切面、權限切面、事務切面等。在實際應用中通常是一個存放共有功能實現的普通Java類,之所以能被AOP容器識別成切面,是在配置中指定的。

   通知(Advice):是切面的具體實現。以目標方法爲參照點,根據放置的地方不同,可分爲前置通知(Before)、後置通知(AfterReturning)、異常通知(AfterThrowing)、最終通知(After)與環繞通知(Around5種。在實際應用中通常是切面類中的一個方法,具體屬於哪類通知,同樣是在配置中指定的。

   連接點(Joinpoint):就是程序在運行過程中能夠插入切面的地點。例如,方法調用、異常拋出或字段修改等,但spring只支持方法級的連接點。

   切入點(Pointcut):用於定義通知應該切入到哪些連接點上。不同的通知通常需要切入到不同的連接點上,這種精準的匹配是由切入點的正則表達式來定義的。

  目標對象(Target):就是那些即將切入切面的對象,也就是那些被通知的對象。這些對象中已經只剩下乾乾淨淨的核心業務邏輯代碼了,所有的共有功能代碼等待AOP容器的切入。

  代理對象(Proxy):將通知應用到目標對象之後被動態創建的對象。可以簡單地理解爲,代理對象的功能等於目標對象的核心業務邏輯功能加上共有功能。代理對象對於使用者而言是透明的,是程序運行過程中的產物。

  織入(Weaving):將切面應用到目標對象從而創建一個新的代理對象的過程。這個過程可以發生在編譯期、類裝載期及運行期,當然不同的發生點有着不同的前提條件。譬如發生在編譯期的話,就要求有一個支持這種AOP實現的特殊編譯器;發生在類裝載期,就要求有一個支持AOP實現的特殊類裝載器;只有發生在運行期,則可直接通過Java語言的反射機制與動態代理機制來動態實現

  

  意義:在開發的時候,各個切面和目標類是完全鬆耦合的,但是最終生成的代理對象的方法把這幾個代理對象的內容就結合起來了。

通知介紹

攔截環繞通知

             

   

 

  1. /**
  2. * 環繞通知
  3. * joinPoint.proceed();這個代碼如果在環繞通知中不寫,則目標方法不再執行
  4. * 能控制目標方法的執行
  5. *前置通知和後置通知能在目標方法的前面和後面加一些代碼,但是不能控制目標方法的執行
  6. */
  7. public void aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable{
  8. System.out.println("begin");
  9. joinPoint.proceed();//調用目標方法
  10. System.out.println("end");
  11. }


Spring中最基礎的通知類型是攔截環繞通知interception around advice)。

Spring裏使用方法攔截的環繞通知兼容AOP聯盟接口。

 前置通知

個更簡單的通知類型是前置通知before advice)。 它不需要MethodInvocation對象,因爲它只是在進入方法之前被調用。

前置通知的一個主要優點是它不需要調用proceed()方法,因此就不會發生無意間運行攔截器鏈失敗的情況。

     

  1. /*
  2. 前置通知
  3. 1、在目標方法執行之前
  4. 2、獲取不到目標方法的返回
  5. <aop:before method="beginTransaction" pointcut-ref="perform"/>
  6. * 參數:連接點
  7. */
  8. public void beginTransaction(JoinPoint joinPoint){
  9. String methodName = joinPoint.getSignature().getName();
  10. System.out.println("連接點的名稱:"+methodName);
  11. System.out.println("目標類:"+joinPoint.getTarget().getClass());
  12. System.out.println("begin transaction");
  13. }

 異常通知

  1. <!--
  2. 異常通知
  3. -->
  4. <aop:after-throwing method="throwingMethod" throwing="ex" pointcut-ref="perform"/>
  5. /**
  6. * 異常通知
  7. * 接受目標方法拋出的異常
  8. */
  9. public void throwingMethod(JoinPoint joinPoint,Throwable ex){
  10. System.out.println(ex.getMessage());
  11. }


 後置通知

 

  1. /**
  2. * 後置通知
  3. * 在目標方法執行之後
  4. 1、後置通知可以獲取到目標方法的返回值
  5. 2、當目標方法拋出異常,後置通知將不再執行
  6. <aop:after-returning method="commit" pointcut-ref="perform" returning="val"/>
  7. */
  8. public void commit(JoinPoint joinPoint,Object val){
  9. System.out.println("目標方法的返回值:"+val);
  10. System.out.println("commit");
  11. }

後置通知可以訪問返回值(但不能進行修改),被調用方法,方法參數以及目標對象。

最終通知

       

  1. /**
  2. * 最終通知
  3. 無論目標方法是否拋出異常都將執行
  4. <aop:after method="finallyMethod" pointcut-ref="perform"/>
  5. */
  6. public void finallyMethod(){
  7. System.out.println("finally method");
  8. }

引入通知

Spring 把引入通知(introduction advice)作爲一種特殊的攔截通知進行處理。

引入通知需要一個IntroductionAdvisor,和一個IntroductionInterceptor,後者實現下面的接口:

Public interface IntroductionInterceptor extends MethodInterceptor {   

                boolean implementsInterface(Class intf);

            }

繼承自AOP聯盟MethodInterceptor 接口的invoke()方法,必須確保實現引入:也就是說,如果被調用的方法位於一個已經被引入接口裏,這個引入攔截器將負責完成對這個方法的調用--因爲它不能調用proceed()方法。

引入通知不能和任何切入點一起使用,因爲它是應用在類級別而不是方法級別。

xml文件配置

  1. <aop:config>
  2. <!-- 面 -->
  3. <aop:aspect ref="cacheInterceptor">
  4. <!-- 點 -->
  5. <!-- 環繞通知 -->
  6. <aop:around method="doAround" pointcut="execution(* cn.itcast.core.service.*.*.get*(..))"/>
  7. <!-- 變更 -->
  8. <!-- 後知通知 -->
  9. <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.update*(..))"/>
  10. <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.add*(..))"/>
  11. <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.delete*(..))"/>
  12. <!--前置通知-->
  13. <!-- <aop:before method="" pointcut-ref=""/> -->
  14. </aop:aspect>
  15. </aop:config>

切入點表達式一般格式


Spring AOP 用戶可能會經常使用 execution切入點指示符。執行表達式的格式如下:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)

          throws-pattern?)

除了返回類型模式(上面代碼片斷中的ret-type-pattern),名字模式和參數模式以外, 所有的部分都是可選的。

 

service包或其子包中定義的任意方法的執行:(國家電力)

execution(* com.xyz.service..*.*(..))

service包中定義的任意方法的執行:

execution(* com.xyz.service.*.*(..))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章