Module 5 CONFIGURING A SPRING DEVELOPMENT ENVIRONMENT The Spring Framework > The Spring framework (spring.io) is a comprehensive Java SE/Java EE application framework > Spring addresses many aspects of Java/Java EE application development, and it can help you to build high-quality, high-performance applications more quickly. > The heart of the Spring framework is a lightweight IoC container that is able to add enterprise services to simple Java objects declaratively.
The Spring Framework > Spring makes extensive use of an excellent programming methodology AOP to provide these services to its components. > Within the Spring IoC container s scope, components are also called beans. > The Spring framework itself incorporates many design patterns, including the GoF object-oriented patterns and Sun s core Java EE patterns. > By using the Spring framework, you will use industry best practices to design and implement your applications. What is Spring? > Spring is a lightweight dependency injection and aspectoriented container and framework. > Lightweight Spring is lightweight in terms of both size and overhead: The Spring Framework can be distributed in a single JAR file that weighs in at just over 2.5 MB. The processing overhead required by Spring is negligible. Spring is nonintrusive: objects in a Spring-enabled application often have no dependencies on Springspecific classes.
What is Spring? > Spring is a lightweight dependency injection and aspectoriented container and framework. > Dependency Injection Spring promotes loose coupling through a technique known as dependency injection (DI). > When DI is applied, objects are passively given their dependencies instead of creating or looking for dependent objects for themselves. What is Spring? > Spring is a lightweight dependency injection and aspectoriented container and framework. > Aspect-oriented Spring comes with rich support for aspect-oriented programming (AOP) 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 other system concerns, such as logging or transactional support.
What is Spring? > Spring is a lightweight dependency injection and aspectoriented container and framework. > Container Spring is a container in the sense that it contains and manages the lifecycle and configuration of application objects. In Spring, you can declare how each of your application objects should be created, how they should be configured, and how they should be associated with each other. What is Spring? > Spring is a lightweight dependency injection and aspectoriented container and framework. > 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 you.
Spring Framework History > Started 2002 Rod Johnson and Juergen Holler Expert One-on-one J2EE Design and Development Spring 1.0 released on March 2004 Interface21, Spring source, now spring io > Current version (2014) 4.0 Spring==Java EE Application Server? > Spring is NOT a Java EE application server Integrates with Java EE application server Can work without a server > Spring does not replace a server > Spring provides application services Spring can replace services traditionally provided by Java EE application servers Make the application independent of server runtime
Spring Framework Mission Statement > Java EE should be easier to use > It is best to program to interfaces, rather than classes. Spring reduces the complexity cost of using interfaces to zero. > JavaBeans offer a great way of configuring applications. > OO design is more important than any implementation technology, such as Java EE. > Checked exceptions are overused in Java. A platform shouldn't force you to catch exceptions you're unlikely to be able to recover from. > Testability is essential Spring should help make your code easier to test. Introducing Spring s Modules > The Spring Framework consists of features organized into about 20 modules.
Introducing Spring s Modules > The Core and Beans 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 configuration and specification of dependencies from your actual program logic. Introducing Spring s Modules > Means to access objects in a framework-style manner that is similar to a JNDI registry. > Advanced Spring IoC container: ApplicationContext. > Adds features such as internationalization support, eventbased communication, and resource loading
Introducing Spring s Modules > The Expression Language module 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 Introducing Spring s Modules > This module establishes an aspect-oriented programming framework, which is referred to as Spring AOP. > AOP is another of Spring s fundamental concepts besides IoC.
Introducing Spring s Modules > This module supports both programmatic and declarative approaches for you to manage transactions. These can be used to add transaction capability to your simple Java objects. Introducing Spring s Modules > This module defines an abstract layer on top of the native JDBC API, which supports programming against JDBC in a template-based approach that dispenses with boilerplate code.
Introducing Spring s Modules > This module integrates popular object/relational mapping frameworks such as Hibernate, JDO, TopLink, ibatis, and JPA into Spring. Introducing Spring s Releases > XML schema based configuration: In Spring 1.x, XML bean configuration files only support DTDs, and everything you can define within them must be through the <bean> element. Spring 2.0 supports XML schema based configuration, which allows you to use Spring s new tags. As a result, your bean configuration files can be much simpler and clearer.
Introducing Spring s Releases > Annotation-driven configuration: As a complement to XML-based configuration, Spring 2.0 supports annotation-driven configuration in certain modules, such as @Required, @Transactional, @PersistenceContext, and @PersistenceUnit Introducing Spring s Releases > New Spring AOP approach: The classic AOP usage in Spring 1.x is through a set of proprietary Spring AOP APIs. Spring 2.0 introduces a brand-new AOP approach through writing POJOs with either AspectJ annotations or XML schema based configuration.
Introducing Spring s Releases > Easier transaction declaration: Declaring transactions in Spring 2.0 is much easier. You can use the new Spring AOP approach to declare transaction, or apply @Transactional annotations with the <tx:annotation-driven> tag. > JPA support: Spring 2.0 introduces support for the Java Persistence API in its ORM module. > Form tag library: Spring 2.0 introduces a new form tag library to make developing forms in Spring MVC easier. Introducing Spring s Releases > Asynchronous JMS support: Spring 1.x only supports receiving JMS messages synchronously through JmsTemplate. Spring 2.0 adds support for asynchronous JMS reception through message-driven POJOs. > Scripting language support: Spring 2.0 supports implementing beans with the scripting languages JRuby, Groovy, and BeanShell.
Introducing Spring s Releases > Annotation-driven configuration: Spring 2.5 supports more, including @Autowired and the JSR-250 annotations @Resource, @PostConstruct, and @PreDestroy. > Component scanning: Spring 2.5 can automatically detect your components with particular stereotype annotations from the classpath without manual configuration. Introducing Spring s Releases > AspectJ load-time weaving support: Spring 2.5 supports weaving AspectJ aspects into Spring IoC container at load time, so you use Aspect aspects beyond the reach of Spring AOP s support. > Annotation-based web controllers: Spring 2.5 supports a new annotation-based approach to web controller development. It can auto-detect your controller classes with the @Controller annotation, and also information you configure in @RequestMapping, @RequestParam, and @ModelAttribute annotations.
New Features & Enhancements in Spring 3.0 > Java 5 The entire framework code has been revised to take advantage of Java 5 features like generics, varargs and other language improvements > Improved documentation: The Spring reference documentation has also substantially been updated to reflect all of the changes and new features for Spring 3.0. New Features & Enhancements in Spring 3.0 > New module organization and build system Modules have been revised and are now managed separately with one source-tree per module jar: org.springframework.aop org.springframework.beans org.springframework.context org.springframework.context.support org.springframework.expression org.springframework.instrument org.springframework.jdbc org.springframework.jms org.springframework.orm org.springframework.oxm org.springframework.test org.springframework.transaction org.springframework.web org.springframework.web.portlet...
New Features & Enhancements in Spring 3.0 > Spring Expression Language > IoC enhancements/java based bean metadata > General-purpose type conversion system and field formatting system > Object to XML mapping functionality (OXM) moved from Spring Web Services project > Comprehensive REST support > @MVC additions including @CookieValue and @RequestHeader, to pull values from cookies and request headers, respectively > Declarative model validation > Embedded database support > Support for JSR-330 dependency injection specification Installing the Spring Framework > The Spring framework s installation is very simple. > You can simply download the Spring framework 2.5 or 3 release in ZIP format and extract it to a directory of your choice to complete the installation. > Before you install the Spring framework, you should have JDK installed on your machine. > Spring 2.5 requires JDK 1.4 or higher. You are strongly recommend that you install JDK 1.5 or higher so that you can use features such as annotations, autoboxing/unboxing, varargs, type-safe collections, and for-each loops.
Installing Spring > http://spring.io/projects Setting Up a Spring Project > We will use SpringSource Tool Suite 3.6.3.RELEASE
Setting Up a Spring Project > Create a Maven Project Setting Up a Spring Project > Open pom.xml and insert the following dependency <dependency> <groupid>org.springframework</groupid> <artifactid> spring-context </artifactid> <version>4.1.4.release</version> </dependency>
Setting Up a Spring Project import org.godel.domain.circle; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class TestSpring { public static void main(string[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( "spring-config.xml"); Circle circle = (Circle) context.getbean("unitcircle"); System.out.println(circle); context.close(); } } Setting Up a Spring Project package org.godel.test; import org.godel.domain.circle; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class TestSpring { public static void main(string[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-config.xml"); Circle circle = context.getbean("unitcircle",circle.class); System.out.println(circle); } }
Setting Up a Spring Project > Edit Spring XML config file: <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.xsd"> <bean id="unitcircle" class="org.godel.domain.circle"> <property name="radius" value="1.0"/> <property name="x" value="0.0"/> <property name="y" value="0.0"/> </bean> </beans> > Run the test code: Container Overview > Your application classes are combined with configuration metadata so that after the ApplicationContext is created and initialized, you have a fully configured and executable system or application.
Container Overview > Several implementations of the ApplicationContext interface are supplied out-of-the-box with Spring. > In standalone applications it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext. > While XML has been the traditional format for defining configuration metadata you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declaratively enable support for these additional metadata formats. Instantiating a container > Instantiating a Spring IoC container is straightforward. > The location path or paths supplied to an ApplicationContext constructor are actually resource strings that allow the container to load configuration metadata from a variety of external resources such as the local file system, from the Java CLASSPATH ApplicationContext context = ); new ClassPathXmlApplicationContext( new String[] { } "services.xml", "dao.xml"
Composing XML-based configuration metadata > It can be useful to have bean definitions span multiple XML files. > Each individual XML configuration file represents a logical layer or module in your architecture. <beans> <import resource="services.xml"/> <import resource="resources/messagesource.xml"/> <import resource="/resources/themesource.xml"/> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/> </beans>