MissBonbon
MissBonbon

Reputation: 161

Spring Boot - How to create manually a bean and pass it to a hash map

Want put a class in a HashMap. For that I have created a Bean with @Service. This is it:

@Service
public class ServiceManagerImpl implements ServiceManager {

@Override
 public void registerService() {
    // registerService will put this in the HashMap!
    dispatcher.registerService("serviceList", getServiceListImpl());
 }

 @Bean
 public BusinessService getServiceListImpl() {
    return new ServiceListManager();
 }
}

Is this the right way to make something like this?

Upvotes: 1

Views: 2007

Answers (1)

so-random-dude
so-random-dude

Reputation: 16485

Move your bean definition from a class annotated with @Service to a configuration class annotated with @Configuration (or at least move to your main class which has @SpringBootApplication annotation, if you have). Then Autowire that bean here in the Service class. `

@Autowired BusinessService businessService

take a look here Where to put @Bean in Spring Boot?

Upvotes: 1

Related Questions