I'm a beginner when I used the usual config and context everything was fine. how to fix it with @SpringBootApplication
Here is the code of the simplest program:
package com.example;
import com.example.domain.Print;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DbApplication {
public static void main(String[] args) {
SpringApplication.run(DbApplication.class, args);
Print print = new Print();
print.testSayHello();
}
}
package com.example.domain;
import org.springframework.stereotype.Component;
@Component
public class Test {
public void sayHello(){
System.out.println("Hello");
}
}
package com.example.domain;
import org.springframework.beans.factory.annotation.Autowired;
public class Print {
@Autowired
public Test test;
public void testSayHello(){
test.sayHello();
}
}
returns the following Exception:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.example.domain.Test.sayHello()" because "this.test" is null
at com.example.domain.Print.testSayHello(Print.java:11)
at com.example.DbApplication.main(DbApplication.java:14)
I tried to replace everything with the usual сontext and config, everything worked, but now I need @SpringBootApplication, help pls