Spring Interview Module 1 Container, Dependency, and IoC Flashcards
what is loose coupling?
- tight coupling: require tightly coupled dependency to function. if the dependency changes, then the function need difficult refactoring
- loose coupling: instead of hard code the dependency, dependency is not directly coupled but loosely defined. the actual dependency can be flexibly determined later
e.g. autowire service in controller, the exact implementation of service dependency can be created separately
what is a dependency?
another class depends on to provide its service
what is IOC inversion of control?
normal way for a parent class to have a dependency, dependency is hardcoded and instantiated by the parent class
IOC allows spring to manage the instantiation and injection of dependency bean into parent class instead
what is dependency injection?
spring framework search for the dependency bean, instantiate the bean, and wire the bean into dependent class for us. instead of we hardcode the dependency when instantiate a new bean by constructing
e.g. @Autowired
what is auto wiring?
process of spring searching for the dependency bean and instantiate beans and wire dependencies
what are the important roles of an IOC container?
- find / identify beans
- wire dependencies : instantiate and inject the dependencies
- manage lifecycle of the bean: instantiate, process bean lifecycle events, destroy bean after complete use
what are bean factory and application context?
can you compare bean factory with application context?
bean factory is basic version. like IOC container
application context: IOC container features
- spring AOP features
- I18N capabilities
- WebApplicationContext for web applications etc
most of time, spring recommends using application context. only in servere memory constraints, bean factory is used
how do you create an application context with spring?
- XML + ApplicationContext context = new ClassPathXmlApplicatonContext(new String[] {“BusinessApplicationContext.xml”})
- JAVA @Configuration + ApplicationContext context = new AnnotationConfigApplciationContext(SpringContext.class)
- create in unit test : @RunWith(SpringJunit4ClassRunner.class) @ContextConfiguration()
how does spring know where to search for components or beans?
what is a component scan?
scanning all packages can cause performance issues
search spring does to find components. input is given by coder
how do you define a component scan in XML ans JAVA configurations?
scan range can be defined by XML or by @ComponentScan
how is component scan done with spirng boot?
@SpringBootApplication or @SpringBootTest
auto initiate a scan of the the package the class with annotation is defined. therefore, if all components are in sub packages, no need to define component scan explicitly
what does @Component signify?
notify spring to manage the bean
what does @autowire signify?
notify spring should find the matching bean and wire the dependency in
what is the difference between @controller, @component, @repository and @service annotations in spring ?
@component: generic component
@repository: encapsulating storage, retrieval, and search behvaiour typically from a relational database. spring will automatically add exception translation for JDBC exceptions into spring exception
@service : business service facade
@controller: controller in MVC pattern
specific annotations signify the component is for specific layer and allow for AOP process on specific layer only
what is the default scope of a bean?
what are the other scopes available?
singleton - one instance per spring context
prototype - new bean whenever requested
request - one bean per HTTP request, web-aware spring applicationContext
session - one bean per HTTP session. web-aware spring ApplicationContext
by default singleton type
are spring beans thread safe?
not thread safe by default as singleton by default. one bean for multiple threads execution. to be thread safe, need ot use other scope types of beans to have a new bean per thread
how is spring’s singleton bean different from gang of four singleton pattern?
gang of four defines singleton as having one and only one instance per ClassLoader. however, spring singleton is defined as one instance of bean definition per container. so if multiple spring ApplicationContext exist, then more than one singleton can exist
what are the different types of dependecy injections?
what is setter injection?
what is constructor injection?
two ways to inject setter vs constructor
setter: @autowired on top of setter method in parent component. when setter method is not defined, @autowired on top of a property directly
constructor: @autowired on constructor method
how do you choose between setter and constructor injections ?
two types of dependecy: optional and mandatory
spring recommends constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null
when use constructor, if too many dependecies, it is obvious in the contructor method. highlight refatorying is required as bean is doing too many jobs
setter injection will mutate object one property at one time. if dependecy is null, setter still allow it to happen as optional dependecy
therefore, use constructor injection for mandatory dependecy and setter for optional
what are the different options available to create applicationContext for spring?
what is the difference between XML and java configurations for spring?
how do you choose between xml and java configurations for spring ?
with use of annotations, the things that are specified in a applicationContext are at bare minimum.
there is little to choose between these as long as the configuration is at a bare minimum
with spring boot, slowly moving to using java configuration as less configuration need to be done compare to loading configuration from xml
how does spring do autowiring?
what are the different kinds of matching used by spring for autowiring ?
by type - class or interface. if dependecies is interface, search for implementation
by name - multiple implementations for an interface, depends on the bean name used in caller e.g. @Autowired serviceInterface serviceImplementationMethodClassName
constructor - similar to by type
how do you debug problems with spring bean definition ?
NoUNiqueBeanDefinitionException: more than one matching components to implement interface and name used in caller is not helping to determine which one
NoSuchBeanDefinitionException : not annotated with @component properly or component scan is not proper
what is @Primary?
when multiple beans found matching, primay one is returned by default
what is @Qualifier
e.g. @Autowired @Qualifier(“qualifier name”) on calling side, on component also add @Qualifier(“qualifier name”)
what is CDI contexts and dependecy injection?
does spring support CDI?
JAVA EE dependency injection standard JSR-330
API tries to standardize dependency injection
spring supports most annotations in the standard
- @inject = @autowired
- @named = @component @ qualifier
- @Singleton defined a scope of Singleton
would you recommend to use CDI or spring annotations ?
CDI is a standard. spring has more complementing features usually compare to the standard
what are the major features in different versions of spring?
what are the new features in spring framewor 4.0?
what are the new features in spring framework 5.0 ?
spirng 2.5 made annotation-driven configuration possible
spring 3.0 made great use of java 5 improvements in language
spring 4.0 first version to fully support java 8 features
min version of java to use spring 4 is java se 6
introduced @RestController annotation. useful for restful services
spring 4.1 supports JCache JSR-107 annotations
spring 5.0: functional web framework, suuport for jigsaw (java modularity to reduce memory cost java), support for reactive programming(building applications around events), support for kotlin(reduce lines of code)
spring @controller vs @restcontroller
@restcontroller is a specialized version of @controller introduced in spring 4.0. it indicates a controller in a RESTful web service
@restcontroller = @controller + @responsebody. therefore, no need to add @responsebody annotation for every handler method
@controller is introduced in spring 2.5
what are the important spring modules?
modules for different tier / layer in web application architecture and also cross-cutting concerns
1. data access / integration: for data layer in web application. jdbc, orm, oxm, JMS(talk to other service over message queues), Transactions
2. web: web socket, servlet, web, portlet
3. core container:beans, core, context, SpEl (spring expression language to support bean definition)
4. test
5. cross-cutting: AOP(aspect oriented programming), Aspects, instrumentation, messaging
all modules share the same version as spring framework e.g. spring 4.0, all modules have 4.0 as release version
what are the spring projects ?
spring boot :quick build application
spring cloud: build cloud native application
spring data: consistent data access to veriaty of databases e.g. nosql
spring batch: batch applications
spring security: secutiry
spring HATEOAS: HATEOAS return data and related links for resources
what is the simplest way of ensuring that we are using single version of all spring related dependecies?
use BOM bill of material. all spring modules will use the same version
<artifactId>spring-framework-bom</artifactId>
<version>5.0.0.RELEASE</version>
what are some of the design patterns used in spring framework?
front controller - dispather servlet
prototype - beans
dependency injection
factory pattern - bean factory & applicationContext
template method - abstract method, jdbc template. provide a basic template wrapper. allow coder to override the common abstract methods
what are some of the important spring annotations?
@component, @service, @repository, @controller @autowired @primary @qualifier @configuration
what do you think about spring framework? why is spring popular?
can you give a big picture of the spring framework?
architecture - flexible & no restrictions. good support for many technologoes e.g. aspectJ, jsp,
design - loosely coupled
code - easy unit testing
features - dependency injection, IOC container, autowiring component scan
spring modules - useful
spring projects - increase usability and easy to work
What is dependency
injection
Dependency Injection is a technique of creating software in which objects do not create their
dependencies on itself, instead objects declare dependencies that they need and it is external
object job or framework job to provide concrete dependencies to objects.
Types of Dependency Injection:
Constructor injection
Setter injection
Interface injection
Advantages of using dependency injection is:
Increases code reusability
Increases code readability
Increases code maintainability
Increases code testability
Reduces coupling
Increases cohesion