Spring
1. What is spring ? Why use spring ?
2. What is IoC ? What is DI ? Types of DI ? When to use which ?
3. What are the modules in spring ?
4. What is AOP ? Why use it ?
5. What is spring MVC? How to setup spring mvc?
6. What is lifecycle of bean?
7. What is beanfactory and application context? Differences?
8. What are scopes of bean in spring ? Differences?
9. Which design patters are used in spring?
10. What are the new features in spring 4?
11. What is bean auto wiring? What are different types of doing that?
12. How will you resolve circular dependency in spring?
13. What is aspect, joinpoint, pointcut, advice, introduction, target?
14. What is weaving? Types?
15. how to integrate hibernate with spring?
16. How to inject list?
17. how to develop rest web services in spring?
18. What are best practices in spring?
19. What is the difference between n-tier and MVC?
20. How is transaction management done in spring? Types? Prefer?
21. Design patterns used in spring?
22. What is dispatcher servlet?
23. Difference between @Component, @Repository, @Service ?
24. Types of IoC container in spring ? Differences ?
25. Spring AOP vs aspectJ AOP ?
26. How to handle exceptions in spring , spring mvc , sprin jdbc , etc?
27. Does spring bean provide thread safety ?
28. Can we have multiple spring configuration files?
29. What is spring xml based configuration and java based configuration and annotation based? Difference? Which one you prefer and use?
30. Are singleton beans thread safe ?
31. How to inject collections?
1. What is spring ? Why use spring ?
2. What is IoC ? What is DI ? Types of DI ? When to use which ?
3. What are the modules in spring ?
4. What is AOP ? Why use it ?
5. What is spring MVC? How to setup spring mvc?
6. What is lifecycle of bean?
7. What is beanfactory and application context? Differences?
8. What are scopes of bean in spring ? Differences?
9. Which design patters are used in spring?
10. What are the new features in spring 4?
11. What is bean auto wiring? What are different types of doing that?
12. How will you resolve circular dependency in spring?
13. What is aspect, joinpoint, pointcut, advice, introduction, target?
14. What is weaving? Types?
15. how to integrate hibernate with spring?
16. How to inject list?
17. how to develop rest web services in spring?
18. What are best practices in spring?
19. What is the difference between n-tier and MVC?
20. How is transaction management done in spring? Types? Prefer?
21. Design patterns used in spring?
22. What is dispatcher servlet?
23. Difference between @Component, @Repository, @Service ?
24. Types of IoC container in spring ? Differences ?
25. Spring AOP vs aspectJ AOP ?
26. How to handle exceptions in spring , spring mvc , sprin jdbc , etc?
27. Does spring bean provide thread safety ?
28. Can we have multiple spring configuration files?
29. What is spring xml based configuration and java based configuration and annotation based? Difference? Which one you prefer and use?
30. Are singleton beans thread safe ?
31. How to inject collections?
how spring simplifies jdbc ?
How validation in spring ?
What are different implementation/types of ApplicationContext ?
all the annotations in spring.
what are the annotation in spring rest?
what are common implementations of applicationContext?
What are exceptions thrown by spring dao?
Different jdbc template? purpose?
23. does spring framework supports all join points ?
How validation in spring ?
What are different implementation/types of ApplicationContext ?
all the annotations in spring.
what are the annotation in spring rest?
what are common implementations of applicationContext?
What are exceptions thrown by spring dao?
Different jdbc template? purpose?
23. does spring framework supports all join points ?
24. what is front controller class in spring mvc
25. @Controller
26. @RequestMapping
27. @PathVariable
28. ViewResolver ? Types ?
31. Benefits of spring tool suite ?
32. What is spring bean ?
33. Different ways of configuring spring bean ?
34.what is context loader listener ?
36. what is multipart resolver and when it's used ?
40. what is spring mvc interceptor ?
41. what is spring security ?
42. how to inject properties ?
48. Explain @Autowired
25. @Controller
26. @RequestMapping
27. @PathVariable
28. ViewResolver ? Types ?
31. Benefits of spring tool suite ?
32. What is spring bean ?
33. Different ways of configuring spring bean ?
34.what is context loader listener ?
36. what is multipart resolver and when it's used ?
40. what is spring mvc interceptor ?
41. what is spring security ?
42. how to inject properties ?
48. Explain @Autowired
49. Explain @Qualifier
50. Explain @Inject
51. Explain @Required
52. DIfferent types of events in spring ?
53. Difference between FileSystemResource , ClassPathREsource
what are inner beans ?
what are limitations in autowiring ?
how to turn on annotation wiring ?
what is spring dao ? (??)
spring aop target object ?
can there be more than one dispatcher servlet?
is it possible to have single context(root) for single dispatcher servlet.(yes)
50. Explain @Inject
51. Explain @Required
52. DIfferent types of events in spring ?
53. Difference between FileSystemResource , ClassPathREsource
what are inner beans ?
what are limitations in autowiring ?
how to turn on annotation wiring ?
what is spring dao ? (??)
spring aop target object ?
can there be more than one dispatcher servlet?
is it possible to have single context(root) for single dispatcher servlet.(yes)
Spring Top 10
Spring
1. What is spring ? Why use spring ?
Spring is an Inversion of Control and Aspected Oriented Programming based container framework.
Spring provides a lot of benefits:
a) Loosely coupled code: Since spring is IoC based it enforces loosely coupled code which has many benefits. Loosely coupled is easy to modify and test.
b) Reduces boilerplate code: Spring provides a lot of templates such as JdbcTemplate, HibernateTempleate which reduces a lot of boilerplate code.
c) Better Exception Handling: Spring has converted a lot of checked exceptions to unchecked and more meaningful exceptions.
d) Lightweight: Spring is very lightweight and has no additional overhead.
e) Provides integration with most of the major frameworks such as jdbc,hibernate,ibatis,struts,etc.
f) It provides support for web applications through spring MVC and support for developing rest web services.
g) Non-Invasive:
1. What is spring ? Why use spring ?
Spring is an Inversion of Control and Aspected Oriented Programming based container framework.
Spring provides a lot of benefits:
a) Loosely coupled code: Since spring is IoC based it enforces loosely coupled code which has many benefits. Loosely coupled is easy to modify and test.
b) Reduces boilerplate code: Spring provides a lot of templates such as JdbcTemplate, HibernateTempleate which reduces a lot of boilerplate code.
c) Better Exception Handling: Spring has converted a lot of checked exceptions to unchecked and more meaningful exceptions.
d) Lightweight: Spring is very lightweight and has no additional overhead.
e) Provides integration with most of the major frameworks such as jdbc,hibernate,ibatis,struts,etc.
f) It provides support for web applications through spring MVC and support for developing rest web services.
g) Non-Invasive:
2. What is IoC? What is DI? Types of DI? When to use which?
IoC stands for Inversion of Control. It refers to the change in program control flow from the code to some external entity. It leads to loosely coupled code which is easy to maintain,extend and test.
IoC can be achieved in various ways:
a) By factoy design pattern
b) By dependency injection
c) By interface injection
IoC stands for Inversion of Control. It refers to the change in program control flow from the code to some external entity. It leads to loosely coupled code which is easy to maintain,extend and test.
IoC can be achieved in various ways:
a) By factoy design pattern
b) By dependency injection
c) By interface injection
Spring uses dependency injection to achieve Inversion of control. DI is of two types:
a) Constructor based: For mandatory values
b) Setter based: For optional values.
a) Constructor based: For mandatory values
b) Setter based: For optional values.
3. What are the modules in spring?
IOC
AOP
MVC
JDBC
ORM
Test
IOC
AOP
MVC
JDBC
ORM
Test
4. What is AOP? Why use it?
AOP stands for Aspect Oriented Programming. It is a programming paradigm which modularize/encapsulate cross-cutting behaviour in an application. Benefits:
a) Modularization of cross-cutting behaviour, thus all the scattered code is at one place which is easy to modify.
b) Clear seperation of concerns.
c) In case when we don't have access to source code, we can implement new functionality.
AOP stands for Aspect Oriented Programming. It is a programming paradigm which modularize/encapsulate cross-cutting behaviour in an application. Benefits:
a) Modularization of cross-cutting behaviour, thus all the scattered code is at one place which is easy to modify.
b) Clear seperation of concerns.
c) In case when we don't have access to source code, we can implement new functionality.
5. What is spring MVC ? Benefits?
Spring provides MVC framework for web applications. MVC is a design patterns which provides seperation of roles among data, front end and business logic in an n-tier application.
Flow:1. Request comes to the Dispatcher Servlet. Dispatcher Servlet uses Handler Mapping and forwards the request to appropriate Controller.
2. The Controller calls the appropriate service methods which interacts with the model and create a ModelAndView object containing model data and view name and return it to Dispatcher Servlet.
3. Dispatcher Servlet passes the view name to View Resolver which returns the actual view.
4. Dispatcher Servlet passes the model data to View which renders the response and return the control to Dispatcher Servlet.
2. The Controller calls the appropriate service methods which interacts with the model and create a ModelAndView object containing model data and view name and return it to Dispatcher Servlet.
3. Dispatcher Servlet passes the view name to View Resolver which returns the actual view.
4. Dispatcher Servlet passes the model data to View which renders the response and return the control to Dispatcher Servlet.
Benefits:
a) Clean separation of roles: All the components have clear seperation of roles, thus it's easy to modify.
a) Modular design: All the components are loosely coupled and can be easily replaced.
a) Clean separation of roles: All the components have clear seperation of roles, thus it's easy to modify.
a) Modular design: All the components are loosely coupled and can be easily replaced.
Spring MVC sample application developed using STS : Download
6. What is lifecycle of bean?
a) Bean is intialized
b) If implements BeanNameAware , name is set.
c) If implements BeanFactory , (?)
d) If implements BeanPostProcessor , initalize method
e) init method
f) If implements BeanPostProcessor , destroy method
g) destroy method
a) Bean is intialized
b) If implements BeanNameAware , name is set.
c) If implements BeanFactory , (?)
d) If implements BeanPostProcessor , initalize method
e) init method
f) If implements BeanPostProcessor , destroy method
g) destroy method
7. What is beanfactory and application context? Differences?
BeanFacotory provides IoC.
ApplicationContext extends BeanFactory and provides a lot of additional benefits in a web application such as internationalization, handling bean life cycle events, reading properties, etc.
BeanFacotory provides IoC.
ApplicationContext extends BeanFactory and provides a lot of additional benefits in a web application such as internationalization, handling bean life cycle events, reading properties, etc.
8. What are scopes of bean in spring ? Differences ?
There are five types of scopes of bean in spring:
There are five types of scopes of bean in spring:
1. singleton(default*)
Scopes a single bean definition to a single object instance per Spring IoC container.
Scopes a single bean definition to a single object instance per Spring IoC container.
2. prototype
Scopes a single bean definition to any number of object instances.
Scopes a single bean definition to any number of object instances.
3. request
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
4. session
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
5. global session
Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
3,4,5 for web application.Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
9. Which design patterns are used in spring ?
a) MVC : Spring MVC
b) Front Controller design pattern: Spring MVC
c) Singleton: bean scopes
d) Prototype: bean scopes
e) Factory, Inversion of Control: BeanFactory and Application Context
f) Proxy
g) Template
a) MVC : Spring MVC
b) Front Controller design pattern: Spring MVC
c) Singleton: bean scopes
d) Prototype: bean scopes
e) Factory, Inversion of Control: BeanFactory and Application Context
f) Proxy
g) Template
10. What are the new features in spring 4?
a) Web Socket: duplex communication
b) Java 8 support
c) Getting current session of hibernate
d) Setting profiles
e) A lot of minor improvements
New features in spring 5:
a) Web Socket: duplex communication
b) Java 8 support
c) Getting current session of hibernate
d) Setting profiles
e) A lot of minor improvements
New features in spring 5:
At a high level, features of Spring Framework 5.0 can be categorized into:
- JDK baseline update.
- Core framework revision.
- Core container updates.
- Functional programming with Kotlin.
- Reactive Programming Model.
- Testing improvements.
- Library support.
- Discontinued support.
11. What is bean auto wiring? What are different types of doing that?
Bean auto-wiring is automatic configuration of beans in spring. There are different ways of doing it:
a) Name
b) Type
c) Constructor
d)
Auto-detect(Deprecated in Spring 3)
Bean auto-wiring is automatic configuration of beans in spring. There are different ways of doing it:
a) Name
b) Type
c) Constructor
d)
12. How will you resolve circular dependency in spring?
We'll use setter injection in this case. Spring can resolve circular dependency
We'll use setter injection in this case. Spring can resolve circular dependency
13. What is aspect, joinpoint, pointcut, advice, introduction, target?
Aspect: Aspect is the concern.
Joinpoint: The point at which new functionality has to be introduced.
Pointcut: Set of all the joinpoints where a particular functionality is to be introduced.
Advice: The functionality which is to be introduced.
Introduction: New method which is to be added to a code.
Target: The target class or code.
Aspect: Aspect is the concern.
Joinpoint: The point at which new functionality has to be introduced.
Pointcut: Set of all the joinpoints where a particular functionality is to be introduced.
Advice: The functionality which is to be introduced.
Introduction: New method which is to be added to a code.
Target: The target class or code.
14. What is weaving? Types?
The process of modification of the existing code to introduce the new functionality by aop is called weaving.
Types:
a)Compile time:
b)Classload time:
c)Runtime:
The process of modification of the existing code to introduce the new functionality by aop is called weaving.
Types:
a)Compile time:
b)Classload time:
c)Runtime:
15. How to integrate hibernate with spring?
Download sample project.
Spring Transaction
Create datasource
Create sessionfactory using datasource , hibernate4sessionfactorylocalbean..
Create spring transaction manager
For transaction use hibernate getCurrentSession
Hibernatetemplate is no more used
Declarative and programmatic transaction, declarative is preferred
hibernate session flush is necessary sometimes
getCurrentSession will work only with transaction, session using this shouldn't be closed.
<tx:annotation-driven/> is required to enable annotation support for transaction
OR use @EnableTransactionManagement
Name in href for jsp should be without .jsp.
Spring Validation
Uses hibernate validation
@Valid to put on model object
Errors will come in BindingResult object.
Add @ModelAttribute and define a method
In jsp file , use spring form taglib and create a form, give path to each field, and give commandName same as @ModelAttribute and pass it in method, define form:error and cssClass to display the error.
Spring Exception Handling
@ExceptionHandler(Exception.class) annotation on a method in the controller file will enable the method to handle that type of exception
Define a class and makr it with @ControllerAdvice to make it global exception handler, inside it same as first point
Also, set response status with error code with message,
Also, define the error pages in web.xml(??)
Spring interceptor
Filter vs interceptor, Filter is more powerful but limited to request and response, interceptor can be used ........
Interceptor can be created by implementing interface HandlerInterceptor or extending class HandlerInterceptorAdapter
Methods to implement , preHandle, postHandle , afterCompletion
Spring Security
Need to include two jars , spring-security-config, spring-security-web
Download sample project.
16. How to inject list?
17. how to develop rest web services in spring? 1. Generate spring MVC using STS. 2. Add the following dependency in pom.xml
jackson json dependency here
It will automatically convert the POJO to json object. 3. Change the annotation @Controller to @RestController.Sample Project Example using Spring4(STS) : Download
18. What are best practices in spring?
a) Naming convention: Use proper naming convention.
b) Avoid overuse of autowiring.
c) Logging: Use SLF4J. Avoid others. Avoid printing to console.
d) Unit Testing: Use spring unit testing support.
e) Don't put version number in xml schemas.
f) Prefer setter injection over constructor injection.
g) Always externalize values,constants.19. What is the difference between n-tier and MVC?
N-tier refers to the different layers in an architecture which are in a linear way such as the application layer depends on business layer which in turns depends on database layer. These are build on top of each other. MVC, on the other hand, is clear seperation of roles and there is traingular way of interaction between the three.20. How is transaction management done in spring? Types? Prefer?
Spring supports two types of transaction management −
Programmatic transaction management − This means that you have to manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain. Declarative transaction management − This means you separate transaction management from the business code. You only use annotations or XML-based configuration to manage the transactions.at a very high level, Spring creates proxies for classes that declare @Transactional on the class itself or on members. The proxy is mostly invisible at runtime. It provides a way for Spring to inject behaviors before, after, or around method calls into the object being proxied. Transaction management is just one example of the behaviors that can be hooked in. Security checks are another. And you can provide your own, too, for things like logging. So when you annotate a method with @Transactional, Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.Transactions in EJB work similarly, by the way.As you observed, through, the proxy mechanism only works when calls come in from some external object. When you make an internal call within the object, you're really making a call through the "this" reference, which bypasses the proxy. There are ways of working around that problem, however. I explain one approach in this forum post in which I use a BeanFactoryPostProcessor to inject an instance of the proxy into "self-referencing" classes at runtime. I save this reference to a member variable called "me". Then if I need to make internal calls that require a change in the transaction status of the thread, I direct the call through the proxy (e.g. "me.someMethod()".) The forum post explains in more detail. Note that the BeanFactoryPostProcessor code would be a little different now, as it was written back in the Spring 1.x timeframe. But hopefully it gives you an idea. I have an updated version that I could probably make available.Programmatic transaction management: By codeSpring AOP vs AspectJ:
Less powerful than AspectJ
Can call on method only, not fields
Can call on classes having interface
To call on classes without interface, use cglib dependency
You can call aop only on spring managed beans.<aop:aspectj-autoproxy /> is required to enable aop
Beans of aspect must be created
Bean of target object also must be created
package com.mkyong.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; @Aspect public class LoggingAspect { //Interface based @Before("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("logBefore() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("******"); } @Before("execution (public void pack.Dog.speak())") public void beforeDog(){ System.out.println("Dog getting angry.."); } //Getting arguments , via args and joinpoint @Before("execution (public void pack.Dog.listen(..)) && args(str,..))") public void beforeListen(JoinPoint joinPoint,String str){ System.out.println("Dog is going to listen this:"+str); System.out.println("Via JoinPoint:"+ joinPoint.getArgs()[0]); } @Before("execution (public void pack.Dog.run())") public void beforeDog(JoinPoint joinPoint){ System.out.println("Kind:"+joinPoint.getKind()); System.out.println("Target:"+joinPoint.getTarget()); System.out.println("This object:"+joinPoint.getThis()); } @After("execution (public void pack.Dog.speak())") public void afterDog(){ System.out.println("Dog cooling down"); } //This works but won't work correctly when method is returning a value @Around("execution (public void pack.Dog.speak())") public void aroundDog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("Before around"); proceedingJoinPoint.proceed(); System.out.println("After around"); } @Around("execution (public String pack.Dog.getBone())") public Object aroundDogBone(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("Before around dog bone"); Object obj = proceedingJoinPoint.proceed(); System.out.println("After around dog bone"); return obj; } @AfterReturning("execution (public void pack.Dog.speak())") public void afterReturningDog(){ System.out.println("after returning"); } @AfterThrowing("execution (public void pack.Dog.speak(..))") public void afterThrowingDog(){ System.out.println("after throwing"); } }Here:
Spring Transaction
Create datasource
Create sessionfactory using datasource , hibernate4sessionfactorylocalbean..
Create spring transaction manager
For transaction use hibernate getCurrentSession
Hibernatetemplate is no more used
Declarative and programmatic transaction, declarative is preferred
hibernate session flush is necessary sometimes
getCurrentSession will work only with transaction, session using this shouldn't be closed.
<tx:annotation-driven/> is required to enable annotation support for transaction
OR use @EnableTransactionManagement
Name in href for jsp should be without .jsp.
Spring Validation
Uses hibernate validation
@Valid to put on model object
Errors will come in BindingResult object.
Add @ModelAttribute and define a method
In jsp file , use spring form taglib and create a form, give path to each field, and give commandName same as @ModelAttribute and pass it in method, define form:error and cssClass to display the error.
Spring Exception Handling
@ExceptionHandler(Exception.class) annotation on a method in the controller file will enable the method to handle that type of exception
Define a class and makr it with @ControllerAdvice to make it global exception handler, inside it same as first point
Also, set response status with error code with message,
Also, define the error pages in web.xml(??)
Spring interceptor
Filter vs interceptor, Filter is more powerful but limited to request and response, interceptor can be used ........
Interceptor can be created by implementing interface HandlerInterceptor or extending class HandlerInterceptorAdapter
Methods to implement , preHandle, postHandle , afterCompletion
Spring Security
Need to include two jars , spring-security-config, spring-security-web
No comments:
Post a Comment