Skip to content
Advertisement

Tag: dependency-injection

Micronaut – Create a bean without creating ApplicationContext

I have a Micronaut declarative HTTP client written using @client annotation. I want to call this while starting micronaut app before creating the ApplicationContext itslef. HttpClient : SampleHttpClient.java Application.java (Main class) Answer I want to call this while starting micronaut app before creating the ApplicationContext itslef. Micronaut doesn’t provide a mechanism to support that. You could write your own thing

How to inject ObjectMapper in spring

I have a batch job written using Spring Batch I have a config file below: I have Json Line aggregator as below: I want to inject objectMapper and don’t want to create it inside the JsonLineAggregator class. Any idea how can I implement it using DI ? Answer You can use @Autowired annotation to inject the dependency in spring like

Spring: re-initialized a bean

This is my Spring bean in the configuration class that creates a gRPC ManagedChannel: The controller method is provided below: The service class is: For each request, I create a new ManagedChannel in the service method processRequest and shut it down using the method called shutdownManagedChannel. Earlier, I try to use the @Autowired for the managed channel as below: This

How to test a Java/Spring service using RestTemplate without calling external API and by using Dependency Injection?

In my Spring application, i have a service MyService. MyService calls an external API, counts the products there and returns the result. To call that API it uses the Spring module RestTemplate. To inject the RestTemplate it is configured as @Bean in the DependencyConfig: Now i want to test it, without calling the external API. So i inject the MyService

Two implementations of one interface in Jersey/HK2, reuse first in other

I have an interface with a naive implementation, say I want to make a cache that caches the doIt, so I wrote a wrapper : Then, I add both implementations to my Binder: I get errors complaining about: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MyService,parent=CachingMyService},position=-1,optional=false,self=false,unqualified=null,1102650897) I trief using Qualitifaction like this: And using that: But that does

How to inject a private field in a Component class while keep initiating with @Autowired in parent class in Spring?

I am learning Spring while I like the idea of using @Component and @Autowired to let Spring manage the dependent bean. For example, I have a Service class and Builder Class I can do with Spring would take care of the creation of from SomeController to SomeService to SomeBuilder with the usage of @Autowired. However, now my SomeService class needs

Advertisement