Spring For Android初體驗

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   os   使用   sp   

Spring For Android是Spring架構的一個擴充,其主要目的在乎簡化Android本地應用的開發,這其中包括了你可以使用該項目提供的 RestTemplate來為你的Android用戶端提供REST服務,另外該項目還提供了跟其它諸如Twitter和Facebook等社交網路的集 成與OAuth授權用戶端等等。關於SPRING FOR ANDROID的一些詳細說明與應用,可參考官方說明文檔:http://static.springsource.org/spring- android/docs/1.0.x/reference/html/overview.html

  概念性質的東西我也就不再說明了,我們直接從該子項目的官方Sample開始學習吧,簡單且直接:

  樣本程式位於:https://github.com/SpringSource/spring-android-samples

  如果你已經安裝了GitHub,那你可直接在指定的檔案夾下輸入

 

Java代碼  
  1. git clone https://github.com/SpringSource/spring-android-samples.git  

 

  從而擷取到原始碼(具體的GitHub使用細節可參考Android源碼擷取 )。

  或者乾脆,你就直接老辦法Download as Zip。

  代碼拿下來以後,可以看到包含四個小項目,本博文就以spring-android-basic-auth作為我們的學習之旅,之所以選擇這個樣本,是因為它麻雀雖小但五髒俱全。非常的具有代表性。

  開啟Eclipse分別匯入maven項目spring-android-basic-auth的client/server,當然前提是,你確保你已經 成功安裝了Android的SDK,Eclipse的外掛程式ADT,以及Maven的Eclipse外掛程式m2eclipse,OK,匯入項目:

  因為需要從網上下載項目關聯的jar包,所以項目匯入速度取決於您當前的網路環境。匯入項目我想你肯定或多或少的會遇到一點點的小麻煩,我只能在這裡列舉一些我在匯入項目所遇到的問題僅供大家參照

  1、我們現在是要在Eclipse上利用Maven來編譯管理Android項目,所以我們需要安裝Android Configurator這個Maven Android外掛程式,提供了針對ADT的Maven依賴管理的能力,這個外掛程式使得
M2E、ADT以及Maven Android Plugin這三個外掛程式在Eclipse內部能夠很良好的協調工作。該外掛程式的安裝可通過Eclipse Marketplace...來安裝。我使用的是Eclipse Indigo版本,直接在Help中找到Eclipse Marketplace..,點擊,輸入:

   Android M2E,如所示:

 

   點擊“install”,如全選:

 

 

  安裝完成後,重啟Eclipse,搞定。

  2、spring-android-basic-auth的server代碼解析:

    2.1、匯入Maven項目,我的工程地址是:G:\source\spring-android-samples\spring-android-basic-auth\server,匯入到Eclipse中,該工程包含了Spring3.0很多最新的功能,同時也包含了Spring Security。我們來看看該工程主要包含哪些內容,我們首先開啟web.xml,重點看兩個新的配置:

 

Xml代碼  
  1. <!-- Java-based annotation-driven Spring container definition -->  
  2. <context-param>  
  3.     <param-name>contextClass</param-name>  
  4.     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>  
  5. </context-param>  
  6.   
  7. <!-- Location of Java @Configuration classes that configure the components   
  8.     that makeup this application -->  
  9. <context-param>  
  10.     <param-name>contextConfigLocation</param-name>  
  11.     <param-value>org.springframework.android.basicauth.config</param-value>  
  12. </context-param>  

 

  第一個context-param,指定AnnotationConfigWebApplication載入WebApplicatioinContext,該類是Spring3.0引入的,提供了基於註解的方式代替XML檔案配置,第二個context-param則指定了AnnotationConfigWebApplicationContext掃描以@Configuration配置的類的路徑。接著我們來看看config包下的內容

  2-2、org.springframework.android.basicauth.config包下的類查看:

     該包下包含三個ComponentConfig、SecurityConfig、WebConfig配置類

     1)ComponentConfig:

 

Java代碼  
  1. @Configuration  
  2. @ComponentScan(basePackages="org.springframework.android.basicauth",   
  3.     excludeFilters={ @Filter(Configuration.class)} )  
  4. public class ComponentConfig {  
  5.   
  6. }  

    @Configuration註解,該註解表明一個類聲明了一個或多個Bean方法且可以被Spring視窗在運行時產生Bean的定義和服務要求。這個註解就類型於我們以前開發定義的Spring XML檔案

    @ComponentScan,這裡用到了兩個屬性,分別為basePackages和excludeFilters,不解釋,類似如下代碼:

 

Xml代碼  
  1. <context:component-scan base-package="org.springframework.android.basicauth" >  
  2.         <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>  
  3. </context:component-scan>  

 

    也就是Spring在掃描組件實體化的時候,不需要執行個體化帶有Configuration的註解。

    2)SecurityConfig:

 

