I am sorry for asking a stupid question and you may delete it later if you find it too stupid, but I want to know why dependency injection in Spring Boot is not working inside main
method? This is my first spring boot project at my job as entry level junior programmer so that is probably the reason why this question is so stupid. Anyway, I have been tasked with developing a data access repository for a table in a database by using spring boot and jpa.
So I created jpa class (E), which, for simplicity i shortened and let us say my jpa class is as follows:
package P;
@javax.persistence.Entity()
public class E
{
@javax.persistence.Id()
private int I;
}
So, nothing special, as basic jpa entity as possible. Next, I defined repository for this entity (R) and simplified it as follows:
package P;
public interface R extends org.springframework.data.repository.PagingAndSortingRepository<P.E, Integer>{}
Now the good part: Spring boot documentation says that the repository implementation can be autogenerated by using dependency injection. So I tried to test this with the following class (A), which I shortened here for simplicity:
package P;
@org.springframework.boot.autoconfigure.SpringBootApplication()
public class A
{
@org.springframework.beans.factory.annotation.Autowired()
static P.R F;
public static void main(java.lang.String[] U)
{
org.springframework.boot.SpringApplication.run(P.A.class, "");
System.out.println(P.A.F);
}
}
So, the class A
is the main class which starts spring boot application. What I would like to do here is to manage my database inside the main
method. I wanted to use the dependency injection for my repository interface R
, so I declared static field (F) in A
class and annotated it with Autowired
in order to trigger the injection. In ideal case, what I would expect to happen after I ran Spring Boot in my main
method (by calling run()
) is that F
field of my main class would be injected with the repository class R
so that I could continue working with data base in my main method. However it is not. println
method prints null
.
Now, my question is not "what did I do wrong", I know that. I found on a lot of places on Internet that I can not use a dependency injection directly in the main
method, but that I instead need to make a Bean. So, for example, I could add the following method (M) in my main class:
@org.springframework.context.annotation.Bean
public boolean M(P.R R)
{
System.out.println(R);
return true;
}
and this would work just fine, it would inject the repository after I annotate it with Bean
in order to trigger injection. But this is called from inside the run()
method in my main method, so, if I use this approach, I can not do anygthing inside my main
method. Whole job which my application does needs to be done inside one or more Beans. At least job which requires the dependency injection. I can do other stuff in the main
method before and after I run the Spring Boot, but not anything for what I need dependency injection.
So, my qestion here is why spring boot can not inject the repository in my main
method, that is, outside of a Bean?
I would not say anything if I called println
before the run
in my main method. Then of course, F
field would be null
since Spring Boot did not start yet. But after it starts, I would like it to inject the repository in the F
field and that repository stays injected after run
method returns so that I can work with it inside the main
method without having to create Beans.
I know that I have to use Beans, just, I want to know what is the reason for it not working in the main
method as well. Thank you.