一、常用的获取请求参数的注解
1、@PathVariable:获取url中的数据
@Controller
@RequestMapping("/User")
public class HelloWorldController {
@RequestMapping("/getUser/{uid}")
public String getUser(@PathVariable("uid")Integer id, Model model) {
System.out.println("id:"+id);
return "user";
}
}
2、@RequestParam:获取请求参数的值
@Controller
@RequestMapping("/User")
public class HelloWorldController {
@RequestMapping("/getUser")
public String getUser(@RequestParam("uid")Integer id, Model model) {
System.out.println("id:"+id);
return "user";
}
}
3、@RequestHeader 把Request请求header部分的值绑定到方法的参数上
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
//..
@Controller
public class HelloController {
@RequestMapping(value = "/hello.htm")
public String hello(@RequestHeader(value="User-Agent") String userAgent)
//..
}
}
@RequestMapping(value = "/hello.htm")
public String hello(@RequestHeader(value="User-Agent", defaultValue="foo") String userAgent)
//..
}
4、@CookieValue 把Request header中关于cookie的值绑定到方法的参数上
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/displayHeaderInfo")
public Map<String, Object> displayHeaderInfo(@RequestHeader("User-agent") String userAgent, @RequestHeader(value = "Accept") String[] accepts,@CookieValue("JSESSIONID") String cookie) {
Map<String, Object> response = new HashMap<>();
response.put("accepts", accepts);
response.put("userAgent", userAgent);
response.put("cookie", cookie);
return response;
}
}
二、导入配置文件
1、@PropertySource注解
引入单个properties文件: @PropertySource(value = {“classpath : xxxx/xxx.properties”})
引入多个properties文件: @PropertySource(value = {“classpath : xxxx/xxx.properties”,“classpath : xxxx.properties”})
@PropertySource注解的使用 注意:该注解仅适用于properties 类型配置文件
我们新建一个 person.properties 配置文件,用来存放 Person 类的配置信息。接下来使用 @ PropertySource 注解,来实现通过读取该配置,实现配置文件与 Java Bean 类的注入操作。
//person.properties
person.name=扛麻袋的少年
person.age=27
person.manager=true
person.birthday=2020/03/27
person.map.k1=v1
person.map.k2=v2
person.list=basketball,tennis,swim
person.address.province=山西省
person.address.distinct=晋中市
person.address.county=祁县
javaBean类:
添加 @PropertySource(value = {“classpath:person.properties”})注解,通过 value 属性让它去加载指定路径配置文件。代码如下:
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})//读取指定路径配置文件
public class Person {
private String name;
private int age;
private boolean isManager;
private Date birthday;
private Map<String, Object> map;
private List<String> list;
private Address address;
/**
* 省略get/set/toString() 方法
*/
}
测试情况和项目结构如图,可以实现 person.properties 文件中的配置信息与自定义的 Person 类完成注入操作:
2、@ImportResource导入xml配置文件
可以额外分为两种模式 相对路径classpath,绝对路径(真实路径)file
注意:单文件可以不写value或locations,value和locations都可用
相对路径(classpath):
引入单个xml配置文件:@ImportSource(“classpath : xxx/xxxx.xml”)
引入多个xml配置文件:@ImportSource(locations={“classpath : xxxx.xml” , “classpath : yyyy.xml”})
绝对路径(file):
引入单个xml配置文件:@ImportSource(locations= {“file : d:/hellxz/dubbo.xml”})
引入多个xml配置文件:@ImportSource(locations= {“file : d:/hellxz/application.xml” , “file : d:/hellxz/dubbo.xml”})
取值:使用@Value注解取配置文件中的值
@Value("${properties中的键}") private String xxx;
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
}
}
3、@Import 导入额外的配置信息
功能类似XML配置的,用来导入配置类,可以导入带有@Configuration注解的配置类或实现了ImportSelector/ImportBeanDefinitionRegistrar。
@SpringBootApplication
@Import({SmsConfig.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三、@Transactional 事务注解
在Spring中,事务有两种实现方式,分别是编程式事务管理和声明式事务管理两种方式
编程式事务管理:
编程式事务管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager。对于编程式事务管理,spring推荐使用TransactionTemplate。
声明式事务管理:
建立在AOP之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务,通过@Transactional就可以进行事务操作,更快捷而且简单。推荐使用
透彻的掌握 Spring 中 @transactional 的使用
四、全局异常处理
1、@ControllerAdvice 统一处理异常
@ControllerAdvice 注解定义全局异常处理类
@ControllerAdvice
public class GlobalExceptionHandler {
}
2、@ExceptionHandler 注解声明异常处理方法
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
String handleException(){
return "Exception Deal!";
}
}