一、注解(annotations)列表
@SpringBootApplication:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Component
@RestController
@Autowired
@PathVariable
@JsonBackReference
@RepositoryRestResourcepublic
二、注解(annotations)详解
@SpringBootApplication:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@ResponseBody:
比如异步获取json数据,加上@responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用。
示例代码:
@RequestMapping(“/test”)
@ResponseBody
public String test(){
return”ok”;
}
@Controller:
示例代码:
@Controller
@RequestMapping(“/demoInfo”)
publicclass DemoController {
@Autowired
private DemoInfoService demoInfoService;
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
System.out.println("DemoController.hello()");
map.put("hello","from TemplateController.helloHtml");
//会使用hello.html或者hello.ftl模板进行渲染显示.
return"/hello";
}
}
@RestController:
示例代码:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(“/demoInfo2”)
publicclass DemoController2 {
@RequestMapping("/test")
public String test(){
return"ok";
}
}
@RequestMapping:
@EnableAutoConfiguration:
你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。
@ComponentScan:
我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。
如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。
@Configuration:
@Import:
@ImportResource:
@Autowired:
@Service:
@Repository:
@Bean:
@Value:
示例代码:
@Value(value = “#{message}”)
private String message;
@Inject:
@Component:
@Bean:
@AutoWired:
@Qualifier:
@Autowired
@Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;
@Resource(name=”name”,type=”type”):
三、JPA注解
@Entity: @Table(name=”“):
@MappedSuperClass:
@NoRepositoryBean:
@Column:
@Id:
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = “repair_seq”):
@SequenceGeneretor(name = “repair_seq”, sequenceName = “seq_repair”, allocationSize = 1):
@Transient:
@JsonIgnore:
@JoinColumn(name=”loginId”):
@OneToOne、@OneToMany、@ManyToOne:
四、springMVC相关注解
@RequestMapping:
- params:指定request中必须包含某些参数值是,才让该方法处理。
- headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。
- value:指定请求的实际地址,指定的地址可以是URI Template 模式
- method:指定请求的method类型, GET、POST、PUT、DELETE等
- consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html;
- produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
@RequestParam:
@RequestParam String a =request.getParameter(“a”)。
@PathVariable:
RequestMapping(“user/get/mac/{macAddress}”)
public String getByMacAddress(@PathVariable String macAddress){
//do something;
}
参数与大括号里的名字一样要相同。