Skip to main content

Spring FrameWork

1. What is Tight Coupling?
When a class (ClassA) is dependent on another class’s object (ClassB), then we say ClassA is "tightly" Coupled with ClassB. Spring helps us to create classes in a way that Tight Coupling can be removed and Loose Coupling can be done.
2. What is Loose Coupling?
Loose Coupling removes the dependency of an object (ClassB) on a class (ClassA). Loose Coupling is approached by creating an interface and a setter & getter method, or by using a constructor which takes the interface object.
3. What are Beans in Spring?
When a class is annotated or decorated using the @Component, such a class is called a Bean in Spring. Beans are maintained by Application Context.
4. Explain Bean creation process?
The process of Bean creation has the following phases
(i) Starts with a class (c1) which has the annotation @Component.
(ii) Checks if the component annotated class (c1) is dependent.
(iii) If yes, then Spring will create a bean for that class (c2) too.
(iv) A connection or autowiring will occur between the two classes (c1 and c2) using @Autowired annotation and also through the constructor (c2) or the default case setClass Function (interface the Interface).
5. What is the importance of the annotation @Primary
This annotation is used on a class that needs to be taken by spring on a primary basis. For instance, if ClassX is @Component annotated and is dependent on both Class1 and Class2 (both @Component annotated) then the compiler would report an error. To show the primary class between Class1 and Class2 we use @Primary.
6. What is Dependency Injection?
Dependency Injection is where Spring searches for beans; once the appropriate bean is found, it autowires the bean to the dependent class. Dependency Injection is the process where Spring framework looks for the beans and identifies the dependencies, and creates the instances of beans and autowires them.
7. Explain Inversion of Control (IOC).
In Tight Coupling the dependent class takes the responsibility of creating its dependency. Whereas, in Loose Coupling, we use @Autowired annotation over the dependency class (or reference) and Spring takes control of creating the instance and injects the dependency.
8. What are the roles of an IOC (Inversion of Control) Container?
IOC Container does the following things-
 (i) Find Beans
(ii) Identify their dependencies and wire the dependencies
(iii) Manage Lifecycle of the Bean (creation, processing, and destruction)
9. What is Application Context?
It is an advanced version of IOC Container. It provides all the functionalities of Bean Factory and also provides things like AOP, Internationalization capabilities, web application context (request, session, etc).
10. Explain the process of creating an ApplicationContext in Spring.
The ApplicationContext can be defined in two ways (i) using XML, (ii) using @Configuration. Once the configuration is done in any of the ways defined above, the ApplicationContext is created using new ClassPathXmlApplicationContext. The ClassPathXmlApplicationContext looks for the XML files, using this is one of the two ways. The other way is to use AnnotationConfigApplicationContext.
11. Explain Component Scan.
Component Scan is one method of asking Spring to detect Spring-managed components, the input for this search is the packages. Two methods are available to define a Component Scan-
(i) Java Configuration; wherein, we use the @Component annotation to which we specify all the packages, for which Spring does the search.
(ii) XML Configuration- we use <context:component-scan base-package=”com.demo.compscanex”/>
12. How do you perform the same (above question) in Spring Boot?
In Spring Boot the annotation used to perform the scan is @SpringBootApplication. This annotation on a class would automatically initiate the component scan on the package where they are in.
13. Differentiate @Component, @Repository and @Service and @Controller?
Typically a web application is developed in layers like the controller (which is the initial point of client communication), business (where the actual code or logic of the application is written) and DAO (where the database connections and interaction happens). In such an architecture web application, @Component can be used in any of the layers. Whereas, the @Controller is used in the controller/web layer. @Service is used in the business layer and @Repository is used in the DAO layer.
14. List out the different scopes of Bean.
(i) Singleton: throughout the spring context only one instance is created.
(ii) Prototype: a new bean is created whenever requested.
(iii) Request: Every HTTP Request creates a bean.
(iv) Session: A bean for every HTTP Session.
15. List out the types of Dependency Injection.
The types of Dependency Injection-
(i) Setter Injection and (ii) Constructor Injection.
16. What is the difference between the Configuration types XML and Annotation?
These are the two ways of setting up the configuration, and they perform in the say way. Though, when the annotation approach is taken very less amount of code is written and the result would be the same as compared to the XML approach.
17. List out the ways Autowiring is done.
(i) byType
(ii) byName
(iii) Constructor (same as byType, but through constructor)
18. What is Dirty Read?
When a transaction (t1) is meant to read the changes that are performed by another transaction (t2) and provided transaction t2 is not committed yet; then in such a situation, the transaction t1 is called Dirty Read transaction.
19. List out the new features available in Spring Framework 4.0 and Spring Framework 5.0?
Spring 4.0 is the first to support Java features. Spring 5.0 has the support for Reactive Programming and Kotlin.
20. What is a FrontController?
In FrontController, the Servlet will not get the first request; the first request would go to FrontController and the request is passed on to the right servlet. In other words, DispatcherServlet is the front controller which intercepts all the requests from the client and then dispatches to appropriate controllers.
21. What is a ViewResolver?
ViewResolver enables a web application to select its view (such as JSP) dynamically. ViewResolver gets a name which is appended by /WEB-INF/views and a .jsp. All the display on the content is done in an HTML page.
22. List out all the concepts that are available in the MVC Architecture?
(i)  The browser sends a request to DispatcherServlet
(ii) DispatcherServlet knows the HanderMapping and can find the appropriate controllers
(iii) Controllers execute the request and put the data in the model and return back the view name to the DispatcherServlet.
(iv) DispatcherServlet uses the view name and ViewResolver to map to the view.
23. Explain Model Attribute?
The annotation @ModelAttribute is decorated on a method typically present inside a Controller. This will help the method to be available in all other methods available in the controller.
24. What is a Session Attribute?
The annotation @SessionAttributes (“argument”) is decorated on class (Controller). The attribute (argument) that is present in Model is available in the session.
25. Define @ControllerAdvice?
This annotation is used when logic needs to be implemented commonly in multiple classes (Controllers). For instance, if an Exception or its subclasses, or when an exception is raised in the classes, it will be handled by a method annotated with @ExceptionHandler. Whenever an exception occurs in any of the controllers, the exception is handled by the method annotated with @ExceptionHandler.
26. Why Spring Boot?
Spring-based applications have a lot of configuration (boiler-plate code). In Spring MVC, a lot of configuration is required (like component scan, dispatcher servlet, view resolver, etc). Whereas, in Spring Boot the boiler-plate code is not required.
27. Spring vs Spring MVC vs Spring Boot?
Spring: the most important feature of Spring is Dependency Injection or Inversion of Control.
Spring MVC: provides a decoupled approach in developing web applications. Concepts like DispatcherServlet, ModelAndView, ViewResolver makes web application development easy.
Spring Boot: makes the configuration very easy and automatic using a feature called Auto Configuration, in which the DispatcherServlet is done by Spring internally.
28. What is the role of @SpringBootApplication?
This annotation is used to launch up the entire application. Internally, @SpringBootApplication does the following,
@SpringBootConfiguration: same as @Configuration in a Spring Application.
@EnableAutoConfiguration: auto-configures the classes available in the classpath.
@ComponentScan: all the classes available under a package will be scanned when this annotation is applied.

