How can we handle multiple beans of same type?
- We can use @Qualifier annotations, which lets us specify, which bean to inject When there are multiple beans available based on their name.
- We can also use @primary Annotation On one of the beans, which will mark as a default choice for injecting type.
Explain the helper class that initializes end destroys web application context?
- Class ContextLoaderListener using methods contextInitialized() and contextDestroyed()
Q What is AnnotationConfigApplicationContext ?
- Annotation Config application context is a standalone application context which accepts annotated classes as input. For instance @configuration or @component. Beans can be looked up with scan() or registered with register().
What will happen if using @service,@component over @Repository in DAO layer?
- If we use @Repository in DAO it gives the user a readable view, he is able to guess that this class contains DAO logic. If we use @Component its generic It’s hard to understand what logic is written there.
- @Repository Helps you to handle persistent related exceptions that spring provides.
- The generic class which handles all the DAO exceptions is called as DataAccessException class.
- Any exception thrown by our persistence layer, irrespective Of the JDBC we are using like hibernate, Spring JDBC or myBatis JDBC will be wrapped by spring exception called as DataAccessException.
What is a controller in spring mvc?
Controller is the class that takes care of all the client requests and send them to the configured resources to handle it.We can create a controller class by using
@Controller
annotation.
What is a front controller? What is its purpose?
org.springframework.web.servlet.DispatcherServlet
is the front controller class that initializes the context based on the spring beans configurations.
What is the difference between @RequestMapping and @ResponseBody annotations?
@RequestMapping maps the request to a specific view file as specified in view resolver.
@ResponseBody treats the response returned from function as the final response and no viewresolver is required in this case.
What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?
- These classes are used to give authority to spring to define objects for us.
- @Component is used to indicate that a class is a component. These classes are used for auto detection and configured as bean, when annotation based configurations are used.
- @Controller is a specific type of component, used in MVC applications and mostly used with RequestMapping annotation.
- @Repository annotation is used to indicate that a component is used as repository and a mechanism to store/retrieve/search data. We can apply this annotation with DAO pattern implementation classes.
- For DAO layer which interacts with database we define annotation @Repository.
- We add queries in this with respect to the database.
- @Service is used to indicate that a class is a Service. Usually the business facade classes that provide some services are annotated with this.
- For service classes where we define business logic we define @Service annotation
What is DispatcherServlet and ContextLoaderListener?
DispatcherServlet
is the front controller in the Spring MVC application and it loads the spring bean configuration file and initialize all the beans that are configured.If annotations are enabled, it also scans the packages and configure any bean annotated with
@Component
, @Controller
, @Repository
or @Service
annotations.ContextLoaderListener
is the listener to start up and shut down Spring’s root WebApplicationContext
. It’s important functions are to tie up the lifecycle of
ApplicationContext
to the lifecycle of the ServletContext
and to automate the creation of ApplicationContext
.
What is ViewResolver in Spring?
ViewResolver
implementations are used to resolve the view pages by name. Usually we configure it in the spring bean configuration file. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
InternalResourceViewResolver
is one of the implementation of ViewResolver
interface and we are providing the view pages directory and suffix location through the bean properties. So if a controller handler method returns “home”, view resolver will use view page located at /WEB-INF/views/home.jsp.
What is a MultipartResolver and when its used?
MultipartResolver
interface is used for uploading filesCommonsMultipartResolver
and StandardServletMultipartResolver
are two implementations provided by spring framework for file uploading.
Once configured, any multipart request will be resolved by the configured MultipartResolver and pass on a wrapped HttpServletRequest.
What is the difference between Bean Factory and Application Context?
BeanFactory is also called basic IOC and ApplicationContext is called Advanced IOC.
BeanFactory uses lazy initialization approach whereas ApplicationContext uses eager initialization approach.
BeanFactory creates a singleton bean only when it is requested from it but ApplicationContext creates all singleton beans at the time of its own initialization.
BeanFactory is also called basic IOC and ApplicationContext is called Advanced IOC.
BeanFactory uses lazy initialization approach whereas ApplicationContext uses eager initialization approach.
BeanFactory creates a singleton bean only when it is requested from it but ApplicationContext creates all singleton beans at the time of its own initialization.
What is a Bean cycle?
- We have 2 annotations @Postconstruct and @Pre destroy
- In @PostConstruct whenever our container is created our beans will be instantiated the Method annotated with @PostConstruct will be called first and then other logic will run.
- The Method annotated with @PreDestroy will Run once our container is about to get Destroyed and bean is about to get out of the container.
- We can use @PostConstruct while creating DB connection So that we don’t need to create it again and again.
- We can use @PreDestroy to close our DB connection Like a cleanup work.
- In spring we have couple of interfaces which do same work
- Initializing Bean
- DisposableBean
- Spring internal classes implement or extend to these interfaces.
How will the @PreDestroy Method be called in this case of standalone application?
- In a web Application when we use context.stop() it will be called but in a standalone application it won’t be called.
- If we use context.close() then the @predestroy annotated method will be called in a standalone application.
- It is always better to use context.registerShutDownHook() rather than context.close() as it asks JVM on exit to destroy Application context object.
What are different scopes of a bean?
- Singleton
- Spring container by default returns a Singleton bean for a class.
- Same object of class is returned by Spring Container each time which can be identified from the hashcode of the object.
- Prototype
- Spring container returns a New object for a class.
- We can change the scope of a been using annotation scope or by using scope property of been element in XML.
- Request
- Used for Web Applications
- Used for Http Requests
- Session
- Used for Web Applications
- Global session
- Used for Web Applications
What are the different stages of bean or explain bean life cycle?
- Spring provides two important methods to every bean
- Public void init()
- Initialization code loading config, connecting DB, web service etc.
- Public void destroy()
- Clean up code
- We can change the name of these methods but signature must be same.
- We give the Spring Bean And a configuration XML file to the spring container.
- This Spring container initializes the Spring bean and sets the values of its properties.
- It then calls the init() method.
- Next we can read and use the Bean.
- Next when the object is about to get destroyed it calls destroy() method to perform cleanup operation.
Q In how many ways we can configure a Spring Bean?
- XML
- Spring interface
- Annotation
- Constructor injection ambiguity. Problem comes when we try to overload a constructor. Thus At runtime when we try to create a bean using constructor injection, the JVM may find ambiguous arguments for different constructors. The priority of which constructor will be used is as follows.
- if we have a constructor with string arguments, then it is considered by default because all parameter types in constructor injection are treated as string by default.
- If we don’t have string argument constructor, then it will look for first constructor to which string can type casted too and we will use it.
- The direction of looking for first constructor depends on where your default constructor is, that is no argument Constructor is placed. If placed at the bottom, then it will look bottom up else it will look top down.
- Try adding no argument constructor annotation from Lombok Library and it will take the last constructor first, since it adds a no argument constructor in the end of the class.
- To Specifically tell JVM to use particular constructor use type property of constructor-arg tag as follows.
- We can also define order of Arguments using index property as follows.
<bean class="com.springimplant.util.Calc" name="Call" ><constructor-arg value="50" type="double" index="1"/><constructor-arg value="100" type="double" index="0"/></bean>
What is the concept of inner bean, and what are the disadvantages of inner bean?
- An inner bean Is a bean That is declared inside the scope of another bean.
- An inner bean Can only be used through outer bean As it encapsulates the outer bean.
- It cannot be injected in another bean.
- It stops accessibility of a bean by other beans.
- It is all about configuring one spring be inside another bean.
No comments:
Post a Comment