Sie sind auf Seite 1von 13

Introduction to Spring Framework

Java platform with comprehensive infrastructure support for developing Java applications a lightweight container for configuration and connecting/wiring of application objects.

Spring Philosophy
Java platform lacks the means to organize the basic building blocks into a coherent whole. There are design patterns such as Factory, Abstract Factory, builder, Decorator, and Service Locator. However, Patterns are formalized best practices that you must implement yourself in your application. Spring Framework codifies formalized design patterns as first-class objects that you can integrate into your own application. It is not just on paper, but ready to use product/solution.

Spring Overview

Core Container
Core and bean modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. The BeanFactory is a sophisticated implementation of the factory pattern. It removes the need for programmatic singletons and allows you to decouple the configurationand specification of dependencies from your actual program logic Context a means to access objects in a framework-style manner that is similar to a JNDI registry. Module inherits its features from the Beans module and adds support for internationalization (using, for example, resource bundles), event-propagation, resource-loading, and the transparent creation of contexts by, for example, a servlet container. The Context module also supports Java EE features such as EJB, JMX ,and basic remoting. The ApplicationContext interface is the focal point of the Context module. Expression language provides a powerful expression language for querying and manipulating an object graph at runtime. It is an extension of the unified expression language (unified EL) as specified in the JSP 2.1 specification. The language supports setting and getting property values, property assignment, method invocation, accessing the context of arrays, collections and indexers, logical and arithmetic operators, named variables, and retrieval of objects by name from Spring's IoC container. It also supports list projection and selection as well as common list aggregations

Inversion of Control
(Dependency Injection- key concept)
Classical example of IoC would be DOS UI to Window, the main control of a DOS UI was inverted, moved away from you to window Framework. For Spring, the inversion is about how to lookup a plugin implementation, which is determined at runtime. For a service or component, which can be accessed through well defined interface, we wish to deploy them in different ways to be able to use different implementations. A simple MovieLister application conveys the concept. The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface as illustrated by the diagram,

There are three main styles of dependency injection. Constructor Injection, Setter Injection, and Interface Injection, often referred to as type 3 IoC, type 2 IoC and type 1 IoC.

How Spring works

Injection with Spring


To get my movie lister to accept the injection I define a setting method for that service class MovieLister... private MovieFinder finder; public void setFinder(MovieFinder finder) { this.finder = finder; } Similarly I define a setter for the filename. class ColonMovieFinder... public void setFilename(String filename) { this.filename = filename; } The next step is to set up the configuration for the files. Spring supports configuration through XML files and also through code, but XML is the expected way to do it.

Meta-data
<beans> <bean id="MovieLister" class="spring.MovieLister"> <property name="finder"> <ref local="MovieFinder"/> </property> </bean> <bean id="MovieFinder" class="spring.ColonMovieFinder"> <property name="filename"> <value>movies1.txt</value> </property> </bean> </beans>

Using the Container


public void testWithSpring() throws Exception {
ApplicationContext ctx; ctx= new FileSystemXmlApplicationContext("spring.xml"); MovieLister lister =(MovieLister)ctx.getBean("MovieLister"); Movie[] movies =lister.moviesDirectedBy("Sergio Leone");

Programming Model
POJO Plus metadata configuration-Bean Definition There are three ways to configure a bean Definition XML format: <beans>/<beans> Is traditional, simple and intuitive. Annotation-based configuration introduced in Spring 2.5 Starting with Spring 3, Java-based configuration can be used to define beans external to an application classes like @Configuration, @Bean, @import, etc.

Any java tools plus xml editor can be used to develop Spring solution, eclipse plus SpringIDE makes it eaiser.

Deploy and Development platform


SpringSource dm Server is an open source, completely modular, OSGi-based Java server designed to run enterprise Java applications and Spring-powered applications with a new degree of flexibility and reliability SpringSource tc Server enhanced Tomcat, included in SpringSource Tool suite, SpringSource Enterprise Ready Server(ERS)- Apache + Tomcat. J2EE container and Standard alone Java application Eclipse and SpringIDE SpringSource ToolSuite for Spring Application, OSGI development and flexible deployment targets

Demo
A simple registration system, which is able to send an email notice without any coding effort by simply injecting related services into the application. Eclipse project Bean creation: implementation, definition Put all together Send an email to everybody

References
Download Spring: http://www.springsource.org/download Download Tool suite: http://www.springsource.com/products/springsou rce-tool-suite-download Spring tutorial: http://www.roseindia.net/spring/ Spring Framework Reference Doc: http://static.springsource.org/spring/docs/3.0.x/s pring-framework-reference/html/index.html Spring book: http://springindepth.com/book/index.html

Das könnte Ihnen auch gefallen