Spring ioc interview questions and answers pdf




















AOPs use an advice as an interceptor that maintains a train of interceptors near the join point. Various types of advice are as follows: Before : These are types of advices, which get executed before the join point methods and can be configured using Before annotation mark.

After returning: These are the types of advices which get executed after the join point methods completes executing and the annotation mark used to configure it is AfterReturning After throwing: These are the types of advices that execute only and only if join point method returns by exiting an exception and annotation mark used to configure it is AfterThrowing.

Around: These are the types of advices that get executed before and after a join point and can be configured using the Around annotation mark. A bean is an object that is initialized, put together, and is managed by a Spring IoC container. The beans are created with the help of configuration metadata that are supplied to the container by the users. Singleton : This is the scope that the bean defines to a single instance for each Spring IoC container. Prototype : This is the scopes that a bean defines to have any number of object instances.

This is valid in the form of a web-aware Spring Application Context. One can configure spring into an application using three different ways. Confusing nature — Auto wiring is less exact than explicit wiring and thus it is not preferred if explicit wiring can be used. A module in spring which contains a set of APIs, which provides for crosscutting requirements is an Aspect. For example, a module that is used in logging is called AOP aspect for logging.

Any application can have as many aspects as per requirement. Join point is a point in an application where one can initialize an AOP aspect. One can also define it as the exact place in the application where an action is said to take place while using spring framework.

Pointcut is a collection of one or more join points where one advice can be executed. One can specify pointcut using patterns as well as expressions. Weaving is the process by which aspects can be linked with other application objects for creating the required object. Online Training Programs. Also Read: 30 Best Spring boot interview Questions These questions and answers on Spring are curated from various information sources to ensure that the reader is exposed to the variety of interview questions.

Subscribe Our NewsLetter. Subscribe Now. Join Onlineinterviewquestions. Support us by disabling your adblocker. Please Reload the page once you disabled the Adblocker. Core Java interview questions.

Spring Boot Interview questions. Java inheritance interview questions. Spring Batch Interview Questions. What are the different things that are defined in Starter Parent? How does Spring Boot enforce common dependency management for all its Starter projects? What is Spring Initializr? What is application. What are some of the important things that can customized in application. How do you externalize configuration using Spring Boot? How can you add custom application properties using Spring Boot?

What is ConfigurationProperties? What is a profile? How do you define beans for a specific profile? How do you create application configuration for a specific profile? How do you have different configuration for different environments? What is Spring Boot Actuator? How do you monitor web services using Spring Boot Actuator?

How do you find more information about your application envrionment using Spring Boot? What is a CommandLineRunner? How is different from JDBC? What is a JdbcTemplate? What is a RowMapper? What is JPA? What is Hibernate? How do you define an entity in JPA? What is an Entity Manager? What is a Persistence Context? How do you map relationships in JPA? What are the different types of relationships in JPA?

How do you define a datasource in a Spring Context? What is the use of persistence. How do you define transaction management for Spring — Hibernate integration?

Spring Data What is Spring Data? What is the need for Spring Data? What is a CrudRepository? What is a PagingAndSortingRepository? What is Mockito? What is your favorite mocking framework? How do you do mock data with Mockito? What are the different mocking annotations that you worked with? What is MockMvc? What is WebMvcTest?

What is MockBean? How do you write a unit test with MockMVC? Service indicates that a class is a service for implementing business logic in fact, it does not differ from Component, but simply helps the developer specify the semantic meaning of the class. To specify a container for a bin class, you can use any of these annotations. But different names make it possible to distinguish the purpose of a particular class.

DispatcherServlet — dispatcher servlet. This servlet parses the requests and sends them to the appropriate controller for processing. In Spring MVC, the DispatcherServlet class is the central servlet that receives requests and sends them to the appropriate controllers.

In the Spring MVC application, there can be an arbitrary number of DispatcherServlet instances designed for different purposes for example, to process user interface requests, REST web service requests, etc.

