Spring for Android is an extension of the spring framework, and its primary purpose is to simplify the development of native Android apps, including the resttemplate you can use to provide rest services to your Android clients. The project also provides integration with other social networks such as Twitter and Facebook, and OAuth authorized clients. For some detailed instructions and applications for the spring for Android, refer to the official documentation: http://static.springsource.org/spring-android/docs/1.0.x/reference/html /overview.html
The concept of the nature of things I will no longer explain, we directly from the sub-project of the official sample began to learn it, simple and direct:
The sample program is located at: https://github.com/SpringSource/spring-android-samples
If you already have GitHub installed, you can enter it directly under the specified folder.
Java code
- git clone https://github.com/springsource/spring-android-samples.git
To get to the source code (specific github use details can refer to the Android source).
Or simply, you just have the old-fashioned download as Zip.
After the code is taken, you can see that there are four small items, and this blog post takes Spring-android-basic-auth as our learning journey, the reason why we chose this example, is because it is smaller but perfectly formed. Very representative.
Open Eclipse to import the MAVEN project Spring-android-basic-auth Client/server, of course, if you make sure that you have successfully installed the Android Sdk,eclipse plugin ADT, As well as Maven's Eclipse plugin M2eclipse,ok, import the project:
Because the project-associated jar package needs to be downloaded from the Web, the project import speed depends on your current network environment. Import a project I think you will be more or less a little bit of trouble, I can only be here to list some of the problems I encountered in the import project is for everyone to refer to
1. We are now using MAVEN to compile and manage Android projects on Eclipse, so we need to install the Maven Android plugin for Android Configurator, which provides MAVEN dependency management capabilities for ADT, This plugin makes
The three plugins m2e, ADT, and Maven Android plugin work well together within Eclipse. The plugin can be installed via Eclipse Marketplace ... To install. I'm using the Eclipse Indigo version, and I found Eclipse Marketplace directly in Help.., click, enter:
Android m2e as shown in:
Click "Install", such as Select all:
After the installation is complete, restart Eclipse and do it.
2. Spring-android-basic-auth Server Code parsing:
2.1, Import Maven project, my project address is: G:\source\spring-android-samples\spring-android-basic-auth\server, import into Eclipse, The project contains many of the newest features of Spring3.0 and also includes spring Security. Let's take a look at the main content of the project, and we'll start with Web. XML, focusing on two new configurations:
XML code
- <!--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, the components
- That's makeup this application-->
- <context-param>
- <param-name>contextconfiglocation</param-name>
- <param-value>org.springframework.android.basicauth.config</param-value >
- </context-param>
The first context-param, specifying annotationconfigwebapplication loading Webapplicatioincontext, This class is introduced in Spring3.0, which provides an annotation-based alternative to the XML file configuration, while the second context-param specifies the Annotationconfigwebapplicationcontext scan at @ The path to a configuration-configured class. Then let's take a look at what's under the config package.
2-2, Org.springframework.android.basicauth.config package under the Class View:
The package contains three componentconfig, Securityconfig, webconfig configuration classes
1) Componentconfig:
Java code
- @Configuration
- @ComponentScan (basepackages="Org.springframework.android.basicauth",
- excludefilters={ @Filter (Configuration. Class)})
- Public class Componentconfig {
- }
@Configuration annotation, which indicates that a class declares one or more bean methods and can be used by the Spring window to generate the bean's definition and service request at run time. This annotation is typed in the spring XML file that we have previously developed to define
@ComponentScan, two properties are used here, basepackages and excludefilters, not explained, similar to the following code:
XML code
- <context:component-scan base-package="Org.springframework.android.basicauth" >
- <context:exclude-filter type="annotation" expression=" Org.springframework.context.annotation.Configuration "/>
- </context:component-scan>
That is, spring does not need to instantiate annotations with a configuration when the scan component is materialized.
2) Securityconfig:
Java code
- @Configuration
- @ImportResource ("Classpath:security.xml")
- Public class Securityconfig {
- }
We just look at the Importresource, introducing the Security.xml file under the classpath, like the spring XML <import/> tag function, Load and parse the instantiation through Annotationconfigapplicationcontext. Security.xml file I'm not out of this, see the project package in detail. The main is to define the system's permissions, only enter the user name/password: roy/spring to log in the system.
3) Webconfig:
Java code
- @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;
- }
- }
The main thing about this class is the configuration of Spring MVC, @EnableWebMvc used with @configuration, by inheriting Webmvcconfigureradapter the adapter, and then inheriting the private method of the class. To achieve the purpose of configuring spring MVC.
The functionality of the Viewresolver () method, similar to the spring XML configuration file, is as follows:
XML code
- <!--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>
The page that parses the spring MVC request is mapped to a file under the/web-inf/views/package with all suffixes named. jsp.
Addviewcontrollers This method functions like
Java code
- <mvc:view-controller path="/" View-name="index"/>
That is, the default page points to the web-inf/views/index.jsp file.
3. Deployment of Spring-android-basic-auth project to Tomcat server:
First, %TOMCAT_PATH%/conf/tomcat-users.xml add a user who has administrator access to the server in the Tomcat server. For example:
XML code
- <? xml version= '1.0 ' encoding=' utf-8 '?>
- <tomcat-users>
- <role rolename="manager"/>
- <user username="admin" password="admin" roles="manager"/>
- </tomcat-users>
This means adding a user name to the manager and then setting the user name and password for this user role as admin. In this way, we enter Http://localhost:8080/manager in the browser to enter the username and password provided above to manipulate some of Tomcat's project management.
Now that we need to set up the project to the Tomcat server through MAVEN, do we have to build a relationship between Maven and Tomcat, so%MAVEN_PATH%/conf/settings.xml的文件中,添加如上所示一致的用户权限信息:
XML code
- <servers>
- <server>
- <ID>tomcatserver</ID>
- <username>admin</username>
- <password>admin</password>
- </Server>
- </Servers>
The code above is the permission configuration information set up to connect to the Tomcat Web server, and the ID must be unique.
With this service connection configuration information, then what do we do, have to use Ah! How to use?! Which project to publish then we'll have to add the affiliate release to that release project. This requires that we add Maven-tomcat plugin to the project's Pom.xml to associate the project with the Tomcat service, as shown in the following configuration:
XML code
- <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>
Launch the Tomcat server and enter MVN Tomcat:deploy in the specified directory to complete the Spring-android-basic-auth Project Server release. This configuration is to tell Maven to post our project to our local Tomcat service with the configuration information of path spring-android-basic-auth-server through Tomcatserver. The process is actually using Tomcat's administrator privileges to publish *.war files to the server.
These server-side code even if the explanation is complete, the next section we will continue the Android client project learning. The attachment includes the project source code that contains the client.
Spring for Android first experience