IQ-Spring


  • What is a Spring  ?

            an application framework and inversion of control container for the Java platform. The framework’s core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform.
           Spring is essentially a lightweight, integrated framework that can be used for developing enterprise applications in java.
---------------------------


  • What are the advantages of spring framework?

                     Predefined Templates
                     Loose Coupling
                     Easy to test
                     Lightweight
                     Fast Development
                     Powerful Abstraction
                     Declarative support
---------------------------


  • What are the modules of spring framework?

                   Some of the important Spring Framework modules are:

                                  Context: for dependency injection.
                                  AOP: for aspect oriented programming.
                                  DAO: for database operations using DAO pattern
                                  JDBC: for JDBC and DataSource support.
                                  ORM: for ORM tools support such as Hibernate
                                  Web Module: for creating web applications.
           MVC: Model-View-Controller implementation for creating web applications, web services etc.
---------------------------


  • What is IOC and DI?

             IOC (Inversion of Control) and DI (Dependency Injection) is a design pattern to provide loose coupling. It removes the dependency from the program.
---------------------------


  • List some of the important annotations in annotation-based Spring configuration.

                          The important annotations are:

                                 @Required
                                 @Autowired
                                 @Qualifier
                                 @Resource
                                 @PostConstruct
                                 @PreDestroy
---------------------------


  • What is the role of IOC container in spring?

                     IOC container is responsible to:
                     Create the instance
                     Configure the instance
                     Assemble the dependencies
---------------------------


  • What are the types of IOC container in spring?

                    There are two types of IOC containers in spring framework.

                               BeanFactory
                               ApplicationContext
---------------------------


  • What is Bean in Spring and List the different Scopes of Spring bean?

                 Beans are objects that form the backbone of a Spring application. They are managed by the Spring IoC container.
                 In other words, a bean is an object that is instantiated, assembled, and managed by a Spring IoC container.

          There are five Scopes defined in Spring beans.
         - Singleton: Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure spring bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues because it’s not thread-safe.
        - Prototype: A new instance will be created every time the bean is requested.
        - Request: This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
        - Session: A new bean will be created for each HTTP session by the container.
        - Global-session: This is used to create global session beans for Portlet applications.
---------------------------


  • What is the role of DispatcherServlet and ContextLoaderListener?

              DispatcherServlet is basically the front controller in the Spring MVC application as it loads the spring bean configuration file and initializes all the beans that have been configured.
             If annotations are enabled, it also scans the packages to configure any bean annotated with @Component, @Controller, @Repository or @Service annotations.

            ContextLoaderListener, on the other hand, is the listener to start up and shut down the WebApplicationContext in Spring root.
            Some of its important functions includes tying up the lifecycle of Application Context to the lifecycle of the ServletContext and automating the creation of ApplicationContext.
---------------------------


  • What are the differences between constructor injection and setter injection?


  1. No Partial Injection in Constructor Injection while Partial Injection in Setter Injection.
  2. Constructor Injection Desn’t override the setter property while Setter Injection Overrides the constructor property if both are defined.
  3. Constructor Injection creates new instance if any modification occurs while Setter Injection Doesn’t create new instance if you change the property value
  4. Constructor Injection is better for too many properties while Setter Injection Better for few properties.

---------------------------


  • What is autowiring in Spring? What are the autowiring modes?

              Autowiring enables the programmer to inject the bean automatically. We don’t need to write explicit injection logic. Let’s see the code to inject bean using dependency injection.

             <bean id="emp" class="com.javatpoint.Employee" autowire="byName" />
         The autowiring modes are given below:

         Mode : Description
        1 no:               this is the default mode, it means autowiring is not enabled.
        2 byName:     Injects the bean based on the property name. It uses setter method.
        3 byType:      Injects the bean based on the property type. It uses setter method.
        4 constructor: It injects the bean using constructor
---------------------------


  • What is the difference between BeanFactory and ApplicationContext ?

              BeanFactory is the basic container whereas ApplicationContext is the advanced container. ApplicationContext extends the BeanFactory interface.
             ApplicationContext provides more facilities than BeanFactory such as integration with spring AOP, message resource handling for i18n etc.
---------------------------


  • What are the transaction management supports provided by spring ?

            Spring framework provides two type of transaction management supports:

           Programmatic Transaction Management: should be used for few transaction operations.
           Declarative Transaction Management: should be used for many transaction operations.
