Spring Interview Questions With Answers
Spring Interview Questions With Answers
8. When to go for Setter based IOC and when to go for Constructor based IOC?
Ans) Use Constructor based IOC for mandatory dependencies and use Setter based IOC for
optional dependencies.
11. What is the difference between Inversion Of Control (IOC) and Dependency Injection (DI)?
Ans)
IOC: It is a design pattern which says Underlying environment is responsible to inject
dependencies.
DI : Implementation of IOC design pattern in spring which says spring container is responsible
to inject dependencies.
22. When prototype bean object is injected to singleton bean, then how many prototype objects are created by
container?
Ans) 1
23. When singleton bean object is injected to prototype bean, then how many single objects are created by
container?
Ans) 1
#applicationContext.xml
<beans>
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
…
</bean>
<bean id="bds" class="org.apache.commons.dbcp.BasicDataSource">
…
</bean>
</beans>
26. What are the differences among @Resource, @Autowired and @Inject?
@Autowired @Inject @Resource
Spring annotation JSR-330 annotation JSR-250 annotation
@Qualifier annotation is used @Named annotation is used The ‘name’ attribute is used to
to resolve bean ambiguities. to resolve bean ambiguities. resolve bean ambiguities.
Always ‘byType’ wiring Always ‘byType’ wiring. First ‘byName’ wiring if not
‘byType’ wiring.
27. What is Java Based Configuration in spring? Give some annotation example?
Ans) Java based configuration option enables us to write most of our Spring configuration
without XML but with the help of few Java-based annotations.
For example:
@Configuration: Indicates that the class can be used by the Spring IoC container as a source of
bean definitions.
@Bean: Annotation tells Spring that a method annotated with @Bean will return an object that
should be registered as a bean in the Spring application context.
41. How to enable (or activate) annotation based wiring in spring container?
Ans) Add <context:annotation-config/> in spring configuration file.
48. How can you configure a bean to get DataSource from JNDI?
Ans) Using JndiObjectFactoryBean class and its property ‘jndiName’. The value should be
java:/comp/evn/mypool
58. What are the differences between Programmatic and Declarative transactions?
Ans)
Programmatic Transactions Declarative Transactions
Business component contains both database Business component contains only
operations as well as transactional database operations without
statements. transactional statements.
Gives fine grained control since single Convenient to use because of AOP.
method may have multiple transactions.
59. Why most of the spring users choose declarative transaction management?
Ans) In case of declarative transactions:
a) The business comp contains only db operations without transactional statements
b) The Transactional statements are part of transaction aspect
c) The above transaction aspect is automatically provided by spring container
d) Our business comp and container provided transaction aspect is merged using AOP.
60. What are the Propagation Behaviors (transaction attributes) supported in spring?
Ans) Spring framework supports 7 transactional attributes: Required, RequiresNew, Supports,
NotSupported, Mandatory, Never, Nested
e) Proxy pattern
Separating business component and services.
Proxy = target + advice(s)
k) View helper
Spring has number of custom JSP tags and velocity macros to assist in separating code
from presentation in views.
72. How to load multiple spring configuration files in web layer?
Ans) The ContextLoaderListener is a servlet listener that loads additional configuration files into
spring application context along with application context created by DispatcherServlet.
<web-app>
<servlet>
<servlet-name>disp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>disp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
</web-app>
We should tell to ContextLoaderListener which spring configuration file(s) it should load,
otherwise the context loader will look for spring configuration file at /WEB-
INF/applicationContext.xml.