Thursday, September 29, 2011

Spring framework interview questions is in rise on J2EE and core Java interviews, As Spring is the best framework available for Java application development and now Spring IOC container and Spring MVC framework are used as de-facto framework for all new Java development. With this popularity interview questions from spring framework is top on any list of core Java Interview questions. I thought to put together some spring interview questions and answers which has appeared on many Java and J2EE interviews and useful for practicing before appearing on any Java Job interview. 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, Spring AOP etc, because of length of this post I haven't included Spring interview questions from Spring JDBC and JMS which is also a popular topic in core Java and J2EE interviews. I suggest to prepare those as well. Any way these Spring questions are not very difficult and based on fundamentals e.g. What is default scope of Spring bean and mostly asked during first round or telephonic round of Java interview. Although you can find answers of these Spring interview questions by doing Google but I have also included some answers for quick reference. As I said Spring and Spring MVC is fantastic Java framework and if you are not using it than start using it, these questions will give you some head start as well.

Top Spring Interview Questions and Answers

Now let’s start with questions, these Spring interview Questions are not very tricky or tough and based upon primary concepts of spring framework. If you are developing application using Spring framework than you may be, already familiar with many of these Java and Spring interview questions and answers. Nevertheless it’s a good recap before appearing to any Spring and Java interview.

Spring Security Interview questions AnswerSome of the reader requested to provide Spring Security interview questions and answer, So i though to update this article with few of Spring security question I came across. Here are those:How do you setup LDAP Authentication using Spring Security?This is a very popular Spring Security interview question as Spring provides out of the box support to connect Windows Active directory for LDAP authentication and with few configuration in Spring config file you can have this feature enable. See How to perform LDAP authentication in Java using Spring Security for detailed code explanation and sample.How do you control concurrent Active session using Spring Security?Another Spring interview question which is based upon Out of box feature provided by Spring framework. You can easily control How many active session a user can have with a Java application by using Spring Security. See this spring security example of how to control concurrent session in Java and Spring for exact details.

Question1: What is IOC or inversion of control?

Answer: This Spring interview question is first step towards Spring framework and many interviewer starts Spring interview from this question. As the name implies Inversion of control means now we have inverted the control of creating the object from our own using new operator to container or framework. Now it’s the responsibility of container to create object as required. We maintain one xml file where we configure our components, services, all the classes and their property. We just need to mention which service is needed by which component and container will create the object for us. This concept is known as dependency injection because all object dependency (resources) is injected into it by framework.

In this example CreateNewStockAccont class contain getter and setter for newBid and container will instantiate newBid and set the value automatically when it is used. This whole process is also called wiring in Spring and by using annotation it can be done automatically by Spring, refereed as auto-wiring of bean in Spring.

Question 2: Explain Bean-LifeCycle.

Ans: Spring framework is based on IOC so we call it as IOC container also So Spring beans reside inside the IOC container. Spring beans are nothing but Plain old java object (POJO).

2 using reflection container will create the object and if any property is defined inside the bean definition then it will also be set.

3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called before the properties for the Bean are set.

6. If an init() method is specified for the bean, it will be called.
7. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.

8. If the Bean definition in the Configuration file contains a 'destroy-method' attribute, then the corresponding method definition in the Bean class will be called.

Question 3: what is Bean Factory, have you used XMLBeanFactory?

Ans: BeanFactory is factory Pattern which is based on IOC design principles.it is used to make a clear separation between application configuration and dependency from actual code.

XmlBeanFactory is one of the implementation of bean Factory which we have used in our project.

org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instance defined in our xml file.

Question 4: What are the difference between BeanFactory and ApplicationContext in spring?

Answer : This one is very popular spring interview question and often asks in entry level interview. ApplicationContext is preferred way of using spring because of functionality provided by it and interviewer wanted to check whether you are familiar with it or not.

ApplicationContext.

BeanFactory

Here we can have more than one config files possible

In this only one config file or .xml file