Java代碼  
  1. @Configuration  
  2. @ImportResource("classpath:security.xml")  
  3. public class SecurityConfig {  
  4.       
  5. }  

    我們就只看ImportResource,引入類路徑下的security.xml檔案,類似Spring XML的<import/>標籤功能,通過AnnotationConfigApplicationContext載入並解析執行個體化。security.xml檔案我就不在此列出了,詳細見工程包。主要就是定義了系統的許可權,只有輸入使用者名稱/密碼:roy/spring才能登入系統。

   3)WebConfig:

 

Java代碼  
  1. @Configuration  
  2. @EnableWebMvc  
  3. public class WebConfig extends WebMvcConfigurerAdapter {  
  4.   
  5.     @Override  
  6.     public void addViewControllers(ViewControllerRegistry registry) {  
  7.         registry.addViewController("/").setViewName("index");  
  8.     }  
  9.   
  10.     @Bean  
  11.     public ViewResolver viewResolver() {  
  12.         InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
  13.         viewResolver.setPrefix("/WEB-INF/views/");  
  14.         viewResolver.setSuffix(".jsp");  
  15.         return viewResolver;  
  16.     }  
  17.    
  18. }  

   這個類呢主要的就是Spring MVC的配置功能,@EnableWebMvc與@Configuration一起使用,通過繼承WebMvcConfigurerAdapter這個適配器,然後繼承該類的私人方法,從而達到配置Spring MVC的目的。

     viewResolver()方法的功能,類似Spring XML設定檔如下:

 

Xml代碼  
  1. <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->  
  2. <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  3.     <beans:property name="prefix" value="/WEB-INF/views/" />  
  4.     <beans:property name="suffix" value=".jsp" />  
  5. </beans:bean>  

 

    就是解析Spring MVC請求的頁面映射到/WEB-INF/views/包下的,所有尾碼名為.jsp的檔案。

     addViewControllers該方法功能類似

 

Java代碼  
  1. <mvc:view-controller path="/" view-name="index"/>  

    也就是預設頁面指向WEB-INF/views/index.jsp檔案。

  3、布署spring-android-basic-auth項目到Tomcat伺服器:

   首先在tomcat伺服器的%TOMCAT_PATH%/conf/tomcat-users.xml中添加一個擁有管理員權限訪問伺服器的使用者。例如:

 

Xml代碼  
  1. <?xml version=‘1.0‘ encoding=‘utf-8‘?>  
  2. <tomcat-users>  
  3.   <role rolename="manager"/>  
  4.   <user username="admin" password="admin" roles="manager"/>  
  5. </tomcat-users>  

  這個意思是,添加一個名為manager的使用者用色,然後為這個使用者角色設定使用者名稱和密碼為admin。這樣,我們在瀏覽器裡輸入http://localhost:8080/manager,就可輸入上述提供的使用者名稱和密碼來操作Tomcat的一些專案管理。

  既然我們需要通過Maven把項目布置到Tomcat伺服器,那我們是不是要將Maven與Tomcat建立一個關聯關係呢,所以在%MAVEN_PATH%/conf/settings.xml的檔案中,添加如上所示一致的使用者權限資訊:

 

Xml代碼  
  1.  <servers>  
  2.     <server>  
  3.         <id>TomcatServer</id>  
  4.         <username>admin</username>  
  5.         <password>admin</password>  
  6.     </server>  
  7. </servers>  

 

  上述代碼就是為了串連Tomcat的Web伺服器而設定的許可權配置資訊,id必須唯一。

  有了這個服務串連配置資訊之後,那我們怎麼辦呢,有了就得要用啊!如何用?!發布哪個項目那我們就得在那個發布項目裡添加關聯發布了。這就需要我們在項目的pom.xml裡添加Maven-Tomcat plugin,來將項目關聯上Tomcat服務,具體配置如下所示:

 

Xml代碼  
  1. <plugin>  
  2.       <groupId>org.codehaus.mojo</groupId>  
  3.       <artifactId>tomcat-maven-plugin</artifactId>  
  4.       <configuration>  
  5.         <url>http://localhost:8080/manager</url>  
  6.         <server>TomcatServer</server>  
  7.         <path>/spring-android-basic-auth-server</path>  
  8.       </configuration>  
  9.       <version>1.1</version>  
  10. </plugin>  

   啟動Tomcat伺服器,到指定目錄輸入mvn tomcat:deploy,即可完成spring-android-basic-auth項目Server的發布。這段配置就是告訴maven,將我們的項目以路徑spring-android-basic-auth-server通過TomcatServer的配置資訊發布到我們的本地Tomcat服務。這個過程實際上就是利用Tomcat的管理員權限用發布*.war的檔案到伺服器裡。

   這些伺服器端的代碼就算是講解完成了,下一節我們來繼續Android用戶端的項目學習。附件附上包含用戶端的項目原始碼。

Spring For Android初體驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.