Wearybands
Wearybands

Reputation: 2455

Dependency Injection in SpringBoot

I have a controller

@Controller
public class AppController{

   @Autowired
   private IDemoApplicationService service;
   //more code

}

Service

@Service("service")
public class DemoApplicationServiceImpl implements IDemoApplicationService{
    @Override
public void createEmployee(Employee employee){  
    try {
        dao.insertEmployee(employee);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public String updateEmployee(Employee employee, int id) {
    dao.updateEmployee(employee, id);
    return "redirect:/employees";
}

@Override
public String deleteEmployee(int id) {
    dao.deleteEmployee(id);
    return "redirect:/employees";
}

@Override
public String getEmployees(Model model) {
    model.addAttribute("employees", dao.getEmployees());
    return "employees";
}

@Override
public Employee findById(int id) {
    return dao.findById(id);
}
}

Service Interface

public interface IDemoApplicationService {

}

I want to use @Autowired in my controller to use the service but when I run the application I get the following error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoApplication': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.IDemoApplicationService controllers.DemoApplication.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.IDemoApplicationService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Can anyone tell me what should I do to make it work?

Thanks

Upvotes: 1

Views: 2544

Answers (2)

Dhaval Simaria
Dhaval Simaria

Reputation: 1964

Your DemoApplication is your application start-up class.

@SpringBootApplication
public class DemoApplication {

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

Then you need to have a separate controller file as follows:

@Controller
public class AppController {

    @Autowired
    private IDemoApplicationService service;
    //more code
}

@Service("service")
public class DemoApplicationServiceImpl implements IDemoApplicationService{

}

Why you need to make above changes:

@SpringBootApplication is equivalent to using three separate annotations i.e. @EnableAutoConfiguration, @ComponentScan and @Configuration. You can read more about this at Using Spring Boot Docs..

When you provide @Controller before @SpringBootApplication, @Configuration part does not get chance to register beans in the context and hence you are getting the said error.

Upvotes: 0

Almas Abdrazak
Almas Abdrazak

Reputation: 3612

If you look inside @SpringBootApplication it contains a lot of another Spring's annotations

Some of them:

1)@Configuration

2)@ComponentScan

In @SpringBootApplication you can path a param:

@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
    public String[] scanBasePackages() default {};

As you can see it's alias for @ComponentScan to know which packages to scan for classes anotated with @Component or sub annotations like @Service @Repository if you don't provide this param , Spring will scan main package where class annotated with SpringBootApplication is lokated.

Most probably your service locate in a differente package

Upvotes: 1

Related Questions