1、@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。
@SpringBootApplication public class Boot02YamlApplication { public static void main(String[] args) { SpringApplication.run(Boot02YamlApplication.class, args); } }
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan public class Boot02YamlApplication { public static void main(String[] args) { SpringApplication.run(Boot02YamlApplication.class, args); } }
2、@Configuration :等同于spring的XML配置文件;使用Java代码可以检查类型安全。
package com.spring.boot01helloworld2.config; import ch.qos.logback.core.db.DBHelper; import com.spring.boot01helloworld2.bean.Pet; import com.spring.boot01helloworld2.bean.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.import; @import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = true) public class Myconfig { @Bean public User user01(){ User zhangsan = new User("zhangsan",19); //user组件依赖了Pet组件,true是成立的 zhangsan.setPet(tomcatPet()); return zhangsan; } @Bean("tom") public Pet tomcatPet(){ return new Pet("tomcat"); } }
3、@EnableAutoConfiguration 自动配置。
4、@ComponentScan 组件扫描,可自动发现和装配一些Bean。
5、@Component 可配合@ConfigurationProperties使用,把JavaBean加载到容器中并且可以使用Java读取到properties文件中的内容。
@Data @Component @ConfigurationProperties(prefix = "mycar") public class Car { private String brand; private Integer price; }
6、@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。
@RestController public class HelloController { @RequestMapping("/person") public Person person(){ return person; } }
7、@ResponseBody:表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。在使用@RequestMapping后,返回值通常解析为跳转路径,加上
@Controller public class HelloController { @RequestMapping("/person") @ResponseBody public Person person(){ return person; } }
8、@Controller:用于定义控制器类,在spring 容器中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。
@Controller public class Controller { @Autowired private StudentService studentService; @RequestMapping(value = "/hello") public ModelAndView hello () { ModelAndView mv = new ModelAndView(); mv.addObject("msg", "Hello,World!"); mv.setViewName("/WEB-INF/views/hello.jsp"); return mv; } }
9、@AutoWired:自动导入依赖的bean。byType方式。把配置好的Bean拿来用,完成属性、方法的组装,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。当加上(required=false)时,就算找不到bean也不报错。
10、@Qualifier:当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。与@Autowired配合使用。@Qualifier限定描述符除了能根据名字进行注入,但能进行更准确的控制如何选择候选者。
@Autowired @Qualifier(value = “studentService”) private StudentService studentService;
11、@PathVariable获取参数。
@PathVariable("xxx") 通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“) @RequestMapping(value=”user/{id}/{name}”) 请求路径:http://localhost:8080/hello/show5/1/james @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping("ghost/{id}/{name}") public ModelAndView error(@PathVariable("id") int id ,@PathVariable("name") String names){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","返回错误信息"); mv.setViewName("error"); return mv; } }
12、@Bean:意思把bean对象 交给spring容器管理,就跟之前在xml中写 一样
@Bean public User user01(){ User zhangsan = new User("zhangsan",19); //user组件依赖了Pet组件,true是成立的 zhangsan.setPet(tomcatPet()); return zhangsan; }
之前在xml中的写法:
13、@Value:注入Spring boot application.properties配置的属性的值。
@Value(value = “#{message}”) private String message;
14、@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。
15、@Service:一般用于修饰service层的组件
16、@importResource:用来加载xml配置文件。
17、@import:用来导入其他配置类。
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将实体对象持久化到数据库中。JPA的总体思想和现有Hibernate、Toplink、JDO等ORM框架大体一致。
JPA的实体类要和数据库一一对应,比如我对一条原生的sql查询语句建立了一个实体来存放数据,假设这个实体类叫User,结果运行之后,我的数据库里面就自动建了一个User表,里面内容是空的。
1、@RequestMapping
@RequestMapping(“/path”)表示该控制器处理所有“/path”的UR L请求。
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)类型中包含该指定类型才返回
2、@RequestParam
将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)
语法:
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””) value:参数名 required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。 defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)