房子最近在装修,着实忙,还好一十几年的老朋友搞装修的,这让我应对装修相当的轻松自如,哥们儿几个都巨羡慕我,啥也不用愁,出门在外,真的是朋友多了路好走啊!真的是没事暗自偷着乐哟!
今天终于能捞到个清闲的日子写写博客,跟大家交流交流。我最近学习一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,那你可直接在指定的文件夹下输入
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,重点看两个新的配置:
<!-- 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:
@Configuration
@ComponentScan(basePackages="org.springframework.android.basicauth",
excludeFilters={ @Filter(Configuration.class)} )
public class ComponentConfig {
}
@Configuration注解,该注解表明一个类声明了一个或多个Bean方法且可以被Spring窗口在运行时生成Bean的定义和服务请求。这个注解就类型于我们以前开发定义的Spring XML文件
@ComponentScan,这里用到了两个属性,分别为basePackages和excludeFilters,不解释,类似如下代码:
<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:
@Configuration
@ImportResource("classpath:security.xml")
public class SecurityConfig {
}
我们就只看ImportResource,引入类路径下的security.xml文件,类似Spring XML的<import/>标签功能,通过AnnotationConfigApplicationContext加载并解析实例化。security.xml文件我就不在此列出了,详细见工程包。主要就是定义了系统的权限,只有输入用户名/密码:roy/spring才能登入系统。
3)WebConfig:
@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配置文件如下:
<!-- 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该方法功能类似
<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 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的文件中,添加如上所示一致的用户权限信息:
<servers>
<server>
<id>TomcatServer</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
上述代码就是为了连接Tomcat的Web服务器而设置的权限配置信息,id必须唯一。
有了这个服务连接配置信息之后,那我们怎么办呢,有了就得要用啊!如何用?!发布哪个项目那我们就得在那个发布项目里添加关联发布了。这就需要我们在项目的pom.xml里添加Maven-Tomcat plugin,来将项目关联上Tomcat服务,具体配置如下所示:
<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客户端的项目学习。附件附上包含客户端的项目源代码。
- 大小: 78.8 KB
- 大小: 49.6 KB
分享到:
相关推荐
Spring for Android jar包 doc文档 源码 1.0.0M3版 Spring for Android jar包 doc文档 源码 1.0.0M3版
Spring for Android。 官网 Spring for Android API。 Spring for Android 开发文档。
Spring For Android是Spring框架的一个扩展,其主要目的在乎简化Android本地应用的开发,这其中包括了你可以使用该项目提供的RestTemplate来为你的Android客户端提供REST服务,另外该项目还提供了跟其它诸如Twitter...
spring for android 源码
Spring+Android框架 基于Spring框架Android开发
Spring for Android Samples This repository contains sample projects illustrating usage of Spring for Android. Each project requires set up of the Android SDK, command line tools, Maven, the Android ...
spring for android官方demo
spring-security初体验demo
Spring for Android 2 中文 参考手册 中文文档 中文文档都是由软件翻译,翻译内容未检查校对,文档内容仅供参考。
Spring for Android Spring for Android is a library that is designed to provide components of the Spring Framework family of projects for use in native Android applications. Code of Conduct This ...
Spring-android 所使用的jar包 及文档
Spring for android,案桌端的rest客户端,在传递rest信息和解析JSON很有帮助
spring-android-core-1.0.0.RELEASE-sources.jar
用spring构建android互联网应用
spring基于android的案例框架:对HTTP请求进行了封装,能够更简单的调用HTTP请求,案例中包括GET请求,POST请求,文件上传,JSON请求等案例
Boot入门初体验是一门针对初学者准备的课程,讲解了如何一步一步进入Spring?Boot开发,课程采用循序渐进的方式,一个知识点一个知识点的讲解和学习,让初学者不知不觉的掌握各种开发技能,课程包含了主流框架搭建、...
Spring for Apache Kafka API。 Spring for Apache Kafka 开发文档。
spring-android-basic-auth