1

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.

Fundhor
  • 3,369
  • 1
  • 25
  • 47

1 Answers1

1

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/.

bambula
  • 365
  • 1
  • 12