Spring Boot custom controller, cannot be scanned

Source: Internet
Author: User

In the Springboot official website According to the introduction wrote a Springboot program

Pom.xml

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId> Spring-boot-starter-parent</artifactid>    <version>1.5.2.RELEASE</version></parent> <dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency></dependencies>

  

Java files

Package Hello;import Org.springframework.boot.*;import Org.springframework.boot.autoconfigure.*;import Org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@ Controller@enableautoconfigurationpublic class application{    @RequestMapping ("/")    @ResponseBody    String Home () {        return "Hello world!";    }    public static void Main (string[] args) throws Exception {        springapplication.run (samplecontroller.class, args);}    }

And I wrote a controller.

However, there is no way to scan your own defined controller (if you are using idea, you can see clearly if you scan the icon). The results of the access results are as follows:

Depressed extremely, to night search result said is Samplecontroller side position is wrong, should let start class and Controller's package in the same level directory, but to me but no effect.

The official recommended location for Application.java:

Finally tried to modify the next application on the note, I originally copied the official code with the @controller and @EnableAutoConfiguration, replaced by @ Springbootapplication annotations , unexpectedly can scan to controller

Really depressed incredibly is this reason, official document says @controller and @EnableAutoConfiguration and @springbootapplication no difference.

Be baffled by this problem all night, should jump the pit one not to be few.

To continue writing:

and checked the official document finally found the reason, the reason is:

@Controller and @EnableAutoConfiguration should add one more note:@ComponentScan can do it.

Summarize:

Two annotation configurations using the Springboot scan:

1. @Controller

@EnableAutoConfiguration

@ComponentScan

2,@springbootapplication

@SpringBootApplicationAnnotations are equivalent to using the default properties @Configuration , @EnableAutoConfiguration and @ComponentScan :

In addition Application.java should also follow the official recommendations in the root directory, so as to scan to the service and DAO, otherwise it will cause, the problem of scanning without annotations.

Spring Boot custom controller, cannot be scanned

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.