Postingan

Menampilkan postingan dengan label spring interview questions

Difference between @Component, @Service, @Controller, and @Repository in Spring

Gambar
Before you learn the difference between @Component, @Service, @Controller, and @Repository annotations in Spring framework, it's important to understand the role of @Component annotation in Spring. During initial release of Spring, all beans are used to be declared in an XML file. For a large project, this quickly becomes a massive task and Spring guys recognize the problem rather quickly. In later versions, they provide annotation-based dependency injection and Java-based configuration. From Spring 2.5 annotation-based dependency injection was introduced, which automatically scans and register classes as Spring bean which is annotated using @Component annotation. This means you don't declare that bean using the <bean> tag and inject the dependency, it will be done automatically by Spring. This functionality was enabled and disabled using <context:component-scan> tag. Now that you know what does @Component annotation does let's see what does @Service, @Controlle...

What is the Role of InternalResourceViewResolver in Spring MVC? Interview Question

Gambar
Earlier, I have explained to you about how Spring MVC works internally and how it process HTTP request coming to your web application. One of the important parts of that processing was view resolution, which is handled by the ViewResolver interface. In this article, you'll learn more about it by explaining the InternalResourceViewResolver class. The InternalResourceViewResolver is an implementation of ViewResolver in Spring MVC framework which resolves logical view name e.g. "hello" to internal physical resources e.g. Servlet and JSP files e.g. jsp files placed under WEB-INF folder. It is a subclass of UrlBasedViewResolver, which uses "prefix" and "suffix" to convert a logical view name returned from Spring controller to map to actual, physical views. For example, if a user tries to access /home URL and HomeController returns "home" then DispatcherServlet will consult InternalResourceViewResolver and it will use prefix and suffix to find the ...

What is the Use of DispatcherServlet in Spring MVC Framework?

Gambar
If you have worked with Spring MVC then you should know what is a DispatcherServlet? It's actually the heart of Spring MVC, precisely the C of MVC design pattern or Controller. Every single web request which is supposed to be processed by Spring MVC goes through DispatcherServlet. In general, its an implementation of Front Controller Pattern which provides a single point of entry in your application. It handles all incoming requests. It is also the bridge between Java and Spring. Btw, the DispatcherServlet is like any other Servlet is declared in the web.xml with a URL pattern but the only special thing is that the URL pattern for dispatcher servlet is enough to map every single web request to DispathcherServlert. It is responsible for request handling by delegating requests to additional components of Spring MVC e.g. actual controller classes i.e. those which are annotated using @Controller or @RestController (in case of RESTful Web Services), Views, View Resolvers, handler mapper...

Difference between @Autowired and @Inject annotation in Spring?

Gambar
What is the difference between @Autowired and @Inject annotation in Spring is one of the frequently asked Spring questions on Java interviews? Since everybody is now moved or moving to annotation-driven and Java configuration in Spring, this question has become even more important for prospective candidates looking for a Java web development job using Spring framework. The @Autowired annotation is used for auto-wiring in Spring framework. If you don't know, autowiring is a process on which Spring framework figure out dependencies of a Spring bean, instead of you, a developer, explicitly specifying them in the application context file. You can annotate fields and constructor using @Autowired to tell Spring framework to find dependencies for you. The @Inject annotation also serves the same purpose, but the main difference between them is that @Inject is a standard annotation for dependency injection and @Autowired is spring specific . Since Spring is not the only fram...

Differences between @RequestParam and @PathVariable annotations in Spring MVC?

Gambar
The Spring MVC framework, one of the most popular frameworks for developing a web application in Java world also provides several useful annotations to extract data from the incoming request and mapping the request to the controller, e.g., @RequestMapping, @RequestParam, and @PathVariable. Even though both @RequestParam and @PathVariable is used to extract values from the HTTP request, there is a subtle difference between them, which makes them a useful question from an interview and spring certification point of view. We'll examine the subtle difference between @RequestParam and @PathVaraible in this article. As the name suggests, @RequestParam is used to get the request parameters from URL, also known as query parameters, while @PathVariable extracts values from URI. For example, if the incoming HTTP request to retrieve a book on topic "Java" is http://localhost:8080/shop/order/1001/receipts?date=12-05-2017, then you can use the @RequestParam annotation to retrieve the ...

