Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Spring equivalent for CDI's Instance, or Guices Provider

Tags:

In CDI you can define an object that will give you items of a certain type, using:

@Inject
Instance<MyObject> myObjectInstance;
//...
MyObject myObjectInstance.get();

Similarly in Guice you can do:

@Inject
Provider<MyObject> myObjectInstance;
//...
MyObject myObjectInstance.get();

I am wondering if there is a similar construct in Spring, or you must use the ApplicationContext in order to get the reference?

like image 969
bogdan.mustiata Avatar asked May 10 '12 16:05

bogdan.mustiata


People also ask

Does Guice work with Spring?

Guice uses binding as the equivalent to wiring in Spring. Simply put, bindings allow us to define how dependencies are going to be injected into a class. Guice bindings are declared in our module's configure() method. Instead of @Autowired, Guice uses the @Inject annotation to inject the dependencies.

Which is better Guice or Spring?

Spring allows you to omit the @Autowired annotation when there's only one constructor. Guice allows binding to a Provider, as well as injecting a Provider of your class, even when your class has no Provider binding.

What is CDI in Spring boot?

1. Overview. CDI (Contexts and Dependency Injection) is a standard dependency injection framework included in Java EE 6 and higher. It allows us to manage the lifecycle of stateful components via domain-specific lifecycle contexts and inject components (services) into client objects in a type-safe way.

Which API is used for dependency injection in Spring?

Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods.


1 Answers

So after a lot of digging around I found out that Spring supports JSR-330. This JSR defines a simple API - the whole spec is literally just this API - that standardizes several dependency injection interfaces, annotations and behaviors.

Unlike Spring's FactoryBean the javax.inject.Provider interface doesn't throws Exception on getting the bean reference. Furthermore, you would still need to define this FactoryBean in some place (read XML, or @Configuration class, and this is suboptimal).

Due to a bug, in current Spring 3.1.1, the javax.inject.Provider does not work. It does work in Spring 3.1.0.

In order to use it you simple need to include the javax.inject jar - if you use maven you can:

    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

Spring will detect it, and from that moment on you can simply:

@Inject
Provider<MyObject> myObjectInstance;
//...
MyObject myObjectInstance.get();

like in the Guice example, since it is the same API.

Despite my previous comment to Konstantin, Spring does create the Provider by itself. (I was testing it against Spring 3.1.1 and run into this Spring Provider regression issue)

like image 79
bogdan.mustiata Avatar answered Sep 29 '22 10:09

bogdan.mustiata