Palo Alto Networks Most Frequently Asked Spring MVC Latest Interview Questions Answers
What NamedParameterJdbcTemplate in Spring?
The NamedParameterJdbcTemplate allow basic set of JDBC operations, it allows named parameter in the query instead of traditional (?) placeholder, the functionality is similar to JdbcTemplate class.
Example :
NamedParameterJdbcTemplate namedParameterJdbcTemplate;
String empInsrtQuery = "INSERT INTO Employee (name, age, salary) VALUES (:name, :age, :salary)";
Map namedParameters = new HashMap();
namedParameters.put("name", name);
namedParameters.put("age", age);
namedParameters.put("salary", salary);
namedParameterJdbcTemplate.update(empInsrtQuery, namedParameters);
What is spring framework? Why Java programmer should use Spring framework?
Very common Spring interview question, Spring is a framework which helps Java programmer in development. Spring provides dependency Injection and IOC container, Spring MVC flow and several useful API for Java programmer.
What is default scope of bean in Spring framework ?
The default scope of a Spring bean is Singleton scope.
Does Spring singleton beans are thread-safe ?
No, Spring singleton beans are not thread-safe. Singleton doesn't mean bean would be thread-safe.
What is dependency Injection?
Dependency Injection is one of the design pattern, which allows injecting dependency on Object, instead of object resolving the dependency.
What is the front controller class of Spring MVC?
A front controller is defined as “a controller which handles all requests for a Web Application.” DispatcherServlet (actually a servlet) is the front controller in Spring MVC that intercepts every request and then dispatches/forwards requests to an appropriate controller.
When a web request is sent to a Spring MVC application, dispatcher servlet first receives the request. Then it organizes the different components configured in Spring’s web application context (e.g. actual request handler controller and view resolvers) or annotations present in the controller itself, all needed to handle the request.
Difference between <context:annotation-config> vs <context:component-scan>?
1) First big difference between both tags is that <context:annotation-config> is used to activate applied annotations in already registered beans in application context. Note that it simply does not matter whether bean was registered by which mechanism e.g. using <context:component-scan> or it was defined in application-context.xml file itself.
2) Second difference is driven from first difference itself. It registers the beans defined in config file into context + it also scans the annotations inside beans and activate them. So <context:component-scan> does what <context:annotation-config> does, but additionally it scan the packages and register the beans in application context.
Difference between @Component, @Controller, @Repository & @Service annotations?
1) The @Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
@Component
public class EmployeeDAOImpl implements EmployeeDAO {
...
}
2) The @Repository annotation is a specialization of the @Component annotation with similar use and functionality. In addition to importing the DAOs into the DI container, it also makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException.
3) The @Service annotation is also a specialization of the component annotation. It doesn’t currently provide any additional behavior over the @Component annotation, but it’s a good idea to use @Service over @Component in service-layer classes because it specifies intent better.
4) @Controller annotation marks a class as a Spring Web MVC controller. It too is a @Component specialization, so beans marked with it are automatically imported into the DI container. When you add the @Controller annotation to a class, you can use another annotation i.e. @RequestMapping; to map URLs to instance methods of a class.
What are Advice, Aspect, Join-point and point cut in spring?
Advice :
An advice is an action taken by the aspect at particular join-point is called Advice.
Aspect :
An aspect is a subprogram which is associated with specific property of a program (Example separating logging code from the main program). An aspect is functionality or feature that cross cuts over object. AOP increase modularity of a program.
Join-Point :
A join point is a point used in spring AOP framework to represent a method execution. It always point during execution of program, method or exception. A join point is basically an opportunity within the code to apply aspect.
Point Cut :
In AOP a point cut is a set of many join points where an advice can execute. A chunk of code (known as Advice) associated with join point get executed.
What are the different types of Advice?
There are different types of Advice:
Before Advice :
The advice which executed before a join point called before advice. The before advice does not have the ability to prevent the execution flow proceeding at the join point (unless it throws an exception).
After Return Advice :
The advice which executed after a join point completed normally without any exception.
Around Advice :
It is responsible for choosing whether to proceeds to the join point or shortcut the advised method execution by returning its own return value or throwing an exception. This is most powerful kind of advice. With Around advice you can perform custom behavior before and after method execution.
After Throwing Advice :
The advice executed when a method throws an exception.
After (finally) Advice :
The advice is executed when program exits the join points either normally or by throwing an exception.
What is Weaving in Spring?
Weaving is the process of linking aspect with other application types or object to create an advised object. This can be performed at compile time, runtime and load time. In spring framework weaving is performed at runtime.
What is AOP Proxy?
AOP proxy is an object to implement the aspect contracts (advice method executions and so on). The AOP proxy is object is created by the AOP framework. In spring framework AOP proxy is JDK dynamic proxy or CGLIB proxy.
What is front controller in Spring MVC?
The Front Controller is basically a type of Design pattern which are being implemented in different framework (e.g. Struts and Spring MVC etc.).
In Spring MVC DispatcherServlet act as a Front Controller for the framework and responsible for intercepting every request and then dispatches/forwards request to appropriate controller. Configure the DispatcherServlet in the web.xml file of web application and request which we want to be handled by DispatcherServlet should be mapped using URL mapping.
For example all the requests ending with *.do will be handled by the DispatcherServlet.
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Difference between FileSystemResource and ClassPathResource.
In FileSystemResource you need to give the configuration file (i.e. spring-config.xml) relative to your project or the absolute location of the file.
In ClassPathResource spring looks for the file in the ClassPath so configuration (i.e. spring-config.xml) file should be included in the classpath. If spring-config.xml is in classpath, you can simply give the name of the file.
For Example: If your configuration file is at src/main/java/com/test/loadresource then your
FileSystemResource would be:
FileSystemResource resource = new FileSystemResource("src/main/java/com/test/loadresource/spring-config.xml");
And ClassPathResource would be :
ClassPathResource resource = new ClassPathResource("com/test/loadresource /spring-config.xml");
What NamedParameterJdbcTemplate in Spring?
The NamedParameterJdbcTemplate allow basic set of JDBC operations, it allows named parameter in the query instead of traditional (?) placeholder, the functionality is similar to JdbcTemplate class.
Example :
NamedParameterJdbcTemplate namedParameterJdbcTemplate;
String empInsrtQuery = "INSERT INTO Employee (name, age, salary) VALUES (:name, :age, :salary)";
Map namedParameters = new HashMap();
namedParameters.put("name", name);
namedParameters.put("age", age);
namedParameters.put("salary", salary);
namedParameterJdbcTemplate.update(empInsrtQuery, namedParameters);
What is spring framework? Why Java programmer should use Spring framework?
Very common Spring interview question, Spring is a framework which helps Java programmer in development. Spring provides dependency Injection and IOC container, Spring MVC flow and several useful API for Java programmer.
What is default scope of bean in Spring framework ?
The default scope of a Spring bean is Singleton scope.
Does Spring singleton beans are thread-safe ?
No, Spring singleton beans are not thread-safe. Singleton doesn't mean bean would be thread-safe.
What is dependency Injection?
Dependency Injection is one of the design pattern, which allows injecting dependency on Object, instead of object resolving the dependency.
What is the front controller class of Spring MVC?
A front controller is defined as “a controller which handles all requests for a Web Application.” DispatcherServlet (actually a servlet) is the front controller in Spring MVC that intercepts every request and then dispatches/forwards requests to an appropriate controller.
When a web request is sent to a Spring MVC application, dispatcher servlet first receives the request. Then it organizes the different components configured in Spring’s web application context (e.g. actual request handler controller and view resolvers) or annotations present in the controller itself, all needed to handle the request.
Difference between <context:annotation-config> vs <context:component-scan>?
1) First big difference between both tags is that <context:annotation-config> is used to activate applied annotations in already registered beans in application context. Note that it simply does not matter whether bean was registered by which mechanism e.g. using <context:component-scan> or it was defined in application-context.xml file itself.
2) Second difference is driven from first difference itself. It registers the beans defined in config file into context + it also scans the annotations inside beans and activate them. So <context:component-scan> does what <context:annotation-config> does, but additionally it scan the packages and register the beans in application context.
Difference between @Component, @Controller, @Repository & @Service annotations?
1) The @Component annotation marks a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. To use this annotation, apply it over class as below:
@Component
public class EmployeeDAOImpl implements EmployeeDAO {
...
}
2) The @Repository annotation is a specialization of the @Component annotation with similar use and functionality. In addition to importing the DAOs into the DI container, it also makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException.
3) The @Service annotation is also a specialization of the component annotation. It doesn’t currently provide any additional behavior over the @Component annotation, but it’s a good idea to use @Service over @Component in service-layer classes because it specifies intent better.
4) @Controller annotation marks a class as a Spring Web MVC controller. It too is a @Component specialization, so beans marked with it are automatically imported into the DI container. When you add the @Controller annotation to a class, you can use another annotation i.e. @RequestMapping; to map URLs to instance methods of a class.
Palo Alto Networks Most Frequently Asked Spring MVC Latest Interview Questions Answers |
Advice :
An advice is an action taken by the aspect at particular join-point is called Advice.
Aspect :
An aspect is a subprogram which is associated with specific property of a program (Example separating logging code from the main program). An aspect is functionality or feature that cross cuts over object. AOP increase modularity of a program.
Join-Point :
A join point is a point used in spring AOP framework to represent a method execution. It always point during execution of program, method or exception. A join point is basically an opportunity within the code to apply aspect.
Point Cut :
In AOP a point cut is a set of many join points where an advice can execute. A chunk of code (known as Advice) associated with join point get executed.
What are the different types of Advice?
There are different types of Advice:
Before Advice :
The advice which executed before a join point called before advice. The before advice does not have the ability to prevent the execution flow proceeding at the join point (unless it throws an exception).
After Return Advice :
The advice which executed after a join point completed normally without any exception.
Around Advice :
It is responsible for choosing whether to proceeds to the join point or shortcut the advised method execution by returning its own return value or throwing an exception. This is most powerful kind of advice. With Around advice you can perform custom behavior before and after method execution.
After Throwing Advice :
The advice executed when a method throws an exception.
After (finally) Advice :
The advice is executed when program exits the join points either normally or by throwing an exception.
What is Weaving in Spring?
Weaving is the process of linking aspect with other application types or object to create an advised object. This can be performed at compile time, runtime and load time. In spring framework weaving is performed at runtime.
What is AOP Proxy?
AOP proxy is an object to implement the aspect contracts (advice method executions and so on). The AOP proxy is object is created by the AOP framework. In spring framework AOP proxy is JDK dynamic proxy or CGLIB proxy.
What is front controller in Spring MVC?
The Front Controller is basically a type of Design pattern which are being implemented in different framework (e.g. Struts and Spring MVC etc.).
In Spring MVC DispatcherServlet act as a Front Controller for the framework and responsible for intercepting every request and then dispatches/forwards request to appropriate controller. Configure the DispatcherServlet in the web.xml file of web application and request which we want to be handled by DispatcherServlet should be mapped using URL mapping.
For example all the requests ending with *.do will be handled by the DispatcherServlet.
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Difference between FileSystemResource and ClassPathResource.
In FileSystemResource you need to give the configuration file (i.e. spring-config.xml) relative to your project or the absolute location of the file.
In ClassPathResource spring looks for the file in the ClassPath so configuration (i.e. spring-config.xml) file should be included in the classpath. If spring-config.xml is in classpath, you can simply give the name of the file.
For Example: If your configuration file is at src/main/java/com/test/loadresource then your
FileSystemResource would be:
FileSystemResource resource = new FileSystemResource("src/main/java/com/test/loadresource/spring-config.xml");
And ClassPathResource would be :
ClassPathResource resource = new ClassPathResource("com/test/loadresource /spring-config.xml");
Post a Comment