Spring Interview Module 1 Container, Dependency, and IoC Flashcards
(93 cards)
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”)