---------------------------


  • How to handle exceptions in Spring MVC Framework ?

           Spring MVC Framework provides following ways to help us achieving robust exception handling.

         Controller Based:
              We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation.

        Global Exception Handler:
             Exception Handling is a cross-cutting concern and Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler.

       HandlerExceptionResolver implementation:
            For generic exceptions, most of the times we serve static pages. Spring Framework provides HandlerExceptionResolver interface that we can implement to create global exception handler. The reason behind this additional way to define global exception handler is that Spring framework also provides default implementation classes that we can define in our spring bean configuration file to get spring framework exception handling benefits.
---------------------------


  • What are the advantages of JdbcTemplate in spring ?

            Less code: By using the JdbcTemplate class, you don't need to create connection,statement,start transaction,commit transaction and close connection to execute different queries. You can execute the query directly.
---------------------------


  • What are classes for spring JDBC API ?

                        JdbcTemplate
                        SimpleJdbcTemplate
                        NamedParameterJdbcTemplate
                        SimpleJdbcInsert
                        SimpleJdbcCall
---------------------------


  • How can you fetch records by spring JdbcTemplate ?

             You can fetch records from the database by the query method of JdbcTemplate. There are two interfaces to do this:
                                ResultSetExtractor
                                RowMapper
---------------------------


  • What are the AOP terminology ?

             AOP terminologies or concepts are as follows:

                      JoinPoint
                      Advice
                      Pointcut
                      Aspect
                      Introduction
                      Target Object
                      Interceptor
                      AOP Proxy
                      Weaving
---------------------------


  • What is Aspect, Advice, Pointcut, JointPoint and Advice Arguments in AOP ?

           Aspect: Aspect is a class that implements cross-cutting concerns, such as transaction management. Aspects can be a normal class configured and then configured in Spring Bean configuration file or we can use Spring AspectJ support to declare a class as Aspect using @Aspect annotation.

         Advice: Advice is the action taken for a particular join point. In terms of programming, they are methods that gets executed when a specific join point with matching pointcut is reached in the application. You can think of Advices as Spring interceptors or Servlet Filters.

        Pointcut: Pointcut are regular expressions that is matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points. Spring framework uses the AspectJ pointcut expression language for determining the join points where advice methods will be applied.

       Join Point: A join point is the specific point in the application such as method execution, exception handling, changing object variable values etc. In Spring AOP a join points is always the execution of a method.

       Advice Arguments: We can pass arguments in the advice methods. We can use args() expression in the pointcut to be applied to any method that matches the argument pattern. If we use this, then we need to use the same name in the advice method from where argument type is determined.
---------------------------


  • What are the types of advice in AOP ?

              There are 5 types of advices in spring AOP.
                    Before Advice
                    After Advice
                    After Returning Advice
                    Throws Advice
                    Around Advice
---------------------------


  • What are the AOP implementation ?

             There are 3 AOP implementation.
                             Spring AOP
                             Apache AspectJ
                             JBoss AOP
---------------------------


  • What do you understand by Dependency Injection ?

            Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable. We can implement dependency injection pattern to move the dependency resolution from compile-time to runtime.

           Some of the benefits of using Dependency Injection are: Separation of Concerns, Boilerplate Code reduction, Configurable components and easy unit testing.
---------------------------


  • How do we implement DI in Spring Framework ?

             We can use Spring XML based as well as Annotation based configuration to implement DI in spring applications. For better understanding,
---------------------------


  • 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.
For example:
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
---------------------------


  • What is a MultipartResolver and when its used ?

            MultipartResolver interface is used for uploading files CommonsMultipartResolver and StandardServletMultipartResolver are two implementations provided by spring framework for file uploading. By default there are no multipart resolvers configured but to use them for uploading files, all we need to define a bean named "multipartResolver" with type as MultipartResolver in spring bean configurations.

           Once configured, any multipart request will be resolved by the configured MultipartResolver and pass on a wrapped HttpServletRequest. Then it’s used in the controller class to get the file and process it.
---------------------------


  • How to create ApplicationContext in a Java Program ?

           There are following ways to create spring context in a standalone java program.

           AnnotationConfigApplicationContext: If we are using Spring in standalone java applications and using annotations for Configuration, then we can use this to initialize the container and get the bean objects.
          ClassPathXmlApplicationContext: If we have spring bean configuration xml file in standalone application, then we can use this class to load the file and get the container object.
          FileSystemXmlApplicationContext: This is similar to ClassPathXmlApplicationContext except that the xml configuration file can be loaded from anywhere in the file system.


No comments:

Post a Comment

Home

Mastering Java Interview Questions: Your Comprehensive Guide         If you're preparing for a Java interview or just lookin...