29. Why do we use application.properties?
The file application.properties is used to configure things like database details, log generation, security (username/password), serialization, etc.
30. What is Spring JDBC?
Spring JDBC uses methods like update (query), execute (query) and query (SQL, resultSetExtractor) to interact with the database.
31. What is the difference between JDBC and Spring JDBC?
In JDBC, the checked exceptions need to be written; whereas, in Spring JDBC those exceptions are made into Runtime Exceptions. Which means, exception handling is not manually done in Spring JDBC.
32. What is JPA?
Java Persistence API (JPA) defines the mapping from Java Object to a Database Table. The procedure to map a Java object to a row in a database table is defined in JPA. JPA provides a lot of useful annotations, using which the relationship between classes and tables are defined.
33. What is Hibernate?
Once the mapping is done, Hibernate (a JPA Implementation) will help us create query under the hood and interact with the database.
34. Describe the cases in which the Dependency Injection is done through Constructors and Setters?
When the dependencies are required/mandatory, the Constructor approach is selected. And when the dependencies are optional then the Setters approach is used.
35. What is the importance of POM.XML file?
Project Object Model (POM) is an XML formatted file in which all the configuration for a maven project is defined. The most commonly used tags in POM.XML are <groupid>, <artifactId>, <version>, <packaging> and a few more.
36. What does the @RequestParam annotation do?
This allows the server side to read from data and automatically bind it to a parameter coming into the method.
37. What is Spring Security?
Spring Security provides security services to J2EE applications. Spring Security is implemented using Servlet Filters under the hood. Servlet Filters are used to pre-process or post-process web requests.

Comments

Popular posts from this blog

What is JDK, JRE and JVM?

What are JDK, JRE, and JVM: JDK :- Java Development Kit (in short JDK) is Kit which provides the environment to Develop and execute(run ) the Java program. For eg. You(as Java Developer) are developing an accounting application on your machine, so what do you going to need into your machine to develop and run this desktop app? You are going to need  J-D-K  for that purpose for this you just need to go to the official website of sun or oracle to download the latest version of JDK into your machine. Hence, JDK is a kit(or package) which includes two things i) Development Tools(to provide an environment to develop your java programs) and ii) JRE (to execute your java program). JDK is only used by Java Developers. JRE: - Java Runtime Environment (to say JRE) is an installation package which provides an environment to only run(not develop) the java program(or application)onto your machine. For eg(continuing with the same example) after developing your accounting ap...

What is Amazon Web Services (AWS)?

 Amazon Web Services Amazon Web Services is a cloud computing platform provided by Amazon. The AWS offers all three service models such as Software as a Service (SaaS), Infrastructure as a Service (IAAS), and Platform as a Service (PaaS). There are more services which comprise the Amazon Web Services including Amazon Elastic Compute Cloud (EC2) which provides virtual servers, Amazon Simple Storage Service (S3) which provides scalable storage for backups, analytics. Then there are other services such as Amazon relational database management system, DynamoDB, AWS Migration hub, and more. AWS provides services in almost every category from mobile development to data analytics. Benefits of using Amazon Web Services: AWS gives access to organizations to use programming models , database and operating system. It provides a cost effective service in which you only have to pay for what you use. Applications can be deployed in multiple regions with just a few clicks. ...

The Future of Java Developers in the Age of AI and ChatGPT

  In today's rapidly evolving tech landscape, AI tools like ChatGPT are becoming more integrated into the software development process. As a Java developer, whether you're just starting out or have years of experience, you might wonder: How can I stay relevant and succeed in the age of AI? In this post, we'll explore practical strategies to ensure you not only survive but thrive as a Java developer in this AI-driven world. 1. Embrace AI as a Tool, Not a Threat AI isn't here to replace you—it's here to help you. Tools like ChatGPT can automate repetitive tasks, assist with coding challenges, and even generate code snippets, allowing you to focus on more complex and creative aspects of development. For Freshers: Use AI tools to learn and understand Java more quickly. Let them assist you in writing code, but make sure you're also learning the logic and concepts behind it. For Experienced Developers: Integrate AI tools into your workflow to enhance productivity. L...