Your DemoApplication class is in the com.ag.digital.demo.boot package and your LoginBean class is in the com.ag.digital.demo.bean package. By default components (classes annotated with @Component) are found if they are in the same package or a sub-package of your main application class DemoApplication. This means that LoginBean isn’t being found so dependency injection fails.
There are a couple of ways to solve your problem:
- Move
LoginBeanintocom.ag.digital.demo.bootor a sub-package. - Configure the packages that are scanned for components using the
scanBasePackagesattribute of@SpringBootApplicationthat should be onDemoApplication.
A few of other things that aren’t causing a problem, but are not quite right with the code you’ve posted:
@Serviceis a specialisation of@Componentso you don’t need both onLoginBean- Similarly,
@RestControlleris a specialisation of@Componentso you don’t need both onDemoRestController DemoRestControlleris an unusual place for@EnableAutoConfiguration. That annotation is typically found on your main application class (DemoApplication) either directly or via@SpringBootApplicationwhich is a combination of@ComponentScan,@Configuration, and@EnableAutoConfiguration.