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 …
Tag: spring
Can’t start Spring Boot because of the class mapping
I was trying to start an Spring Boot application, with a simple class JPA mapping and H2 database, but every time I try to start the application, I have the same class error at the same column. Class with error: Error logged: The column that is with error is a table and already mapped: My H2 configurations: I…
How to pass an a single parameter in JSON body and why am I having an exception doing that?
Here is the code I have: Here is the JSON body I pass to this method Here is the exception: If I change value to String like this: I have this error: The question is how can I pass a single parameter in JSON body and why do I have these exceptions? I do appreciate your answers a lot! Answer
Communicate between microservices on the same machine without exposing a public API
I am relatively new to Camel and Spring, and I am making a service to predict stock prices using a neural network to practise using Camel, Spring and also DL4J. My service is divided into 5 microservices (Gateway, H2 SQL Database, Admin Console, Data Fetcher, DL4J Handler) which will each run in their own Jav…
How to handle an error when database returns null value for one of the columns in the table
I’m using Java and Spring Boot with JPQL to access the data. I’m getting the below error because hourlyRate is null in the database: Caused by: org.hibernate.QueryException: could not instantiate class [com.example.model.response.school.Employee] from tuple. I still would like to be able to get th…
Send data object using key
I want to create something like quick switch to send Java object based on Key. I tried this: Full code example: https://github.com/rcbandit111/Code_Switch_Select_POC/blob/main/src/main/java/org/poc/PocController.java Is it possible to use @Service(“first”) instead of @Component(“first”…
direct logging on elasticsearch vs using logstash and filebeat
I’m using a Spring Boot back-end to provide some restful API and need to log all of my request-response logs into ElasticSearch. Which of the following two methods has better performance? Using Spring Boot ResponseBodyAdvice to log every request and response that is sent to the client directly to Elasti…
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. H…
Using Spring Framework Scheduler in a non-bean class
I have Spring used in a legacy project and would like to use “Scheduled” runs in a class that is not created as a bean, but as a usual “new” class. So annotations such as @Scheduled are not active. How can I then enable scheduling by calling all relevant Spring methods explicitly? Answ…
Error Creating Bean Error (Spring Cloud Config Client)
I am trying to learn Spring Cloud Config. So first I setup a Server, where I can fetch the properties using http://localhost:9090/config/default/master/app.static.properties on the browser. It has about 5 or 6 properties. I am trying to get just one for now. I wrote my classes like: and My bootstrap.propertie…