mercredi 20 mai 2015

Define bean using factory method in plain Java EE - WITHOUT Spring

I want to create a bean which can be automatically injected (autowired) by plain Java EE, not with Spring.

The code I have is this:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

@ApplicationScoped
public class MyConnector {
    ....
    private Client client = ClientBuilder.newClient();
    ....
}

I'd like to use dependency injection like that instead:

    @Inject
    private Client client;

In good old Spring I would just define the bean following the guideline http://ift.tt/1AeAMna

<bean id="client"
    class="javax.ws.rs.client.ClientBuilder"
    factory-method="createInstance"/>

and the @Autowired would inject the proper bean.

QUESTION: Can I achieve the same somehow in the plain Java EE without Spring? Can I define a bean in a similar way - and if so, where (in which configuration file)?

Aucun commentaire:

Enregistrer un commentaire