標籤: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代碼
- 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代碼
- <!-- Java-based annotation-driven Spring container definition -->
- <context-param>
- <param-name>contextClass</param-name>
- <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
- </context-param>
-
- <!-- Location of Java @Configuration classes that configure the components
- that makeup this application -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>org.springframework.android.basicauth.config</param-value>
- </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代碼
- @Configuration
- @ComponentScan(basePackages="org.springframework.android.basicauth",
- excludeFilters={ @Filter(Configuration.class)} )
- public class ComponentConfig {
-
- }
@Configuration註解,該註解表明一個類聲明了一個或多個Bean方法且可以被Spring視窗在運行時產生Bean的定義和服務要求。這個註解就類型於我們以前開發定義的Spring XML檔案
@ComponentScan,這裡用到了兩個屬性,分別為basePackages和excludeFilters,不解釋,類似如下代碼:
Xml代碼
- <context:component-scan base-package="org.springframework.android.basicauth" >
- <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
- </context:component-scan>
也就是Spring在掃描組件實體化的時候,不需要執行個體化帶有Configuration的註解。
2)SecurityConfig:
Java代碼
- @Configuration
- @ImportResource("classpath:security.xml")
- public class SecurityConfig {
-
- }
我們就只看ImportResource,引入類路徑下的security.xml檔案,類似Spring XML的<import/>標籤功能,通過AnnotationConfigApplicationContext載入並解析執行個體化。security.xml檔案我就不在此列出了,詳細見工程包。主要就是定義了系統的許可權,只有輸入使用者名稱/密碼:roy/spring才能登入系統。
3)WebConfig:
Java代碼
- @Configuration
- @EnableWebMvc
- public class WebConfig extends WebMvcConfigurerAdapter {
-
- @Override
- public void addViewControllers(ViewControllerRegistry registry) {
- registry.addViewController("/").setViewName("index");
- }
-
- @Bean
- public ViewResolver viewResolver() {
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setPrefix("/WEB-INF/views/");
- viewResolver.setSuffix(".jsp");
- return viewResolver;
- }
-
- }
這個類呢主要的就是Spring MVC的配置功能,@EnableWebMvc與@Configuration一起使用,通過繼承WebMvcConfigurerAdapter這個適配器,然後繼承該類的私人方法,從而達到配置Spring MVC的目的。
viewResolver()方法的功能,類似Spring XML設定檔如下:
Xml代碼
- <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
- <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <beans:property name="prefix" value="/WEB-INF/views/" />
- <beans:property name="suffix" value=".jsp" />
- </beans:bean>
就是解析Spring MVC請求的頁面映射到/WEB-INF/views/包下的,所有尾碼名為.jsp的檔案。
addViewControllers該方法功能類似
Java代碼
- <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代碼
- <?xml version=‘1.0‘ encoding=‘utf-8‘?>
- <tomcat-users>
- <role rolename="manager"/>
- <user username="admin" password="admin" roles="manager"/>
- </tomcat-users>
這個意思是,添加一個名為manager的使用者用色,然後為這個使用者角色設定使用者名稱和密碼為admin。這樣,我們在瀏覽器裡輸入http://localhost:8080/manager,就可輸入上述提供的使用者名稱和密碼來操作Tomcat的一些專案管理。
既然我們需要通過Maven把項目布置到Tomcat伺服器,那我們是不是要將Maven與Tomcat建立一個關聯關係呢,所以在%MAVEN_PATH%/conf/settings.xml的檔案中,添加如上所示一致的使用者權限資訊:
Xml代碼
- <servers>
- <server>
- <id>TomcatServer</id>
- <username>admin</username>
- <password>admin</password>
- </server>
- </servers>
上述代碼就是為了串連Tomcat的Web伺服器而設定的許可權配置資訊,id必須唯一。
有了這個服務串連配置資訊之後,那我們怎麼辦呢,有了就得要用啊!如何用?!發布哪個項目那我們就得在那個發布項目裡添加關聯發布了。這就需要我們在項目的pom.xml裡添加Maven-Tomcat plugin,來將項目關聯上Tomcat服務,具體配置如下所示:
Xml代碼
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>tomcat-maven-plugin</artifactId>
- <configuration>
- <url>http://localhost:8080/manager</url>
- <server>TomcatServer</server>
- <path>/spring-android-basic-auth-server</path>
- </configuration>
- <version>1.1</version>
- </plugin>
啟動Tomcat伺服器,到指定目錄輸入mvn tomcat:deploy,即可完成spring-android-basic-auth項目Server的發布。這段配置就是告訴maven,將我們的項目以路徑spring-android-basic-auth-server通過TomcatServer的配置資訊發布到我們的本地Tomcat服務。這個過程實際上就是利用Tomcat的管理員權限用發布*.war的檔案到伺服器裡。
這些伺服器端的代碼就算是講解完成了,下一節我們來繼續Android用戶端的項目學習。附件附上包含用戶端的項目原始碼。
Spring For Android初體驗