provides software and services that enable enterprises
Live Chat 1-888-673-6564

Open Source Software Technical Articles

  • Home
  • Search
  • Contact Us
  • Products and Support
  • Services
  • Enterprise OSS Blog
  • Wazi Technical Blog
  • About Wazi
  • Attributions and Licensing
  • Supply Chain Compliance
  • How to Contribute
  • Contributors
  • Resources Library
  • Cloud Services
  • Partners
  • Customers
  • Community
  • Company
  • Careers
  • News and Events

Subscribe to Wazi by Email

Your email:


Enterprise Developer Support 24 x 7, Get a Support Quote Now!


click-here-to-chat-with-an-online-representative

download-oss-discovery

Latest Posts

  • The secret to great reporting with Drupal 7
  • A more colorful LibreOffice unveiled
  • Toward a more colorful LibreOffice
  • Flexible administration with Puppet's Facter and templates
  • Knock for OpenSSH
  • Get more out of phpMyAdmin
  • Image annotation in GIMP, Dia, and OpenOffice Draw
  • Solr, Drupal 7, and faceted search
  • Using FreeNAS' new full disk encryption for ZFS
  • Create distributed storage with Gluster

Connect with Us!

Current Articles | RSS Feed RSS Feed

An Introduction to Open Source JEE Frameworks Via Spring

Posted by Rares Aioanei on Thu, Aug 23, 2012
  
Email This Email Article  
Tweet  
  

Spring, Apache Struts, JavaServer Faces, RIFE, Xerces, JAX-WS – these are just a few of the many Java frameworks in use around the world. If you haven't dealt with frameworks before, Java or otherwise, consider this article your introduction. You'll get to know what a framework is and what you can do with it, but we won't get so detailed as to bog you down with lots of code. All you need before we start is some Java experience and maybe XML knowledge.

A software framework offers an abstraction layer upon which you can build your software – a skeleton of generic functionality upon which you can modify, improve, and add whatever you feel is necessary. You might think we just gave you the definition of a library or an API, but a framework offers some features that set it apart:

  • Unlike libraries, with frameworks, control flow is dictated by the framework instead of the user. This inversion of control (IoC) is a central concept for Spring.

  • A framework always provides a default behavior.

  • Frameworks are extensible – that is, basically, a framework's primary role. However, the core framework code itself is not modifiable.

IoC is implemented through containers, which are components in which you can store other components, such as, for example, applets. If you've ever had to deal with Java web code, you know that containers are responsible for the management of the objects subordinated to them, their life cycles, creation, and even the glue that brings them together. Objects created by a container are called Java Beans, which is a term widely used in Enterprise Java. Said Beans can be offered by the container via dependency injection – by being called by name – or by dependency lookup, where the caller asks the container for a certain object.

A simple example, although not an easy one, is a compiler. A compiler is a framework because it respects all the four laws above. You don't get to control how the compiler works with data structures or how it generates the assembly code, but you can extend it to support more languages or dialects.

When speaking about software frameworks, we also speak of hot spots, which are parts that are changed by the developer, and frozen spots, which represent remain the same regardless of the usage pattern. Problems appear when the two types of components are out of balance. If there are too many frozen spots, the end result will be bloatware, and if there are too few, the developer has to do too much work, which obviates the whole point of a framework. Since there is no such thing as "one size fits all" when it comes to frameworks, choosing the right framework for your needs can be a challenging task, and one we're too cowardly to advise you on. Instead, let's get more practical and see how to install and use Spring.

Installation and Usage

If you've never used Spring, go to the Spring website to learn a bit about its layout from the available video tutorials and written docs.

On my Fedora 17 machine, Spring is available from the repositories, but CentOS users must install it from a tarball, which isn't as complicated as you might think. As long as you have Java EE installed, all you have to do is choose a directory where you want Spring to be downloaded and get it. You can choose either a non-installer archive, which you can just unpack and run, or if you want a point-and-click experience, choose the installer version. We chose the former, so we ran the following commands:

cd springdir
wget -c http://download.springsource.com/release/STS/3.0.0/dist/e4.2/spring-tool-suite-3.0.0.RELEASE-e4.2-linux-gtk-x86_64.tar.gz #adjust architecture as needed
tar xzvf spring-tool-suite-3.0.0.RELEASE-e4.2-linux-gtk-x86_64.tar.gz
cd springsource/sts-3.0.0.RELEASE/
./STS #this launches the Spring IDE

You will notice two things when you launch Spring: First, the IDE is a memory hog, and second, once it has (finally) started, the environment looks a lot like Eclipse. Both of these things are true because Spring's IDE is built on the Eclipse SDK, which also means that you can easily integrate Spring into Eclipse, if that's the IDE you use every day. Some well-written resources with lots of screenshots can help you with that task.

spring ide1 resized 600

To get started, go to File -> New -> Spring Template Project from within the Spring IDE, download said template if it isn't already there, set the top-level package (in the project settings window) to something like com.springframework.* – feel free to change that if you like – and start coding.

What better example should we start with than a Hello World program? Let's see:

//put package and import statements here....
public class HelloSpringBean 
{
	private String message;
	
	public void setMessage( String message )
	{
		this.message = message;
	}
	
	public String getMessage()
	{
		return this.message;
	}
	
	public static void main( String[] args )
	{
		ApplicationContext context = new FileSystemXmlApplicationContext( "applicationContext.xml" );
		HelloSpringBean helloSpringBean = ( HelloSpringBean )context.getBean( "helloSpringBean" );
		System.out.println( helloSpringBean.getMessage() );
		
	}
}

As you can see, we need an XML configuration file to specify where you set up various properties and their values. Here's an example that fits the code above:

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
	<bean id="helloSpringBean" class="com.myclass.hellospring.HelloSpringBean" >
		<property name="message" value="Hello, Spring" />
	</bean>
</beans>

Once you build the .java file, after some messages, the operation will indeed yield the message "Hello, Spring."

Obviously we've just barely started exploring Spring. The framework requires lots of concepts be mastered, both theoretical and practical, and Spring has other facets you might be interested in, including Roo (a rapid application development subsystem), Security, Mobile, Data, and Batch. But if you find the general concept of a Java framework interesting and efficient, we hope you will like Spring just as much as we do.

 

e21cef24-2786-4d03-b349-a36ab27f5913
Follow @openlogic
Follow @OSCloudServices

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.
Tags: Spring, Tutorial, Programming, Java

Comments

Currently, there are no comments. Be the first to post one!
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics

Loading...
Error sending email
Email sent successfully

Email article
Email To : 
Your name : 
Message : (maximum 200 characters)
Home | Search | Contact Us | Products and Support | Services | Enterprise OSS Blog | Wazi Technical Blog | Resources Library | Cloud Services | Partners | Customers | Community | Company | Careers | News and Events
Products
OpenLogic Exchange (OLEX)
License Compliance Module
OSS Discovery
OSS Deep Discovery
OpenUpdate
Services
Open Source Support
CentOS Support
Scanning & Compliance
Open Source Training
Professional Services
Solutions
Support & Indemnification
Open Source Governance
Open Source Scanning
Open Source Provisioning
Consulting & Training
Contact Us
1-888-673-6564


© 2013 OpenLogic, Inc. All rights reserved.
Site Map  |  Privacy Policy