歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ssh(sturts2_spring_hibernate) 框架搭建之spring

ssh(sturts2_spring_hibernate) 框架搭建之spring

日期:2017/3/1 9:12:13   编辑:Linux編程

一、spring總結:

  ⑴、spring是一個輕量級的JAVA開發框架,主要的作用是用來管理實例(可以解決JAVA類中new對象的問題,節省內存資源。)和降低代碼之間的耦合性,促進代碼模塊化。

  ⑵、促進代碼的模塊化學也就是所說的IOC(Inversion Of Control)。然其中DI用的較為廣泛。

二、spring搭建:

  ⑴、導入相關的jar包:

    ①、下載好spring後解壓出來找到lib目錄將其中除了javaDoc和sources的jar包全部導入項目中,並且在struts解壓包lib目錄中找到struts2-spring-plugin的jar包和Commons-logging的jar包導入項目中。如下:

    

    ②、在web.xml中進行相關的文件配置:

      

    ③、在struts.xml中進行聲明:目的地是所有的實例創建都交給spring去做(使用spring來管理實例):

       

    ④、在項目src中新建一個xml文件即applicationContext.xml,在這裡進行spring的bean的配置:

      ⑴、首先需要進行beans標簽。beans標簽中是基本的配置頭信息,基本上不會改變,除了版本之間的差異之外需要改一下版本信息,其他基本變化不大。  

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

        

      <!--之後在這裡進行bean實例的配置。-->

    <beans>

       ⑵、其次配置bean實例中進行,下面是示例的配置:

        

         · bean標簽中的id屬性是bean的名字,可能一個實例中需要用到另外一個實例,就需要引用,這個id就是引用的名字。

         · bean標簽中的class屬性是指向的這個類,也就是會對這個類進行new實例,但是不是自己作,而是bean幫我們解決。  

         · bean標簽中的scope是設置實例的創建形式,如:

              singleton:單實例,在bean無狀態使用。

              prototype:每次對這個bean的請求都會創建一個實例。

              request:每次http請求將會有各自的bean實例,類似於prototype。

              session:在一個http session中,一個bean定義對應一個bean實例。

             global session:在一個全局的http session中,一個bean定義對應一個bean實例。典型情況下,僅在使用portlet context的時候有效。

          

        ① 、這行代碼的意思是:首先,在action.ActionService類中需要有一個成員屬性為ms,定義了這個屬性的set方法(spring會幫忙注入這個實例),並且類型是一個services.MyServiceImp的類型或者是這個類的接口類型,如下:

  

         其次,這個屬性引用了下面的bean的id為myServiceImp的實例。

         最後,這個句最終的作用就是為這個ms成員屬性注入了一個myServiceImp的實例。

        

        ②、 這句代碼的意思則是:new出了一個util.MyConnectionImp的實例,並且每次請求這個bean的時候都會new一個這個實例。

Copyright © Linux教程網 All Rights Reserved