Skip to content

Alternative to applicationcontext getBean spring

  • by
alternative to applicationcontext getbean spring

1. Overview

In this article, we will learn the alternative solutions to applicationcontext getBean spring.

2. ApplicationContext getBean spring

Let’s see an example where we would use ApplicationContext to get bean in Spring.

Consider the following interface Vehicle.

public interface Vehicle
{
   String getNumber();
}

Both the Car and Bike classes implement Vehicle interface.

/* Car class */
@Service
public class Car implements Vehicle
{
    @Override
    public String getType() {
        return "car_vehicle";
    }
}

/* Bike class */
@Service
public class Bike implements Vehicle
{

    @Override
    public String getType() {
        return "bike_vehicle";
    }

}

The following VehicleController handles the HTTP GET requests /getVehicle from the client.

Based on the path variable type, we had to either use the bean Car or Bike. Here, we are using the application context to get the bean using its name.

@RestController("/")
public class VehicleController {

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/getVehicle/{type}")
    public String getVehicle(@PathVariable("type")
                                   String type) {

        Vehicle vehicle = applicationContext.getBean(type, Vehicle.class);
        return vehicle.getType();
    }
}

If you hit the URL http://localhost:8080/getVehicle/bike, the getVehicle method returns the value bike_vehicle correctly.

3. Alternative to applicationcontext getBean spring

Now, let’s see the alternatives to get the bean programmatically without using the application context.

3.1. Use BeanFactory

The BeanFactory is the root interface for accessing the Spring bean container and contains utility methods to get specific beans. It is a central registry of application components and centralizes configuration of them.

Since BeanFactory is a Spring bean, you can autowire and access directly in your class:

@Autowired
private BeanFactory beanFactory;

You can use the getBean method of the BeanFactory to get the required bean implementation rather than using the getBean method of the application context.

@RestController("/")
public class VehicleController {

    @Autowired
    private BeanFactory beanFactory;


    @GetMapping("/getVehicle/{type}")
    public String getVehicle(@PathVariable("type")
                                   String type) {

        Vehicle vehicle = beanFactory.getBean(type, Vehicle.class);
        return vehicle.getType();
    }
}

If you re-run and hit the URL http://localhost:8080/getVehicle/bike, the getVehicle method returns the value bike_vehicle as expected.

3.2. Autowire all beans into a map

Since Spring 4.3, you can collect all the bean implementations of a particular type into a Map comprising the bean name as key and the bean instance as value. You can later at runtime determine which bean implementation to use.

For example, the Spring collects and provides all the beans (Car and Bike) of the Vehicle interface in the vehicleMap.

@Autowired
private Map<String, Vehicle> vehicleMap;

At runtime, we will use the bean name passed as the path variable in the URL to get the specific bean instance from the Map.

@RestController("/")
public class VehicleController {

    @Autowired
    private Map<String, Vehicle> vehicleList;


    @GetMapping("/getVehicle/{type}")
    public String getVehicle(@PathVariable("type")
                                   String type) {

        Vehicle vehicle = vehicleList.get(type);
        return vehicle.getNumber();
    }
}

4. Conclusion

To sum up, we have learned about the alternative to applicationcontext getBean spring.

Leave a Reply

Your email address will not be published. Required fields are marked *