Application contexts can publish events to beans that are registered as listeners

Doesn’t support.

Support internationalization (I18N) messages

It’s not

Support application life-cycle events, and validation.

Doesn’t support.

Support many enterprise services such JNDI access, EJB integration, remoting

Doesn’t support.

Question 5: What are different modules in spring?

Answer : spring have seven core modules

1.The Core container module

2.Application context module

3.AOP module (Aspect Oriented Programming)

4.JDBC abstraction and DAO module

5.O/R mapping integration module (Object/Relational)

6.Web module

7.MVC framework module

Question 6: What is difference between singleton and prototype bean?

Ans: This is another popular spring interview questions and an important concept to understand. Basically a bean has scopes which defines their existence on the application

Singleton: means single bean definition to a single object instance per Spring IOC container.Prototype: means a single bean definition to any number of object instances.

Whatever beans we defined in spring framework are singleton beans. There is an attribute in bean tag named ‘singleton’ if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default it is set to true. So, all the beans in spring framework are by default singleton beans.

Ans: This spring interview questions is little difficult as compared to previous questions just because transaction management is a complex concept and not every developer familiar with it. Transaction management is critical in any applications that will interact with the database. The application has to ensure that the data is consistent and the integrity of the data is maintained. Two type of transaction management is supported by spring

1. Programmatic transaction management

2. Declarative transaction management.

Question 8: What is AOP?

Answer : The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. AOP is a programming technique that allows developer to modularize crosscutting concerns, that cuts across the typical divisions of responsibility, such as logging and transaction management. Spring AOP, aspects are implemented using regular classes or regular classes annotated with the @Aspect annotation

Question 9: Explain Advice?

Answer: It’s an implementation of aspect; advice is inserted into an application at join points. Different types of advice include “around,” “before” and “after” advice

Question 10: What is joint Point and point cut?

Ans: This is not really a spring interview questions I would say an AOP one. Similar to Object oriented programming, AOP is another popular programming concept which complements OOPS. Join point is an opportunity within code for which we can apply an aspect. In Spring AOP, a join point always represents a method execution.

Pointcut: a predicate that matches join points. A point cut is something that defines at what join-points an advice should be applied

These spring interview Questions and answers are not very difficult and focused on spring fundamentals rather than focusing on advanced feature of session management, spring security, authentication etc. we will cover of those question on some other interview article. I would also suggest that share some spring questions asked to you guys during interview and than I can put together those with there answers for quick reference of everybody.

@Anonymous, Thanks for your comments, In My opinion Spring is better than J2EE , though this is a premature comment because I have not used J2EE quite except Jsp , Servlet and EJB a lot but with that limited experience I found Spring more better.

Can you add some more Spring framework interview questions , especially from spring mvc part. I am going for spring interview and expecting spring mvc interview questions based on job description. Also Can you please upload pdf version of spring interview questions so that I can take a print out.

I think Spring is awesome framework and very modular. One of best frameworks around in Open Source Projects. I am just overwhelmed with features and abilities provided by these framework. I don't this it needs anyone approval. Most of bigger firms use it in bigger projects for scalability. Love Live Spring Framework.....

can you also put some spring mvc questions, like questions on controller, dispatch-servlet.xml and view mappings on spring framework. spring-mvc is more asked than any other module of spring framework so please share some spring mvc interview questions.

1) What is Spring IOC Container ?2) Does Singleton from Spring Container is thread safe?3) Why Spring MVC is better than Struts?4) What is Spring Integration?5) How to call remote method by RMI using Spring6) What scheduling feature Spring framework provides?7) How do you make a Singleton bean to lazy load in ApplicationContext which loads all Singleton beans eagerly during startup?8) Does Spring Security part of Spring framework?9) How to configure Spring using Annotation10) Which version of Spring have you used recently and what is difference you observed from previous spring version.

