Spring Flashcards
(4 cards)
1
Q
Dependency Injection
A
- A design pattern that achieves loose coupling
- The process of passing one instantiated object to another that needs it (has-a relationship) via constructor or a setter
Steps
- Have at least 2 classes [and their interfaces] (one is a dependency of another)
- Write one class to accept an instance in the constructor
- Write the bean of the dependency
- Write the bean of the other class
- Pass the dependency as a constructor argument
1.
2
Q
Bean
A
- basic java object that is created and managed by the Spring Container
- must have getters and setters
- must have parametrized constructor (?)
In XML
- id
- unique identifier for that bean
- Used when retrieving bean from Spring Container
- class
- the fully-qualified name of the class
3
Q
Inversion of Control
A
- paradigm in which ocnstructor and management of objects is outsourced to an object factory
Steps
- Create beans in XML file
- Must include both id and class
- Create Spring Container (Application Context)
- Initialize context by referencing xml file
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“file.xml”);
- Retrieve bean from Spring Container
- Get bean using id and interface
- Obj myObj = context.getBean(“theBean”, Obj.class)
- Use new object
- Close context
- context.close();
4
Q
Spring Container
A
- Also known as an Application Context
- Essecially an special object that can be used for both IoC and DI
- Object that is used to perform important Spring functionality