Spring Boot

@注解

@SpringBootApplication

申明让spring boot自动给程序进行必要的配置,这个配置等同于@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

@ResponseBody

该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful的api,该注解一般会配合@RequestMapping一起使用。

@Controller (配合模板使用,return 到模板)

用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。

  • @PathVariable
    获取url中的数据
    1
    2
    3
    4
    @RequestMapping(value = "/person/{id}",method = RequestMethod.GET)
    public String person(@PathVariable(value = "id",required = false ,defaultValue = "000") String id){
    return "name"+name+"age"+age;
    }

请求方式:localhost:8080/person/100000

  • @RequestParam
    获取请求参数的值
    @GetMapping(“/person”)
    public String person(@RequestParam(value = “id”,required = false ,defaultValue = “000”) String id){
    return “name”+name+”age”+age;
    }
  • @GetMapping
    组合注解
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @RequestMapping(value = "/person",method = RequestMethod.GET)
    public String person(@PathVariable(value = "id",required = false ,defaultValue = "000") String id){
    return "name"+name+"age"+age;
    }
    #改用组合注解
    @GetMapping("/person")
    public String person(@RequestParam(value = "id",required = false ,defaultValue = "000") String id){
    return "name"+name+"age"+age;
    }

@RestController (Spring4)

@ResponseBody和@Controller的合集

RequestMapping

提供路由信息,负责URL到Controller中的具体函数的映射
a composed annotation that acts as a shortcut for

  • @PostMapping
    @RequestMapping(method = RequestMethod.POST)
  • @PutMapping
    @RequestMapping(method = RequestMethod.PUT)
  • @DeleteMapping
    @RequestMapping(method = RequestMethod.DELETE)
  • @PatchMapping
    @RequestMapping(method = RequestMethod.PATCH)

@EnableAutoConfiguration

Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们

@ComponentScan

表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类

@Configuration

相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件

@Import

用来导入其他配置类

@ImportResource

用来加载xml配置文件

@Autowired

自动导入依赖的bean

@Service

一般用于修饰service层的组件

@Repository

使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项

@Bean

用@Bean标注方法等价于XML中配置的bean

@Value

注入Spring boot application.properties配置的属性的值

@Qualifier

@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者

@Inject

等价于默认的@Autowired,只是没有required属性

@Transactional

事务

配置文件

有两种配置文件的写法,使用.properties 或者使用yml

.properties

1
2
server.port:8080
server.context-path:/hello

.yml (yml语法)

1
2
3
4
server:
port: 8080
context-path: /hello
age: 100

使用配置文件中的属性取值,使用@Value注入,配置文件中不用管值的类型:

1
2
@Value("${age}")
private String age;

配置文件中再使用配置:

1
2
3
age: 100
name: imchen
content: "cage: ${age},cname: ${name}"

使用配置类:

1
2
3
4
Person:
age: 18
name: imchen
height: 1999

创建一个配置类,使用注解@ConfigurationProperties(prefix=”Person”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Component
@ConfigurationProperties(prefix="Person")
public class PersonPropertis(){
private Integer age;
private String name;
private Integer height;
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age=age
}
public Integer getHeight(){
return height;
}
public void setHeight(Integer height){
this.height=height;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
public class PersonController {
@Autowired
private PersonPropertis personPropertis;
@RequestMapping(value = "/person",method = RequestMethod.GET)
public String test(){
return personPropertis.getName();
}
}

多个配置文件使用:

方式一:修改配置文件实现

application.properties

application-dev.properties

application-product.properties

在application.properties中配置

1
2
3
spring:
profile:
active: dev #使用哪一个配置文件

方式二:使用命令切换配置文件

1
2
maven install
java -jar target/demo.jar --spring.profiles.ative=dev

请求

多个请求映射到同一个方法:

1
@RequestMapping(value={"/hello","hi","fine"},method=RequestMethod.GET)

把url映射到类:

1
2
3
4
5
6
7
8
9
10
@Controller
@RequestMapping("/main")
public class PersonController {
@RequestMapping(value = "/person",method = RequestMethod.GET)
public String person(@RequestParam(value = "name",required = false ,defaultValue = "defaultName") String name,@RequestParam(value = "age",required = false,defaultValue = "defaultAge")){
return name;
}
}

请求到person()使用:localhost:8080/main/person