Answer of "What is Spring IOC Container"Spring IOC container is an implementation of Dependency Injection design pattern provided by Spring framework. Instead of creating objects in application code using new() keyword, we use ApplicationContext.getBean("nameOfBean") to get reference of Objects. Spring framework creates objects, manages object and set the dependency automatically so that you can get Object in proper state.

Answer 2 - "Does Spring Singleton are thread-safe"I am not sure on this , I guess this may depends upon whether object is immutable or not.

Hi, I am an experienced Java programmer (4 years experience in Java and J2EE technology) but I am new to Spring framework. I am going to appear for client interview this week and looking for some Spring questions for 2 to 4 years experienced programmer. Would preparing above Spring question would be sufficient? if possible can you please share some tough, difficult or triky Spring interview question as most of them are quite command and frequently asked. They are also looking for Some spring MVC , Spring JDBC and Spring Security experience. Would be great if you could share few Spring MVC, JDBC and Spring Security interview question and answers as well. Thanks you

Please share some questions answers for Spring hibernate integration and Spring AOP support. Also if you could include questions like What is new in Spring 3.0, Which newe features are introduced in Spring 3.0 etc, That would be fantastic.

Few of Spring JDBC interview questions, which I am aware of:What is benefit of using JdbcTemplate ? Why should you use JdbcTemplate in Spring ?How do you handle SQLException in Spring?How to you setup JDBC connection pool in Spring etc.interview questions from JDBC in Spring is not that many, but you should be familiar with JdbcTemplate.

Hi Sunil, IOC does lot of stuff for you. It creates dependency and construct objects. If you don't need explicit instance of bean, you can avoid that getBean() call, but you almost always need that. It's better to wrap casting and call to getBean in a separate class itself and use Generics , instead of casting everywhere in code.

question about prototype was asked to me in ML interview. The tougher question is why we need a prototype scope ?the asnwer is if you want one instance of the bean per thread or session i.e. if you need a stateful bean

SirI have .war file in java eclipse,but how can i upload it on server please tell me because when i m asking to the domain giver sites they recommend me to convert it into either asp.net or any other html files.Will you please give me result???

Hi,Dear sirI have a query,I have coded a web site in jee,html,js in eclipse and i got a .war file in it and i want to upload it on server as the same,I have asked to many domain giver how to upload it in .war file but they recommended me to use it either in asp.net or in completrly html form Please Tell me how can i do it???

Arpan....you are so confusing and your english is funndy. Some needs pdf's some needs help ....guys do your homework and work hard ..there is no free lunch. Don't be so stupids!!...you are a shame to IT

Hello dude, these Spring framework questions are good for freshers and beginners, but not good enough for experienced deveopers. Can you add some more advanced questions for senior programmers, who knows Spring basic and has over 2 to 4 yeras experience in developing Spring MVC framework?

I am nikita. Currently working in bpo company. Having 1 year experience.I want my career in java.have completed 1 project in java.Can I switch into java bcz I am from computer background.In java wants 3-4 year experience. And now I can't apply fresher also.What should I do?Anyone plz suggest me..

I have one questionI am currently working in bpo company.But I want career in java.If I complete Project in spring .then is it possible to get hire in IT company? Without having any experience in java?

Hello Nikita, If you have any engineering degree or courses on computer science e.g. M.Sc or MCA, it's quite easy to get Job on Java Software development. I also suggest to do Oracle Java Certification, a good score there will boost your chances of Java job for sure. Coming to Spring, it's not mandatory for Java, but having this experience is always good. Good luck.

I presume, BCS is a bachelors degree in computer science, so that's good. If you don't have any experience than I suggest to do certification and at same time try to work as intern or trainee to gain some experience. Having said that, no harm in trying and giving Job interviews. There are times when company needs developer in large numbers and they are less restrictive in filtering. Having worked in Spring will also help you.

Hi,Thanks for such an useful topic.In one interview I face the question that is if we declare bean with same class but different ID's then will spring container create more object or it creates single object as we are creating these beans with singletone scope.Is there any difference in singletone design pattern and singletone bean scope?