Monday, April 9, 2018

Spring interview questions

Q) What are bean scopes?

Q) Lifecycle of a bean?
http://javainsimpleway.com/spring-bean-life-cycle/

Q) Why is @component used?

Q) How to create a web request in spring MVC?

Q) When to use @service and @component annotations?

Q) Transaction management in Spring?

Q) What are different types of handler mappings and view resolvers in spring?

Q) How to create a synchronized bean in spring?
Ans: https://tarunsapra.wordpress.com/2011/08/21/spring-singleton-request-session-beans-and-thread-safety/
https://javabeat.net/spring-singleton-java-singleton/

Q) What is dependency injection?
Ans Dependency injection is a design pattern and IOP is the way spring achieves it

Q) How to handle internationalization and localization in spring MVC?
Ans - Steps
1) Include two links-English and French on top of jsp page
2) Register an interceptor with the name localeChangeInterceptor  in the spring cofiguration file
3) Include an entry of cookieLocaleResolver bean in the spring's configuration file

Q) Difference between bean id and bean name and their uses?
Ans - Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the container the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases.
When using XML-based configuration metadata, you use the 'id' or 'name' attributes to specify the bean identifier(s). The 'id' attribute allows you to specify exactly one id, and as it is a real XML element ID attribute, the XML parser is able to do some extra validation when other elements reference the id; as such, it is the preferred way to specify a bean id. However, the XML specification does limit the characters which are legal in XML IDs. This is usually not a constraint, but if you have a need to use one of these special XML characters, or want to introduce other aliases to the bean, you may also or instead specify one or more bean ids, separated by a comma (,), semicolon (;), or whitespace in the 'name' attribute.

Q) What are the new features in spring 4.0 and spring 4.1
Ans -
  •  spring-webmodule provides support over websocket based communication in web applications
  • spring 4.0 is based on servlet 3.0+ implementations
  • @RestController annotation is introduced to use with Spring applications
  • Spring 4.1 introduces @JMSListener annotations to easily register JMS listener endpoints
  • Spring 4.1 supports Jcache(JSR- 107) annotations using spring's existing cache configuration
  • Jackson's @Jsonviewisdirectlysupportedon @ResponseBodyandResponseEntitycontroller methods
Q) Difference between singleton pattern and spring singleton bean
Ans. Singleton pattern supports singleton for one object per JVM whereas spring singleton scope is one bean per application context.

Q) How is validation done using spring framework
Ans Using Validator interface

Q) Steps to create a spring mvc application

1) create a maven project with archetype as maven-archetype-webapp
2) Modify pom.xml and include spring dependencies
3) Modify web.xml to specify dispatcher servlet (Front Controller) class
4) create spring-dispatcher-servlet.xml (front controller) and specify handler mapping, controller class and view resolver settings.(Can use annotations for handler mapping and controller)
5) Create controller class

Q) Types of controllers in spring MVC?






Useful links:
http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation
https://www.youtube.com/watch?v=8V4ArtwNuwk&list=PLBgMUB7xGcO31B2gBmy1igpZn6LK78-CJ&index=12
https://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-requestparam


No comments: