Introduction To Spring Framework and Dependency Injection: Aaron Zeckoski
Introduction To Spring Framework and Dependency Injection: Aaron Zeckoski
Spring Framework
and Dependency Injection
Aaron Zeckoski
[email protected]
URL: http://www.springframework.org/ 2
Spring code structure
3
More Spring
URL: http://en.wikipedia.org/wiki/Spring_framework 4
What does Spring offer?
Dependency Injection
Also known as IoC (Inversion of Control)
Aspect Oriented Programming
Runtime injection-based
Portable Service Abstractions
The rest of spring
ORM, DAO, Web MVC, Web, etc.
Allows access to these without knowing how they
actually work
5
Dependency Injection defined
Method to create needed dependencies or look
them up somehow without doing it in the
dependent code
Often called Inversion of Control (IoC)
IoC injects needed dependencies into the object
instead
Setters or Contructor
Primary goal is reduction of dependencies in
code
an excellent goal in any case
This is the central part of Spring
URL: http://en.wikipedia.org/wiki/Inversion_of_Control 6
Aspect Oriented Programming
defined
Attempts to separate concerns, increase
modularity, and decrease redundancy
Separation of Concerns (SoC)
Break up features to minimize overlap
Dont Repeat Yourself (DRY)
Minimize code duplication
Cross-Cutting Concerns
Program aspects that affect many others (e.g. logging)
AspectJ is the top AOP package
Java like syntax, IDE integration
URL: http://en.wikipedia.org/wiki/Aspect-oriented_programming 7
Portable Service Abstractions
defined
Services that easily move between
systems without heavy reworking
Ideally easy to run on any system
Abstraction without exposing service
dependencies
LDAP access without knowing what LDAP is
Database access without typical JDBC hoops
Basically everything in Spring that is not
IoC or AOP
8
What is a bean?
13
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
14
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>
16
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>
17
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>
18
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
<bean id="exampleBean" class=org.example.ExampleBean"
init-method=init />
19
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>
20
Abstract (parent) beans
Allows definition of part of a bean which can
be reused many times in other bean
definitions
<bean id="abstractBean" abstract="true" The parent bean
class="org.example.ParentBean"> defines 2 values (name,
<property name="name" value="parent-AZ"/> age)
<property name="age" value="31"/> The child bean uses the
</bean> parent age value (31)
The child bean
<bean id="childBean" overrides the parent
class="org.example.ChildBean" name value (from
parent="abstractBean" init-method="init"> parent-AZ to child-AZ)
<property name="name" value="child-AZ"/> Parent bean could not
</bean> be injected, child could
21
AOP in Spring
Provides way to create declarative services
and custom aspects
Transaction management is the most
common aspect (or concern)
Spring handles AOP via advisors or
interceptors
Interception point is a joinpoint
A set of joinpoints are called a pointcut
pointcuts are key to Spring AOP, they allow intercepts
without explicit knowledge of the OO hierarchy
Action taken by an interceptor is called advice
22
AOP advice types
Around
Most common and powerful
Execute code before and after joinpoint
Before
Executes before joinpoint, cannot stop execution
Throws
Executes code if exception is thrown
After return
Executes code after normal joinpoint execution
23
Spring AOP key points
Pure java implementation
Allows method interception
No field or property intercepts yet
AOP advice is specified using typical
bean definitions
Closely integrates with Spring IoC
Proxy based AOP
J2SE dynamic proxies or CGLIB proxies
Not a replacement for AspectJ
24
Example transaction proxy
This wraps a transaction interceptor around a DAO
<bean id="daoBean"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="daoBeanTarget"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
25
Working example
26
Example App
27
Example App Structure
Charlie handles
Charlie
application logic
Delta handles data
access
Dependency graph is DeltaImpl Delta
non-cyclical
No A => B => C => A A B = A depends on B
28
Non-spring version
29
Spring version
30
Questions?
Spring framework
http://www.springframework.org/
31