Spring Puzzlers #02: The Happy Traveler

I really love traveling, and even more when I don’t know what my next destination will be. That’s one of my biggest passions, I have to admit it. If you also love traveling, you should consider using my TravelService for your next trip. Let me introduce you the second Spring Puzzler:

The TravelService will be our entry point. It has a method to print a destination picked by a DestinationPicker (shown below). Notice that the method is annotated with @Async to run it asynchronously in another thread:

public class TravelServiceImpl implements TravelService {
	private Logger logger = Logger.getLogger(TravelServiceImpl.class);
	private DestinationPicker destinationPicker;
	public void printNextDestination() {
		logger.info("Running from thread: " + Thread.currentThread().getId());
		System.out.println("Your next holiday destination: " + 

	public void setDestinationPicker(DestinationPicker destinationPicker) {
		this.destinationPicker = destinationPicker;

This is the RandomDestinationPicker, which uses a really complex algorithm to choose the destination from a list of available destinations :). It uses the id field (increased in every object creation) to determine the location:

public class RandomDestinationPicker implements DestinationPicker {
	private static int instances = 0;
	private int id;
	private List<String> availableDestinations;	
	public RandomDestinationPicker(List<String> availableDestinations) {
		this.availableDestinations = availableDestinations;
		id = instances++;
	public String getDestination() {
		return availableDestinations.get(id % availableDestinations.size());

The next XML file has the configuration for our Application Context:
– It defines a list of destinations (a list of strings)
– It defines the DestinationPicker injecting the list of available destination and setting the scope of the bean to ‘puzzle’.
– It defines the TravelService injecting the DestinationPicker.
– It enables the @Async annotation (task:annotation-driven)
– It defines a new scope called ‘puzzle’ which is a thread scope

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<bean id="travelService" class="com.sergialmar.springpuzzlers.sp02.TravelServiceImpl" 

	<bean id="destinationPicker" class="com.sergialmar.springpuzzlers.sp02.RandomDestinationPicker" 
		<constructor-arg ref="destinationList"/>
	<util:list id="destinationList">
		<value>San Francisco</value>
		<value>Kuala Lumpur</value>
		<value>Mexico City</value>
		<value>Cape Town</value>

	<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
      <property name="scopes">
              <entry key="puzzle">
                  <bean class="org.springframework.context.support.SimpleThreadScope"/>

The bootstrap class creates the application context and gets the TravelService bean, calling the printNextDestination method 4 times.

public class TravelBootstrap {

	public static void main(String... args) {
		ApplicationContext applicationContext = 
			new ClassPathXmlApplicationContext("com/sergialmar/springpuzzlers/sp02/app-context.xml");
		TravelService travelService = 

Time to vote! Which of the following statements is right:


Un pensamiento en “Spring Puzzlers #02: The Happy Traveler

  1. Sincerely, I have no idea.

    But I’ve had a lot of fun trying to discover the solution.

    At the end, I’ve just copy-paste the code in order to see it running.

    Before doing that, I read the javadoc for the Spring classes involved, and I could discard some answers. While I was copying the code, I saw something weird that gave me a clue for the right answer.

    But honestly, I didn’t know the solution until I saw the code running.

    Thanks a lot for giving some fun to us, as well as a good source for learning.


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s