I would like to set up a starter Spring Boot project like this:
MyFirstSpringApplication.java:
package com.myfirstspring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyFirstSpringApplication {
public static void main(String[] args) {
SpringApplication.run(MyFirstSpringApplication.class, args);
}
}
HomeController.java:
package com.myfirstspring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
private SpyGirl spy;
@Autowired
public void setSpy(SpyGirl spyFn) {
this.spy = spyFn;
}
@RequestMapping("/")
public String home() {
return this.spy.letsSpeak();
}
}
SpyGirl.java:
package com.myfirstspring;
import org.springframework.context.annotation.Scope;
@Scope("session")
public class SpyGirl {
public String letsSpeak() {
return "I am the worst spy in the world! (NetBeans)";
}
}
But I got this error:
Description:
Parameter 0 of method setSpy in com.myfirstspring.HomeController required a bean of type 'com.myfirstspring.SpyGirl' that could not be found.
Action:
Consider defining a bean of type 'com.myfirstspring.SpyGirl' in your configuration.
2020-05-25 13:39:10.795 WARN 26852 --- [ restartedMain] o.s.boot.SpringApplication : Unable to close ApplicationContext
If I remove the setter completely with the @Autowired annotation (from HomeController.java) and initialize spy variable as new SpyGirl() it works.
What am I doing wrong?