@SpringBootApplication
申明讓Spring boot 自動給程序進(jìn)行必要的配置冰肴,這個配置等于:@Configuration 胰蝠,@EnableAutoConfiguration 和 @ComponentScan
@Controller
用于定義控制類,在Spring項目中將用戶發(fā)來的url請求轉(zhuǎn)發(fā)到對應(yīng)的服務(wù)器接口(Service層)
@RestController
用于標(biāo)注控制層組件,等同于@ResponseBody+@Controller
@RequestMapping
提供路由信息,負(fù)責(zé)映射url到具體函數(shù),其組合注解如下:
- @GetMapping
- @PostMapping
- @DeleteMapping
- @PutMapping
@Autowired
自動導(dǎo)入依賴的bean
@Value
注入application中配置的屬性值
@Value(value = “#{message}”)
private String message;
@Bean
用@Bean標(biāo)注方法等價于XML中配置的bean才漆。
@Component
泛指組件,當(dāng)組件不好歸類的時候佛点,我們可以使用這個注解進(jìn)行標(biāo)注醇滥。
@PathVariable
獲取url中的數(shù)據(jù)
RequestMapping(“user/get/mac/{macAddress}”)
public String getByMacAddress(@PathVariable String macAddress){
//do something;
}
@RequestParam
獲取請求中傳過來的值
@RequestParam(Value = "id", required = false, defaultValue =0)
@ConfigurationProperties
將配置文件轉(zhuǎn)成對象,yml配置:
spring:
redis:
dbIndex: 0
hostName: 192.168.58.133
password: nmamtf
port: 6379
timeout: 0
poolConfig:
- maxIdle: 8
- minIdle: 0
- maxActive: 8
- maxWait: -1
定義轉(zhuǎn)換對象:
@Component
@ConfigurationProperties(prefix="spring.redis")
public class RedisProps {
private int dbIndex;
@NotNull
private String hostname;
private String password;
@NotNull
private int port;
private long timeout;
private List<Map<String,String>> poolConfig;
public int getDbIndex() {
return dbIndex;
}
public void setDbIndex(int dbIndex) {
this.dbIndex = dbIndex;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public long getTimeout() {
return timeout;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public List<Map<String, String>> getPoolConfig() {
return poolConfig;
}
public void setPoolConfig(List<Map<String, String>> poolConfig) {
this.poolConfig = poolConfig;
}
}