Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
29 Cards in this Set
- Front
- Back
Spring Triangle
|
* Injection & dependency
* AOP * Enterprise Service Abstractions |
|
How Spring Works in High-Level
|
POJOs & Configuration Instruction -> Spring ApplicationContext CREATES Fully configured app sys (Ready for use)
|
|
One line to get context
|
ApplicationContext context
= new ClassPathXmlApplicationContext("application-config.xml"); |
|
How is Spring context implemented?
|
* "singletons" by default (one instance per bean)
* generate objects by reflection |
|
Reflection
|
Examples
* Class c = "foo".getClass() * c.getMethods() |
|
Dependency Injection
|
* Constructor: constructor-arg
* Setter: nested elements inside bean defn, i.e. <property name="repository" ref="repository"/> |
|
Case for constructor DI
|
* enforce mandatory dependencies [can also have required=true flag with setter DI]
* promote immutability (can assign dependencies to final fields) * concise for programmatic usage - creation and injection in one line of code |
|
Case for setters
|
* Allow optional dependencies and defaults
* Have descriptive names * follow JavaBean conventions * Inherited automatically |
|
Automatic Value Type Conversion
|
* value = "29" --> new Integer("29")
* property editor |
|
Singleton example
|
public class LegacySingleton {
(1) public static LegacySingleton inst = new LegacySingleton(); // public static new (2) private LegalSingleton(){} // private construction (3) public static LegacySingleton getInstance() { return inst; } // static getInstance() method to retrieve instance |
|
FactoryBean
|
implements FactoryBean & implements Object getObject() for beans which are complex to create
|
|
FactorBeans example
|
* JndiObjectFactoryBean
* FBs for creating remoting proxies * FBs for configuring data access technologies like Hibernate |
|
Where can a Spring app context be used?
|
Spring app contexts can be bootstrapped in any environment, e.g. JUnit, Web app, EJB, standalone app
|
|
.+ApplicationContext examples
|
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext XmlWebApplicationContext |
|
Resource Loading Prefixes
|
say, XmlWebAppContext
* classpath: * file: * http: |
|
recommendation for layering of app context files
|
Partitioning Configuration
1. app beans 2. infrastructure beans (env. specific) N.B. these two evolve at a different rate |
|
Benefits of DI
|
(1). Separation of concern -> Code reusability
(2). promotes programming to interfaces (3) improves testability - can unit test with stub dependencies (4) centralised control over object lifecycle |
|
Spring xml namespaces
|
context
tx util |
|
Phases of app life cycle
|
1. init
2. use 3. destruction (release any sys resources & now can eligible for garbage collection) |
|
Init phrase of life cycle
|
init phase completes when a context is created
(1) Load bean defns (2) initialise bean instances, i.e. new's |
|
Load bean defns
|
(1) *parse* XML files
(2) bean defns (indexed by own's id) are loaded into the context's BeanFactory [kinda like a *HashMap*] (3) *BeanFactoryPostProcessor* beans are invoked - can modify defns of any bean |
|
BeanFactory vs FactoryBean
|
BF: factory to get beans
FB: interface with getObject() |
|
BeanFactoryPostProcessor extension point
|
* useful for applying transformations to groups of bean defintions
* there are several useful implementions * can implement BFPP |
|
BFPP
|
BeanFactoryPostProcessor
|
|
example of BFPP
|
PropertyPlaceholderConfigurer
${vars} in bean defns are subs'ed with values from .properties file <bean class="PPC"> <property name="location" value="/WEB-INF/datasource.properties" /> more concise: <context:property-placeholder location="..."/> |
|
Initialise bean instances
|
* each bean is eagerly instantiated by default (w/ dependencies injected)
* after DI, each bean goes through a post-processing phase * after post-processing, bean init'ed and ready for use, tracked by id until context destroyed |
|
Application context - defn
|
Spring's *bootstrap* object that loads all the high-level (*entry-point*) objects that
are *required* by your application. |
|
DI - formal defn
|
A *pattern* (design) whereby *collaborators* are passed into an object from the
*outside* |
|
How does FactoryBean work?
|
1. created by Spring automatically
2. setters are called on the factory 3. getObject |