The largest Interview Solution Library on the web


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

81.How do you configure parallel execution of steps with Spring Batch?

• To execute jobs in parallel we can use a split. Example shows configuring a split.
• In below example, flow1 (step1 and step2 sequence) is executed in parallel with flow2 (step3).
• Step4 is executed after both flows are complete.













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");
}
}
}

82.How do you start running a Spring Batch Job?

• A Job Launcher can be used to execute a Spring Batch Job.
• A job can be launched/scheduled in a web container as well.
• Each execution of a job is called a Job Instance.
• Each Job Instance is provided with an execution id which can be used to restart the job (if needed).
• Job can be configured with parameters which can be passed to it from the Job Launcher.

83.What are the different ItemReader and ItemWriter implementations available with Spring Batch?

Important ones are those that allow to read/write from
• Flat File
• Hibernate Cursor
• JDBC
• JMS
• Hibernate Paging
• Stored Procedure

84.What are the important concepts related to setting up a Job in Spring Batch?

A Job in Spring Batch is a sequence of Steps. Each Step can be configured with
• next : next step to execute
• tasklet : task or chunk to execute.A chunk can be configured with a Item Reader, Item Processor and Item Writer.
• decision : Decide which steps need to executed.

85.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.

86.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"/>

87.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

88.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.

89.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).

90.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 {

91.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 behaviour before and after the method invocation.

92.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.

93.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.

94.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");
}
}
}

95.How do you use values defined in a property file in an application context xml?

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










96.How do you represent Stateful bean in Spring?

• Stateful beans are represented in Spring with a prototype scope.
• A new instance is created every time a request for bean is made. Example below.

97.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.

98.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.

99.Which ViewResolver class is widely used?

The org.springframework.web.servlet.view.InternalResourceViewResolver class is widely used.

100.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 @JsonViewissupporteddirectly on @ResponseBody and ResponseEntity controller methods for serializing different amounts of detail for the same POJO

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


copyright © 2014 - all rights riserved by javatechnologycenter.com