What is SecurityContext and SecurityContextHolder in Spring Security?

Gambar
The SecurityContext and SecurityContextHolder are two fundamental classes of Spring Security. The SecurityContext is used to store the details of the currently authenticated user, also known as a principle. So, if you have to get the username or any other user details, you need to get this SecurityContext first. The SecurityContextHolder is a helper class, which provide access to the security context. By default, it uses a ThreadLocal object to store security context, which means that the security context is always available to methods in the same thread of execution, even if you don't pass the SecurityContext object around. Don't worry about the ThreadLocal memory leak in web application though, Spring Security takes care of cleaning ThreadLocal. Btw, that's not the only way a SecurityContextHolder can store current SecurityContext, it can be configured with a strategy on startup to specify how you would the context to be stored. For example, you can use SecurityContextHol...

2 Ways to setup LDAP Active Directory Authentication in Java - Spring Security Example Tutorial

Gambar
The LDAP authentication is one of the most popular authentication mechanism around the world for enterprise application and Active directory (an LDAP implementation by Microsoft for Windows) is another widely used LDAP server. In many projects, we need to authenticate against active directory using LDAP by credentials provided in the login screen. Sometimes this simple task gets tricky because of various issues faced during implementation and integration and no standard way of doing LDAP authentication in a Java web application . Even though Java provides LDAP support but in this article, I will mostly talk about spring security because of it's my preferred Java framework for authentication, authorization, and security-related stuff. We can do the same thing in Java by writing ower own program for doing LDAP search and then LDAP bind but as I said its much easier and cleaner when you use spring security for LDAP authentication. Along with LDAP Support, Spring Security also pro...

How to limit number of concurrent session in a Java web application using Spring Security?

Gambar
If you don't know, Spring security can limit the number of sessions a user can have in a Java web application. If you are developing a web application especially a secure web application in Java JEE then you must have come up with the requirement similar to many online banking portals have like only one session per user at a time or no concurrent session per user . If the user tries to open a new session then either an alert is shown or his previous session is closed. Even though you can also implement this functionality without using spring security but with Spring security, its just piece of cake with coffee :).  You just need to add a couple of lines of XML in your spring security configuration file and you are done. In order to implement this functionality, you can use the <concurrency-control> tag. You can configure a maximum number of the session your application support and then Spring security will automatically detect if user breach that limits and direct them to ...

Top 22 Spring Interview Questions Answers for Java JEE Developers

Gambar
Spring framework interview questions are on the rise on Java EE and core Java interviews Spring, which is obvious given Spring is the best framework available for Java application development and now Spring IOC container and Spring MVC framework are used as a de-facto framework for all new Java development. Because of its popularity,  interview questions from spring framework are top on any list of Core Java Interview questions. I thought to put together some spring interview questions and answers which have appeared on many Java and J2EE interviews and useful for practicing before appearing on any Java Job interview. I first wrote this article a long back and even its content is still relevant I thought to update it, especially after finishing my list of Spring Boot Interview Questions recently. This list of Spring interview questions and answers contains questions from Spring fundamentals e.g. Spring IOC and Dependency Injection, Spring MVC Framework , Spring Security, Spri...

Difference between @ContextConfiguration and @SpringApplicationConfiguration in Spring Boot Testing?

Gambar
Even though both @ContextConfiguration and @SpringApplicationConfiguration annotations are used along with SpringJUnit4ClassRunner to specify how to load the Spring application context, there is a subtle difference between them. Although @ContextConfiguration does a great job in loading application context it doesn't take full advantage of Spring Boot features. Spring Boot applications are ultimately loaded by either SpringApplication ( in case of the JAR) or SpringBootServletInitializer. This class not only loads the application context but also enables logging and loading of external properties specified in application.properties or application.yml file, and other features of the Spring Boot framework, which is not loaded or enabled by the @ContextConfiguration annotation. In short, it's better to use the @SpringApplicatoinConfiguration annotations rather than @ContextConfiguration for writing an integration test for Spring boot application, including a test for web pages o...