Fundhor
Fundhor

Reputation: 3587

Equivalent of @Bean without Spring

How to make the same thing without Spring ? I don't know how to declare the bean using specific constructor.

Usually I would do it with Spring this way, but right now I can't use Spring.

@Configuration
public class ClientConfig {
    @Bean
    public MyApi myApi() {
        return Feign.builder().target(MyApi.class, myUrl);
    }
}

And use injection in another class.

public class AnotherClass {
    @Inject
    MyApi myApi;
}

How to "declare" the bean? Today I'm using a singleton and it is 'ugly', so thanks in advance for any advice.

Upvotes: 2

Views: 2384

Answers (1)

bambula
bambula

Reputation: 415

You can use producer method which creates desired injectable bean. It's good practice to put producer methods in separated bean:

@Dependent
public class Resources {

    @Produces
    @Default  
    public MyApi myApi() {
        return Feign.builder().target(MyApi.class, myUrl);
    }
}

you can use @Inject MyApi myApi anywhere you need it then.

For more info see the CDI documentation http://weld.cdi-spec.org/.

Upvotes: 1

Related Questions