您的位置:首页 > 编程语言 > Java开发

Spring For Android初体验

2012-09-07 16:11 489 查看
Kaiwii注释:

关于这篇文章有两点说得不是太好的地方:

1、关于Spring的@Configuration讲解得不是太好的,可以参考这篇文章,理解这点:

http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/

2、而关于tomcat与maven用于项目发布的内容也是有详细的地方,可以参考这篇文章:

http://www.cnblogs.com/cbf4life/archive/2010/01/29/1659502.html

正文:

今天终于能捞到个清闲的日子写写博客,跟大家交流交流。我最近学习一Spring子项目:SPRING FOR ANDROID。

官方网址:http://www.springsource.org/spring-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代码



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建立一个关联关系呢,所以在
%M***EN_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客户端的项目学习。附件附上包含客户端的项目源代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: