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
@SpringBootApplication
Annotations 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