Each DispatcherServlet instance has its own WebApplicationContext configuration that defines the characteristics of the servlet level, such as controllers that support the servlet, the display of handlers, the recognition of representations, internationalization, theme design, validation, type conversion and formatting, etc.

The main purpose is to link the life cycle of ApplicationContext and ServletContext , as well as the automatic creation of ApplicationContext. You can use this class to access bins from various spring contexts. Configured in web. What is a ViewResolver in Spring? ViewResolver is a view recognizer. A variety of implementation classes are provided to support various presentation recognition mechanisms. Searches for a given path, prefix, suffix and name. The MultipartResolver interface is used to download files.

By default, this interface is not included in the application and you must specify it in the configuration file. Once configured, any download request will be sent to this interface. This recognizer handles certain standard Spring MVC exceptions by setting a special response status code. You can also implement your own exception handler by annotating the controller method using the ExceptionHandler annotationand passing an exception type as an attribute.

In general, exception handling can be described as follows:. Controller Based — specify methods for handling exceptions in the controller class. To do this, mark such methods with the ExceptionHandler annotation. Global Exception Handler — for handling global exceptions the spring provides an annotation ControllerAdvice. HandlerExceptionResolver implementation — The Spring Framework provides the HandlerExceptionResolver interface , which allows you to specify a global exception handler.

The implementation of this interface can be used to create your own global exception handlers in an application. In the independent Java program, ApplicationContext can be created as follows:. By specifying contextConfigLocation, you can specify multiple Spring configuration files. Parameters are separated by commas or spaces:. To create a simple Spring MVC application, follow these steps:. Spring MVC provides a very simple and convenient application localization feature.

To do this, do the following:. Give an example of frequently used Spring annotations. Controller is the controller front class in the Spring MVC project.

ResponseBody — allows you to send an Object in the response. Autowired — used for automatic binding of dependencies in spring beans. Service — indicates that the class performs service functions. The container throws BeanInitializationException if the affected bean property has not been populated. The Autowired annotation provides more fine-grained control over where and how auto wiring should be accomplished. When there are more than one beans of the same type and only one is needed to be wired with a property, [email protected] annotation is used along with Autowired annotation to remove the confusion by specifying which exact bean will be wired.

When using the Spring JDBC framework the burden of resource management and error handling is reduced. So developers only need to write the statements and queries to get the data to and from the database. JDBC can be used more efficiently with the help of a template class provided by the Spring framework, which is the JdbcTemplate. JdbcTemplate class provides many convenient methods for doing things such as converting database data into primitives or objects, executing prepared and callable statements, and providing custom database error handling.

This allows us to switch between the persistence technologies fairly easily and to code without worrying about catching exceptions that are specific to each technology. Most users of the Spring Framework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non-invasive lightweight container.

Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code. Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management.

The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules.

It is a module that has a set of APIs providing cross-cutting requirements. For example, a logging module would be called the AOP aspect for logging. An application can have any number of aspects depending on the requirement.

The Concern is the behavior we want to have in a module of an application. A Concern may be defined as functionality we want to implement. The cross-cutting concern is a concern that is applicable throughout the application and it affects the entire application. For example, logging, security, and data transfer are the concerns that are needed in almost every module of an application, hence they are cross-cutting concerns.

The joinpoint represents a point in an application where we can plug in an AOP aspect. It is the actual place in the application where an action will be taken using the Spring AOP framework. The advice is the actual action that will be taken either before or after the method execution. This is an actual piece of code that is invoked during the program execution by the Spring AOP framework.

Spring aspects can work with five kinds of advice:. The pointcut is a set of one or more joinpoints where advice should be executed.

You can specify pointcuts using expressions or patterns. A target object is an object being advised by one or more aspects. It will always be a proxy object. It is also referred to as the advised object. A proxy is an object that is created by applying advice to a target object. When you think of client objects the target object and the proxy object are the same. Weaving is the process of linking aspects with other application types or objects to create an advised object.



0コメント

  • 1000 / 1000