Download - Spring essentials 1 (Spring Series 01)
SPRING ESSENTIALS 1Introduction to Spring and XML Configuration
(Spring Series 01)
Presenter: Heartin Jacob Kanikathottu
TOPICS
• History and Need for Spring
• Inversion of Control (IoC) and Dependency Injection (DI)
• Different Ways to Configure Spring 4 Applications
• Writing Spring Programs Using XML Configuration
• Constructor Injection and Setter Injection
• Autowiring
• Java Configuration and Annotations
HISTORY
• 1999 – EJB 1.1
• 2002 - Rod Johnson, released the Spring framework with his book
• 2003 – First version of Spring framework released.
• 2004 - First milestone release of Spring framework (1.0)
• 2006 - Spring 2.0, simplified the XML config files.
• 2007 - Spring 2.5, introduced annotation configurations.
• 2012 - Spring 3.2, Java configuration, Java 7, Hibernate 4, Servlet 3.0.
• 2014 - Spring 4.0, Java 8, Spring Boot.
INVERSION OF CONTROL (IOC) AND DEPENDENCY INJECTION (DI)
• Inversion of Control is a pattern where the controls of dependencies are moved out
usually from program components to container.
• Two ways to achieve IOC:
• Dependency lookup is the looking up of dependencies using container provided services,
like a JNDI lookup.
• Dependency injection is the process of injecting the dependencies of your class from
outside of the class.
• Note: DI is always preferred.
DI – SETTER VS. CONSTRUCTOR
• Setter Injection
• Advantage : Allows re-configuring of the components after its creation.
• Disadvantage: Might not be suitable if the order of invocation of setter methods is important.
• Constructor Injection
• Advantage : component will be in a consistent state ready to use immediately after its
creation.
• Disadvantages :
• Will not be able to reconfigure the component, unless setters are also present.
• Constructor injection cannot handle circular dependency: if two objects depend on the other through
constructor injection, both objects will not be created as both cannot be created without the other.
CONFIGURING SPRING 4 APPLICATIONS
• You can configure a Spring application entirely using xml configuration or using a
Java configuration.
• Annotation s can be used along with both XML and Java configurations.
• Java configuration is the most preferred approach in newer projects as:
• Can keep configurations at one centralized place (compared to all annotations approach)
• Can make use of Java compiler to check configs (compared to xml approach)
• Can use any of the Java language features to write any kind of (even complex) bean
configurations (compared to xml approach)
CONFIGURING SPRING 4 APPLICATIONS (CONTD…)
• Though annotations and Java configuration are the preferred approaches, xml
configurations might be handy in cases such as:
• We do not have access to the classes to write annotations.
• Some libraries (or some versions) might not support Java configurations completely.
• Some features might be significantly more difficult to do in Java configuration than on
XML due to various shortcuts and features available with XML configurations.
GETTING STARTED
• Spring Tool Suite (STS) / Intellij IDEA
• Any IDE (E.g. Eclipse) through Maven/ Gradle.
• Manual download of jars
• Not preferred
• Spring Boot (STS/Intellij/start.spring.io)
• Most preferred.
FIRST SPRING PROGRAM USING XML CONFIGURATION
• Problem Summary
• Create a simple class, JJWriter.java, with a single method write. Create a simple spring
config file, called spring.xml with a simple bean definition for JJWriter;:
• Maven dependency: spring-context
• Bean Definition: <bean id="jjwriter" class="com.example.JJWriter"> </bean>
• Loading xml config file and getting bean (from test class):
• AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
• JJWriter writer = context.getBean("jjwriter", JJWriter.class);
FIRST SPRING PROGRAM USING XML CONFIGURATION (CONTD…)
• Add a variable called data of type String to any of the class and do DI from bean def.
• <property name = "data" value="Sample Data"/> or
• <constructor-arg type="java.lang.String" value="Sample Data"></constructor-arg>
• Extended Problem Summary
• Create JJWriter.java, with a single method write and two subclasses – JJDatabaseWriter.java
and JJFileWriter.java – both overriding the write method.
• Bean definition: Bean Definition: <bean id="jjwriter" class="com.example. JJDatabaseWriter
"> </bean>
• Replace JJDatabaseWriter with JJFileWriter and execute again.
OBJECT INJECTION EXAMPLE
• Problem Statement
• Create a Point class with x and y coordinates, and then a Line class that has two Point objects.
Let Spring to inject the Point objects into the Line object based on bean definitions.
• Bean Definitions:
• <bean id="line" class="com.example.Line">
<property name="pointA" ref = "point1"></property>
<property name="pointB" ref = "point2"></property> </bean>
<bean id="point1" class="com.example.Point">
<property name="x" value ="1"></property>
<property name="y" value ="1"></property> </bean>
…
AUTOWIRING WITH XML CONFIGURATION
• Let spring to intelligently guess some of the bean injection without needing to
configure explicitly.
• Based on the type of the bean, name of the bean, and constructor of the bean.
• <bean id="line" class="com.example.Line“autowire="byName">
• Four ways to autowire:
• autowire="byName”
• autowire="byType”
• autowire="constructor“
• autowire="no” (the default)
JAVA CONFIGURATION
• Config class needs to be annotated with @Configuration.
• @Bean can be specified over a method that return a bean:
• @Bean (name = "jjwriter")
public JJWriter getJJWriter()
• We can load a config class as:
• ApplicationContext context = new
AnnotationConfigApplicationContext(DemoConfig.class);
• Config class can import other java configs using @Import and other non-java configs
using @ImportResource.
ANNOTATIONS
• @Component indicates that an annotated class is a "component".
• @ComponentScan annotation can be used to specify the packages that need to be
scanned for components.
• @Repository, @Service and @Controller can be used instead of @Component based on
actual application layer you are developing.
• @Autowired annotation provide autowiring over properties, setters and constructors
• @Qualifier annotation can be used with @Autowired to do autowiring by name.
• @Resource and @Inject are Java annotations similar to @Autowired. However,
@Resource, can specify autowiring by name instead of using @Qualifier annotation.
WHAT NEXT?
• Please complete doing examples for all concepts mentioned.
• We will go deep into Java Configurations, Annotations, Profiles, Junit Testing and
eventually start with Spring Boot.
RESOURCES & REFERENCES
• https://spring.io/docs
• http://javajee.com/spring-framework-4-essentials