Spring Annotation(注解)

Spring Boot Annotation

@SpringBootApplication

  必须作用在main 方法所在类

@RequestMapping @GetMapping @PostMapping ...

  配置URL映射

@Controller  

  处理HTTP请求

@RestController

  等同于@Controller  + @ResponseBody

@Value("${配置文件application.properties中的属性名}")

@ConfigurationProperties

  实例:  

    application.properties配置如下:

          server.port=8080

          server.testAttribute=ZZZ

       java:

         //省去了用@Value 对类成员一个个注解的麻烦

         @ConfigurationProperties(prefix="server")

       public class ServerProperties(){

         private String port;

         private String testAttribute

       }

 

@Component 

  作用于类  将类讲给Spring管理,便于其他Spring注解处用到该类

  注:在持久层、业务层和控制层中,分别采用@Repository、@Service和@Controller对分层中的类进行凝视,而用@Component对那些比较中立的类进行凝视。

 

@Autowired  @Resource

  修饰字段、构造函数或者设置方法,用于注入

  @Autowired

  private ServerProperties serverProperties;  //无需自己new对象,由@Autowired告知Spring自动注入

  注:@Autowired默认按类型装配

      如果要允许null值 - @Autowired(required=false)

      使用名称装配 - @Autowired() @Qualifier("bean的name")    

      存疑:Spring Boot 用@Component注解的bean 的 name 是什么,不是没有bean的配置文件吗?

 

@Valid

  表单验证,并将验证结果填入BindingResult

posted @ 2018-05-09 23:30  Mr.Zhongzz  阅读(111)  评论(0编辑  收藏  举报