Based on the previous article, a springboot simple project was built, in which the Hellowordcontroller.java interface class was written, and a main function was written in the class to start the portal for the interface service of the class. If you add more than one interface service class at this point, not modifying any code is an interface service that cannot access the new class.
In fact, Springboot provides a starting method for configuring the Global Scan Interface service class uniformly, this article describes how to use:
How to configure the Unified boot Portal
Create a new app package under the SRC root directory, creating a App.java class under the package that is configured as a generic Startup entry class for the project by code:
Packageapp;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;ImportOrg.springframework.context.annotation.ComponentScan; @ComponentScan ("Com.dx.controller") @EnableAutoConfiguration Public classApp { Public Static voidMain (string[] args) {Springapplication.run (App.class, args); }}
Note:
1) Componentscan is used to configure the path of the package currently in the scan class;
2) enableautoconfiguration automatic injection;
3) Springapplication.run (App.class,args) is used as the server boot portal to start the interface service.
Test the interface service class:
The interface service classes already owned are:
Hellowordcontroller.java
PackageCom.dx.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod; @Controller Public classHellowordcontroller {@RequestMapping (value= "/index", method =requestmethod.get) PublicString Index () {SYSTEM.OUT.PRINTLN ("Index is running ..."); return"Welcome"; }}
Restcontrollertest.java
PackageCom.dx.controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportJava.util.HashMap;ImportJava.util.Map; @RestController Public classrestcontrollertest {@RequestMapping ("/rest") PublicMap<string, object>Rest () {Map<string, object> result =NewHashmap<>(); Result.put ("Code", "404"); Result.put ("MSG", "Unkown"); returnresult; }}
, run the app. App.java class, wait for startup to complete.
1) Visit: http://127.0.0.1:8888/rest
2) Access:
Springboot (b): Set Springboot the same interface program start entry