The largest Interview Solution Library on the web


Interview Questions
« Previous | 0 | 1 | 2 | 3 | 4 | Next »

61.Can you give an overview of a web application that is implemented using Spring related Modules?

Different Layers of a web application can be implemented using different spring modules.
Beauty of Spring framework is that Spring provides great integration support with other non Spring open source frameworks –
In case you would want to integrate with frameworks like AspectJ (instead of Spring AOP) or Struts 2(instead of Spring MVC).
Service & Business Layers

Core Business Logic using simple POJOs, managed by Spring's IoC container
Transaction Management using Spring AOP

Integration Layer

Spring ORM to integrate with Databases (JPA & iBatis).
Spring JMS to intergrate with external interfaces using JMS
Spring WS to consume web services.

Web Layer
Spring MVC to implement MVC pattern.
Spring WS to expose web services.

62.What is the simplest way of ensuring that we are using single version of all Spring related dependencies?

Spring is made up of a number of small components (spring-core, spring-context, spring-aop, spring-beans and so on).
One of the quirks of using Spring framework is the dependency management of these components.
Simple way of doing this is using Maven "Bill Of Materials" Dependency.



org.springframework
spring-framework-bom
4.1.6.RELEASE
pom
import



All spring dependencies can now be included in this and the child poms without using version.


org.springframework
spring-context


org.springframework
spring-web


63.What are the major features in different versions of Spring ?

Spring 2.5 made annotation-driven configuration possible.
Spring 3.0 made great use of the Java 5 improvements in language.
Spring 4.0 is the first version to fully support Java 8 features.
Minimum version of Java to use Spring 4 is Java SE 6.

64.What are the latest specifications supported by Spring 4.0?

Spring Framework 4.0 supports the Java EE 7 specifications

JMS 2.0
JTA 1.2
JPA 2.1
Bean Validation 1.1
JSR-236 for Concurrency.

65.Can you describe some of the new features in Spring 4.0?

Following are some of the new features in Spring 4.0 and Spring 4.1

spring-websocket module provides support for WebSocket-based communication in web applications.
Spring Framework 4.0 is focused on Servlet 3.0+ environments
@RestController annotation is introduced for use with Spring MVC 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@JsonViewissupporteddirectlyon@ResponseBodyandResponseEntitycontroller methods for serializing different amounts of detail for the same POJO

66.What is auto-wiring?

The Spring container can autowire dependencies into interacting beans.
Spring container can resolve dependencies by looking at the other beans defined in the ApplicationContext.
This elimates the need for maintaining an xml specifying beans and their dependencies.
Autowiring can be done byName (name of the property) and byType (type of the property).

67.What would happen in Spring Container finds multiple bean definitions matching the property to be auto wired?

Spring framework does not resolve this kind of ambiguity.
It would throw and exception.
The programmer has the choice to remove one of the bean or explicitly wire in a dependency.

68.How is Spring’s Singleton bean different from Gang of Four Singleton Pattern?

The singleton scope is the default scope in Spring.
The Gang of Four defines Singleton as having one and only one instance per ClassLoader.
However, Spring singleton is defined as one instance of bean definition per container.

69.How do you represent Stateful bean in Spring?

Following example shows how to configure a data source using the url, username and password configured in a property file (database-connection.properties)









70.How is validation done using Spring Framework?

Spring validator can be used both in web and business layers to validate objects.
It is based on the org.springframework.validation.Validator interface having two important methods

supports(Class) – does this validator support a particular class
validate(Object, org.springframework.validation.Errors) – validates and sets errors into Errors object
An example is provided below: MessageCodesResolver can be used to convert the message code into proper internationalized text.
public class CarValidator implements Validator {
public boolean supports(Class clazz) {
return Car.class.equals(clazz);
}
public void validate(Object obj, Errors e) {
ValidationUtils.rejectIfEmpty(e, "name", "name.is.empty");
Car c = (Car) obj;
if (c.getUsedYears() < 0) {
e.rejectValue("usedYears", "not.yet.bought");
}
}
}

