Spring Intro
Spring Intro
• ApplicationContext
– ClassPathXmlApplicationContext
– FileSystemXmlApplicationContext
Metadata
• Container gets its instructions on what objects
to instantiate, configure, and assemble by
reading configuration metadata.
• In the form of
– XML
– Java annotations
– Java code
Bean Instantiation
Constructor
Static Factory
Instance Factory
Constructor
<beans>
<bean id="exampleBean" class="examples.ExampleBean"/>
<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
</beans>
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean>
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
</beans>
Static Factory Method
• Spring eagerly creates and configures all singleton beans as part of the
initialization process.
• Pre-instantiation is desirable, because errors in the configuration or
surrounding environment are discovered immediately
• Lazy-initialized bean tells the IoC container to create a bean instance when
it is first requested
Lazy Initialization - example
<bean id=“expensive" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<bean name=“lightWeight" class="com.foo.AnotherBean"/>
Autowiring
• Spring can autowire relationships between beans.
• Autowiring has the following advantages:
– Reduce the need to specify properties or constructor
arguments
– Can update a configuration as objects evolve. If a new
dependency is added to a class, that dependency can be
satisfied automatically without you needing to modify the
configuration.
• Specify autowire mode for a bean definition with the
autowire attribute of the <bean/> element
Autowiring Modes
• no: No autowiring
• byName: Autowiring by property name. Spring looks for a bean
with the same name as the property that needs to be autowired.
• byType: Allows a property to be autowired, if exactly one bean of
the property type exists in the container. If more than one exists, a
fatal exception is thrown
• constructor: Similar to byType, but applies to constructor
arguments
Disadvantages of autowiring
• Explicit dependencies in property and constructor-arg settings
always override autowiring
• You cannot autowire simple properties such as primitives, Strings,
and Classes
• Autowiring is less exact than explicit wiring
• Wiring information may not be available to tools that may
generate documentation from a Spring container.
• Multiple bean definitions within the container may match the
type specified by the setter method or constructor argument to
be autowired. If no unique bean definition is available, an
exception is thrown
Autowiring - Example
package com.examples.spring.core.autowire;
public class Customer {
private Person person;
public Customer(Person person) {
this.person = person;
}
package com.examples.spring.core.autowire;
public class Person {
//...
}
Autowiring – byName
<bean id="customer" class=“com.examples.g.spring.core.autowire.Customer"
autowire="byName" />
<bean id="person" class="com.examples.spring.core.autowire.Person" />
Autowiring - byType
<bean id="customer" class="com.examples.spring.core.autowire.Customer"
autowire="byType" />
<bean id="person" class="com. examples.spring.core.autowire.Person" />
Autowiring – constructor
<bean id="customer" class="com. examples.spring.core.autowire.Customer"
autowire="constructor" />
<bean id="person" class="com. examples.spring.core.autowire.Person" />
Initialization & Destruction Callbacks
<bean id=“greetings" class="com.training.spring.core" init-method="init"
destroy-method="cleanup"/>
Bean definition inheritance
<bean id="inheritedTestBean" abstract="true"
class="org.springframework.beans.TestBean">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithDifferentClass"
class="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override"/>
</bean>
Thank You!
38