Introduction To Spring Framework and Dependency Injection: Aaron Zeckoski
Introduction To Spring Framework and Dependency Injection: Aaron Zeckoski
Aaron Zeckoski
azeckoski@gmail.com
Sakai Montreal CRIM Workshop
Creative Commons AttributionNonCommercial-ShareAlike 2.5 License
Spring Framework
A popular and stable Java application framework for enterprise development
Ubiquitous for Java development Well established in enterprise Java apps Time tested and proven reliable
More Spring
Considered an alternative / replacement for the Enterprise JavaBean (EJB) model Flexible
Programmers decide how to program
Cross-Cutting Concerns
Program aspects that affect many others (e.g. logging)
What is a bean?
Typical java bean with a unique id In spring there are basically two types
Singleton
One instance of the bean created and referenced each time it is requested
Prototype (non-singleton)
New bean created each time Same as new ClassName()
Spring uses a BeanFactory to create, manage and locate beans which are basically instances of a class
Typical usage is an XML bean factory which allows configuration via XML files
12
Often they are created when the factory loads the definitions Can override this behavior in bean
<bean class=className lazy-init=true />
You can also override this in the factory or context but this is not recommended
13
14
Load multiple config files using Resources in the application context constructor
Recommended by the spring team Not always possible though
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
15
Bean properties?
The primary method of dependency injection Can be another bean, value, collection, etc.
<bean id="exampleBean" class="org.example.ExampleBean"> <property name="anotherBean"> <ref bean="someOtherBean" /> </property> </bean>
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
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 /> public class ExampleBean { public void init() { // do something } }
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
Example of injecting a string value <bean class="org.example.ExampleBean"> <property name="email"> <value>azeckoski@gmail.com</value> </property> </bean>
20
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
Before
Executes before joinpoint, cannot stop execution
Throws
Executes code if exception is thrown
After return
Executes code after normal joinpoint execution
23
25
Working example
Lets look at some example code pre and post spring
Simple application that allows a user to add, remove, and list a set of strings
Example App
The example app is a simple command line Java app which is meant to demonstrate a reasonable dependency structure This app allows a user to save, delete, and list a set of strings associated with their username
27
Charlie
DeltaImpl
Delta
B = A depends on B
28
Non-spring version
Involves using new to create needed dependencies Each class must know about the dependencies that it needs Singletons have to be created and handed to the classes that need them at the same time or you need a static way to access them (or a framework) Tightly coupled code structure
29
Spring version
No more new use Classes only have to know about the interface
or class if no interface available
30
Questions?
Spring framework
http://www.springframework.org/
31