Web: http://www.springframework.org/
Lightweight — Spring is lightweight in terms of both size and overhead. The entire Spring framework can be distributed in a single JAR file that weighs in at just over 1 MB. And the processing overhead required by Spring is negligible. What’s more, Spring is non intrusive: objects in a Spring-enabled application typically have no dependencies on Spring specific classes.
Inversion of control — Spring promotes loose coupling through a technique known as inversion of control (IoC). When IoC is applied, objects are passively given their dependencies instead of creating or looking for dependent objects for themselves. You can think of IoC as JNDI in reverse—instead of an object looking up dependencies from a container, the container gives the dependencies to the object at instantiation without waiting to be asked.
Aspect-oriented — Spring comes with rich support for aspect-oriented programming
that enables cohesive development by separating application business logic from system services (such as auditing and transaction management). Application objects do what they’re supposed to do—perform business logic—and nothing more. They are not responsible for (or even aware of) other system concerns, such as logging or transactional support.
Container — Spring is a container in the sense that it contains and manages the life cycle and configuration of application objects. You can configure how your each of your beans should be created—either create one single instance of your bean or produce a new instance every time one is needed based on a configurable prototype—and how they should be associated with each other. Spring should not, however, be confused with traditionally heavyweight EJB containers, which are often large and cumbersome to work with.
Framework — Spring makes it possible to configure and compose complex applications from simpler components. In Spring, application objects are composed declaratively, typically in an XML file. Spring also provides much infrastructure functionality (transaction management, persistence framework integration, etc.), leaving the development of application logic to developer.
Starting with Spring Framework
Downloading required files
You can download the latest release of Spring framework from this link. Select the spring-framework-2.x.z-with-dependencies.zip file, that contains the base files and the files to use Spring modules.
Unpacking file
Unpack the downloaded zip file to a directory. The directory structure, that is created looks like this:
The [lib] is the most important directory, the JAR files to Spring extensions are here.
Creating the first Spring application
Our sample application will have a service interface and implementation, what writes the Hello World to the console.
Required JAR files
To build and run our first application using Spring framework, we must add the following JAR files to the classpath:
- spring.jar
- spring-core.jar
- spring-context.jar
- commons-logging.jar
- log4j-1.2.xy.jar
Creating the Service interface
I don't discuss this step, this is a simple interface declaration.
Source code:
Implementing the interface
This is an easy step too, a simple interface implementation, I hope everyone realizes it.
Source code:
Creating configuration XML file
This is the first step, where we see something new: the XML configuration file. These files are the "heart of Spring". Here are the bean definitions, configuration data, I discuss it later in detail.
In brief, we create 3 instance of GreetingServiceImp with identification
- greetingService
- inheritedGreetingService
- wrongGreetingService
- greetingMessage
- name
<bean id="greetingService" class="spring.example.helloworld.GreetingServiceImp">bean definition means:
<property name="greetingMessage">
<value>Hello</value>
</property>
<property name="name">
<value>World</value>
</property>
</bean>
- create a spring.example.helloworld.GreetingServiceImp instance,
- set the property greetingMessage to "Hello",
- set the property name to "World",
- assign the greetingService identification to the instance.
XML file:
Running the code
The running code gets the three GreetingServiceImp instance from the ApplicationContext and calls the sayHello() methods.
The
ApplicationContext context = new ClassPathXmlApplicationContext("greeting.xml");
line create an ApplicationContext instance. Through this context we can access the beans defined in greeting.xml file, like this:
GreetingService greeting = (GreetingService) context.getBean("greetingService");
Source code:
Downloads
You can download this sample application as NetBeans project with dependencies here.
No comments:
Post a Comment