0

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?

hazazs
  • 39
  • 1
  • 8
  • 2
    You only have `@Scope` you also need `@Component` on your `SpyGirl` to make it a spring bean. ] – M. Deinum May 25 '20 at 11:48
  • still got error: ```Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-05-25 14:08:26.427 ERROR 24872 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed 2020-05-25 14:08:26.434 WARN 24872 --- [ restartedMain] o.s.boot.SpringApplication : Unable to close ApplicationContext``` – hazazs May 25 '20 at 12:09
  • 1
    Without the proper error (instead of a snippet) we don't know what is wrong. It isn't related to autowiring (assuming you are following the best practices for structuring a Spring Boot application!) – M. Deinum May 25 '20 at 12:17

3 Answers3

2

As said in the error message, Spring cannot find a bean of type SpyGirl. @Scope is not enough to have it seen as a bean. Add @Component on your class.

gervais.b
  • 2,294
  • 2
  • 22
  • 46
1

Remove @Autowired on top of the setSpy method. instead add it to the SpyGirl DI.

@RestController
public class HomeController {

    @Autowired
    private SpyGirl spy;

    @RequestMapping("/")
    public String home() {
        return spy.letsSpeak();
    }
}

use annotation like @Component or @Service to the SpyGirl class.

@Scope("session")
@Service
public class SpyGirl {
    public String letsSpeak() {
        return "I am the worst spy in the world! (NetBeans)";
    }
}
Natsu
  • 443
  • 3
  • 10
0

I have found this in the error message:

Error creating bean with name 'spyGirl': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton;

And then I have found a solution in another thread (special thanks to @DimaSan for it):

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)

And also thanks guys (@gervais.b, @M.Deinum, @Natsu D) for the help.

Community
  • 1
  • 1
hazazs
  • 39
  • 1
  • 8