Answer:
This one is a very popular Spring interview question and often asks in an entry-level interview. ApplicationContext is the preferred way of using spring because of functionality provided by it and the 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. | Don't support. |
Support internationalization (I18N) messages. | It’s not. |
Support application life-cycle events, and validation. | Doesn’t support. |
Supports many enterprise services such as JNDI access, EJB integration, remoting | Doesn’t support. |
Answer:
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. The XmlBeanFactory is one of the implementations of Bean Factory which we have used in our project.
The org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instance defined in our XML file.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
Or
ClassPathResource resorce = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resorce);
Answer:
Setter injection is more flexible than constructor injection because you must remember the type and order of constructor parameter. Also, constructor injection is generally used to inject the mandatory dependency, while setter can be used to inject optional dependency.