6
votes

I am building a basic program of "hello world" in SpringBoot

Code

MyController.java

package controllers;
import org.springframework.stereotype.Controller;

@Controller
public class MyController {
    public String hello() {
        System.out.println("Hello World");
        return "foo";
    }
}

DemoApplication.java

package di.prac;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import controllers.MyController;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext ctx=SpringApplication.run(DemoApplication.class, args);
        MyController m = (MyController)ctx.getBean("myController");
        m.hello();
        System.out.println("*******"+Arrays.asList(ctx.getBeanDefinitionNames()));

    }
}

I am using eclipse and created this project from http://start.spring.io/ without any dependencies.

I learned that Spring create the bean of MyController class with name myController ,but Spring is not able to find myController bean

ERROR

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myController' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:686) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1210) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) at di.prac.DemoApplication.main(DemoApplication.java:16)

Please find and explain the error in the Project

3
Your controller is in a different package as your demo application class.Indraneel Bende
Place MyController in di.prac. This will allow spring boot to perform automatic component scanningAjit Soman

3 Answers

10
votes

Place your controller under sub package of di.prac like di.prac.controllers or use @ComponentScan on your controller. By default, Spring scans the current and sub packages where your main application is present. If you want to scan other packages too, then you can specify the packages in @SpringBootApplication as an argument like.

@SpringBootApplication(scanBasePackages = {"com.xyz.controllers", "com.abc.models""})

We should avoid putting the @Configuration class in the default package (i.e. by not specifying the package at all). In this case, Spring scans all the classes in all jars in a classpath. That causes errors and the application probably doesn't start.

4
votes

For your controller to be available in the context of Spring, you need to define that it is managed by the Spring container. Only the @Controller annotation is not enough, it indicates only the stereotype of your bean, as well as the annotations @Repository and @Service.

In cases where the beans have these annotations and are managed by Spring, it is because their packages that the spring is scanning to search for them has been specified programmatically or per xml. In your case, you should annotate your DemoApplication class with 2 other annotations:

  1. @Configuration - Allows access to spring context
  2. @ComponentScan - Packages to be scanned by Spring

    @Configuration
    @ComponentScan (basePackages = {"controllers"})
    public class DemoApplication {
            public static void main(String[] args) {
            ApplicationContext ctx=SpringApplication.run(DemoApplication.class, args);
            MyController m = (MyController)ctx.getBean("myController");
            m.hello();
            System.out.println(Arrays.asList(ctx.getBeanDefinitionNames())); 
        }
    }
    
1
votes

Just encountered same problem, solution is simple.You just (me also) created package "controllers" on the wrong place. It should be created not in java folder but under folder with name of your project. Simple but deadly mistake. Your code is written perfectly fine.