Spring Flashcards
What is Spring?
- Framework created to address the complexity of enterprise level application
- Includes an Inversion of Control container
- Involves layered architecture that users can select which aspects of Spring to use
- Components/Modules - Test, Core Container, Web, Data Access and Integration, AOP
Bean Scopes.
Prototype - Properties changed in one instance won’t affect other instances of that bean
Singleton - Properties changed on a global level
Request - (web service) scope of a single HTTP request
Session - (web service) within a session
GlobalSession - (web service) lifecycle of a global HTTP session
Spring Modules
Spring Web Spring Core Spring Context Spring Data Spring Security Spring AOP Spring MVC
Spring bean lifecycle
- Instantiate
- Populate properties
- setBeanName
- setBeanFactory
- setApplicationContext
- Preinitialization
- Initializing Bean
- Custom init
- Post Initialization
- Destroy
- Custom Destroy
What is the Spring Container?
IOC Container
Application Context
Bean Factory
ApplicationContext vs BeanFactory.
Bean Factory - no longer in use; lazy initialization for beans (created upon request)
ApplicationContext - supports Spring’s newer modules; eager initialization for beans (can also tell it to do lazy initialization); includes new features such as messaging
Inversion of Control.
- The idea that parts of the architecture you set up yourself, you pass on to some other entity to other handle for you.
- Don’t worry about the implementation, focus on the business logic.
- Spring uses dependency injection, which implements inversion of control.
Types of Dependency Injection
Constructor and setter
Different types of Bean wiring.
manual or autowiring
Types of autowiring?
byName and byType
How do I create a bean in the container?
- If you are using XML-based configuration metadata, you can specify the type (or class) of object that is to be instantiated using the ‘class’ attribute of the element.
- You can declare beans using the @Bean annotation in a configuration class.
- Or you can mark the class with one of the annotations from the org.springframework.stereotype package and leave the rest to component scanning.
@Autowired.
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities. Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.
@Autowired vs @Inject vs @Resource.
- @Autowired - Spring property annotation that inject a resource by-type, i.e. by the class or by the interface of the annotated field or constructor.
- @Inject - Identifies injectable constructors, methods, and fields. This annotation is an almost complete drop-in replacement for Spring’s @Autowired annotation. So, instead of using the Spring-specific @Autowired annotation, you might choose to use @Inject. One of the differences between @Autowired and @Inject is that @Inject does not have the required field so in case we fail to find a suitable object to injected it will fail while @Autowired can used required=false and allow null-able field (only if required!).
- @Resource - Is quite similar to @Autowired and @Inject, but the main difference is the execution paths taken to find out the required bean to inject. @Resource will narrow down the search first by name then by type and finally by Qualifiers (ignored if match is found by name). @Autowired and @Inject will narrow down the search first by type then by qualifier and finally by the name.
What is @RequestMapping?
Marks a method as an endpoint to which a client can connect, where you can specify the request type it will handle and the path of the endpoint.
@controller vs @restcontroller
Rest Controller annotation is the same as Controller annotation, but assuming that @ResponseBody is active by default, so all the json are parsed to java objects