0

My Spring Boot application is configured entirely using annotations (no XML context file). The root class looks like this:

package uk.org.sehicl.website;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import uk.org.sehicl.website.users.EmailSender;
import uk.org.sehicl.website.users.UserDatastore;
import uk.org.sehicl.website.users.UserManager;
import uk.org.sehicl.website.users.impl.RedisDatastore;
import uk.org.sehicl.website.users.impl.SendgridSender;

@SpringBootApplication
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public UserDatastore userDatastore()
    {
        return new RedisDatastore(System.getenv("REDIS_URL"));
    }

    @Bean
    public EmailSender emailSender()
    {
         return new SendgridSender();
    }

    @Bean
    public UserManager userManager()
    {
        return new UserManager();
    }
}

The UserManager bean contains @Autowired dependencies on the other two:

@Autowired
private UserDatastore datastore;

@Autowired
private EmailSender emailer;

When I run the application, everything starts up fine; but the datastore and emailer fields of the UserManager bean are null.

I'm sure I must be missing something really obvious, but how do I get these autowired dependencies to be, erm, autowired?

Jeremy Hicks
  • 121
  • 6
  • 1
    You’ve taken over creation of the UserManager yourself but don’t seem to be providing those dependencies. Just give the class an appropriate stereotype annotation and remove that method. – jonrsharpe Oct 08 '17 at 08:23
  • Did you try with `@Configurable`? – imk Oct 08 '17 at 10:19
  • Show us the error you get, I copied your code and it wires the beans correctly. The difference was that I put everything to one package, been to lazy to recreate them :) – DevDio Oct 08 '17 at 10:30
  • I've just spotted the stupid mistake I had made, and it's all working now. Sorry for wasting your time, and thanks for the replies. – Jeremy Hicks Oct 09 '17 at 11:29

0 Answers0