Spring Framework is one of the most popular and widely used Java frameworks which makes it very easy to create a Java application/web application using several out-of-box framework components provided by Spring. Thus, it is imperative that beginners starting to do Java web app would end up using Spring framework for building the app. From that perspective, it becomes of great importance for beginners to take note of some of the basic concepts related to which interview questions can be asked.
In this post, you will learn some of the Spring framework fundamentals concepts which could be asked in form of interview questions and answers. These questions and answers may found to be useful for freshers or beginners of Spring framework.
public class Laptop { private Processor processor; private RAM ram; private OperatingSystem operatingSystem; public Laptop(String processorType) { this.processor = ProcessorFactory.getProcessor(processorType); this.ram = new RAM(); this.operatingSystem = new OperatingSystem(); } }
With Spring Framework, the above code would look like this:
@Component public class Laptop { private Processor processor; private RAM ram; private OperatingSystem operatingSystem; public Laptop(@Autowired Processor processor, @Autowired RAM ram, @Autowired OperatingSystem operatingSystem) { this.processor = processor; this.ram = ram; this.operatingSystem = operatingSystem; } }
public class Car { private Engine engine; public Car() { this.engine = new Engine(); } }
The following is the code where object dependencies are injected:
@Component(name="car") public class Car { private Engine engine; public Car(@Autowired Engine engine) { this.engine = engine; } }
The above style of dependency injection is termed as injection via the constructor. Above code enables the Engine object to be injected (Autowired) during Runtime while creating a bean named car of a class type as Car. Dependencies can also be injected via setter methods. This post can be read for greater details.
public class Car { private Engine engine; public Car(@Autowired Engine engine) { this.engine = engine; } }
public class Car { @Autowired private Engine engine; public void setEngine(Engine engine) { this.engine = engine; } }
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
Later in the application, retrieve Bean objects using following code. In the code below, an instance of WebApplicationContext is obtained from which the bean instance is retrieved. Get the details on WebApplicationContextUtils from Spring Doc page on WebApplicationContextUtils.
WebApplicationContext webAppContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext()); SomeBean someBean = (SomeBean) webAppContext.getBean("someBean");
Different techniques are listed on this page.
In this post, you learned about fundamental concepts of Spring Framework in form of interview questions and answers. These questions could prove to be very helpful if you are a fresher or a beginner or junior developer and planning to attend upcoming interviews related to Spring framework.
Did you find this article useful? Do you have any questions or suggestions about this article in relation to interview questions and answers related to Spring framework? Leave a comment and ask your questions and I shall do my best to address your queries.
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…