My application in main method reads parameters and runs Spring application. My Configuration with DataSource bean inside needs to read one of the parameter to use in initialization of datasource. Application class Configuration class So I need my DataSource getDataSource() to properly read that credentialsPropertyPrefix argument and use in datasource. Answer Try
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 use dependency injection to mock out file system access?
I have a class that creates a new File object: Now I want to mock out the dependency to File. So I created an interface IFile: For the actual code I created a FileWrapper class that implements this interface and calls java.io.File: My initial idea was to use IFile in the constructor of MyClass (and create a mock implementation for
Spring Boot – Injecting application properties into a Util class variable
I would like to ask a question as per title. I have a util class such as follows: When I launch the application, and debug, the above variable SOME_VAR comes as null 🙁 I am aware that I am using this in a Util class, which is an antipattern, I think. Could someone help me understand what I need to
Should we use Spring @Bean with static method?
Is this a good practice to use @Bean with static method? Answer Generally speaking, there’s no need for @Bean methods to be static. When a @Bean method is not static, creating the bean requires an instance of its class, FooFactory in your example, to be created first. The vast majority of the time this is fine, but it can sometimes
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