微服務註解詳解
阿新 • • 發佈:2019-01-03
@Controller:修飾class,用來建立處理http請求的物件
@RestController:Spring4之後加入的註解,原來在@Controller中返回json需要
@ResponseBody來配合,如果直接用@RestController替代@Controller就不需要再配
置@ResponseBody,預設返回json格式。
@RequestMapping:配置url對映
@SpringBootApplication:主類宣告
@EnableEurekaServer註解啟動一個服務註冊中心提供給其他應用進行對話
@EnableDiscoveryClient 註解,該註解能啟用Eureka中的服務類的實現,才能實現Controller中對服務資訊的輸出。
@EnableCircuitBreaker註解開啟斷路器功能:
@EnableZuulProxy註解開啟Zuul
@SpringCloudApplication註解,它整合了@SpringBootApplication、@EnableDiscoveryClient、@EnableCircuitBreaker,主要目的還是簡化配置
使用@EnableEurekaClient就能簡單的開啟Eureka Client中的功能
通過@EnableWebSecurity註解開啟Spring
Security的功能
@ComponentScan(basePackages={"com.fiberhome.config"})
元件掃描,可自動發現和裝配一些Bean
@Configuration 等同於spring的XML配置檔案;使用java程式碼可以檢查型別安全
@EnableAutoConfiguration 自動配置
@Component可配合CommandLineRunner使用,在程式啟動後執行一些基礎任務
eg:
@Component
public class MyCommandLineRunner implements CommandLineRunner{
@Autowired
private UserService userService;
@Override
public void run(String... arg0) throws Exception {
User user = userService.findByUserAcount("admin");
System.out.println(user.getAcount());
}
}