Day 10-Injecting Data, Autowiring, Bean Concepts
Day 10-Injecting Data, Autowiring, Bean Concepts
Training
2016
FOUNDATION TRAINING
Injecting data, Autowiring,
Bean Concepts
1
Deloitte Training 2016
J2EE
Day 10
3
Objectives of Day 10 Deloitte Training 2016
4
Spring Dependency Injection
Inversion of Control (IoC)
“Hollywood Principle”
Don't call me, I'll call you
“Container” resolves (injects) dependencies of
components by setting implementation object (push)
As opposed to component instantiating or Service
Locator pattern where component locates
implementation (pull), Martin Fowler calls
Dependency Injection
Dependency Injection Variants
Variations on dependency injection
Interface based (Avalon)
Setter-based (Spring)
7
What is a bean definition?
Defines a bean for Spring to manage
Key attributes
class (required): fully qualified java class name
id: the unique identifier for this bean
configuration: (singleton, init-method, etc.)
constructor-arg: arguments to pass to the
constructor at creation time
property: arguments to pass to the bean setters at
creation time
Collaborators: other beans needed in this bean
(a.k.a dependencies), specified in property or
constructor-arg
Typically defined in an XML file
8
Sample bean definition
11
How are beans injected?
• A dependency graph is constructed based on
the various bean definitions
• Beans are created using constructors (mostly
no-arg) or factory methods
• Dependencies that were not injected via
constructor are then injected using setters
• Any dependency that has not been created is
created as needed
12
Multiple bean config files
• There are 3 ways to load multiple bean config files (allows
for logical division of beans)
– Load multiple config files from web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/WEB-INF/spring-config.xml, classpath:/WEB-
INF/applicationContext.xml</param-value>
</context-param>
14
Anonymous vs ID
• Beans that do not need to be referenced
elsewhere can be defined anonymously
• This bean is identified (has an id) and can be
accessed to inject it into another bean
<bean id="exampleBean" class="org.example.ExampleBean">
<property name="anotherBean" ref="someOtherBean" />
</bean>
15
What is an inner bean?
<bean id="outer" class="org.example.SomeBean">
<property name="person">
<bean class="org.example.PersonImpl">
<property name="name"><value>Aaron</value></property>
<property name="age"><value>31</value></property>
</bean>
</property>
</bean>
16
Bean init-method
• The init method runs AFTER all bean
dependencies are loaded
– Constructor loads when the bean is first instantiated
– Allows the programmer to execute code once all
dependencies are present
17
Bean values
• Spring can inject more than just other beans
• Values on beans can be of a few types
– Direct value (string, int, etc.)
– Collection (list, set, map, props)
– Bean
– Compound property
<bean class="org.example.ExampleBean">
<property name="email">
<value>[email protected]</value>
</property>
</bean>
18
Abstract (parent) beans
• Allows definition of part of a bean which can be
reused many times in other bean definitions
19
Dependency Injection (cont'd)
• BeanFactory configured components need
have no Spring dependencies
– Simple JavaBeans
• Beans are singletons by default
• Properties may be simple values or
references to other beans
• Built-in support for defining Lists, Maps,
Sets, and Properties collection types.
– Custom PropertyEditors may be defined to
convert string values to other, arbitrary types.
XmlBeanFactory Example
• Property and constructor based IoC
<bean id="exampleBean" class="examples.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo"><ref bean="yetAnotherBean"/></property>
<property name="integerProperty">1</property>
</bean>
OR
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
MyBeanClass bean = (MyBeanClass)ctx.getBean(“myBean”);
ApplicationContext
• Extends functionality of BeanFactory
• Pre-instantiates singleton beans
• Detects and registers BeanPostProcessors and
BeanFactoryPostProcessors
• Supports nesting of contexts
• ApplicationListener and ApplicationEvents
– Initialized and closed predefined
– Custom may be created
• MessageSource provides i18n messaging
– <bean id=”messageSource”
class=”...ResourceBundleMessageSource”/>
– Contains list of bundle base names
Web Initialization
• Web applications may use
ContextLoaderListener to initialize Spring
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>database.properties</value></property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${database.connection.driver_class}</value>
</property>
<property name="url">
<value>${database.connection.url}</value>
</property>
</bean>
Summary Deloitte Training 2016
You have learned how to:
34