Spring For Android初体验

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,如下图所示:

M2E-ANDROID

   点击“install”,如下图全选:

  安装完成后,重启Eclipse,搞定。

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

    2.1、导入Maven项目,我的工程地址是:G:sourcespring-android-samplesspring-android- basic-authserver,导入到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客户端的项目学习。附件附上包含客户端的项目源代码。

原文地址:https://www.cnblogs.com/welhzh/p/4107078.html