71.How do you implement cross cutting concerns in a web application?

Functionality spanning multiple layers of an application are called cross cutting concerns.
Examples are logging, security, declarative transactions.
Cross cutting concerns are best implemented using Aspect Oriented Programming (AOP).
AOP enables us to apply the cross cutting features across multiple classes.

72.What is an Aspect and Pointcut in AOP?

Two important AOP concepts are Aspect : Aspect is the concern that we are trying to implement generically.
Ex: logging, transaction management. Advice is the specific aspect of the concern we are implementing.
Pointcut : An expression which determines what are the methods that the Advice should be applied on.

73.What are the different types of AOP advices?

Before advice : Executed before executing the actual method.
After returning advice: Executed after executing the actual method.
After throwing advice : Executed if the actual method call throws an exception.
After (finally) advice : Executed in all scenarios (exception or not) after actual method call.
Around advice : Most powerful advice which can perform custom behavior before and after the method invocation.

74.How do you define transaction management for Spring – Hibernate integration?

First step is to define a a transaction manager in the xml.
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />

Next, we can add the @Transactional annotation on the methods which need to part of a transactions.
@Transactional(readOnly = true)
public class CustomerDaoImpl implements CustomerDao {

75.How do you choose the framework to implement AOP - Spring AOP or AspectJ?

Spring AOP is very simple implementation of AOP concepts.
Its an ideal fit If the needs of an application are simple like

Simple method executions and/or
Advising the execution of operations only on spring beans
AspectJ is a full fledged AOP framework that helps you
Advise objects not managed by the Spring container .
Advise join points other than simple method executions (for example, field get or set join points).

76.What are the different mock objects provided by Spring test framework?

org.springframework.mock.env package : Mock implementations of the Environment and PropertySource abstractions.
org.springframework.mock.jndi package : Implementation of the JNDI SPI, which can be used to create a simple JNDI environment for test suites.
org.springframework.mock.web package : Servlet API mock objects to test Spring MVC classes like controllers.

77.What are the utility methods available to test JDBC classes?

JdbcTestUtils provides the following static utility methods.
countRowsInTable(..) & deleteFromTables(..): counts/deletes the rows in the given table
countRowsInTableWhere(..) & deleteFromTableWhere(..): counts /deletes the rows in the given table, using the provided WHERE clause
dropTables(..): drops the specified tables

78.How do you setup a Session Factory to integrate Spring and Hibernate?

Hibernate SessionFactory can be configured as a spring bean.




tablename.hbm.xml




hibernate.dialect=org.hibernate.dialect.HSQLDialect



Datasource can be created from JNDI.



79.How do you implement caching with Spring framework?

Enabling caching in Spring is all the matter of making the right annotations available in appropriate methods.
First, we need to enable caching. This can be done using Annotation or XML based configuration.
Below example shows enabling Spring based caching using Annotation.

@Configuration
@EnableCaching
public class AppConfig {
}
Next step is to add the annotations on the method we would like to cache results from.
@Cacheable("players")
public Player findPlayer(int playerId) {
...
}
Spring offers features to control caching

Conditional Caching : condition="#name.length < 32"
@CachePut : Update existing cache. For example, when player details cached above are updated.
@CacheEvict : Remove from cache. For example, the player is deleted.
Spring also provides integration with well known caching frameworks like EhCache. Sample xml configuration shown below.

class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>

location="ehcache.xml"/>

80.What are the important features of Spring Batch?

Restartability : Easy to restart a batch program from where it failed
Different Readers and Writers : Provides great support to read from JMS, JDBC, Hibernate, iBatis etc.
It can write to JMS, JDBC, Hibernate and more.
Chunk Processing : If we have 1 Million records to process, these can be processed in configurable chunks (1000 at a time or 10000 at a time).
Easy to implement proper transaction management even when using chunk processing.
Easy to implement parallel processing. With simple configuration, different steps can be run in parallel.

« Previous | 3 | | 5 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com