日常提交

This commit is contained in:
2022-12-19 01:34:09 +08:00
parent 0f6c62e459
commit 3dec6613e8
2 changed files with 40 additions and 3 deletions

View File

@@ -47,14 +47,14 @@
- execution匹配目标方法的执行可以在括号中接收一个函数签名包含返回类型、函数名和函数参数类型 - execution匹配目标方法的执行可以在括号中接收一个函数签名包含返回类型、函数名和函数参数类型
```java ```java
// 被@JointPoint注解标注的方法必须具有void的返回类型 // 被@JointPoint注解标注的方法必须具有void的返回类型
@JoinPoint("execution(* Point.*(..))") @Pointcut("execution(* Point.*(..))")
void methodInjected() { void methodInjected() {
} }
``` ```
- within:匹配声明在某一特定类中的方法 - within:匹配声明在某一特定类中的方法
```java ```java
@JoinPoint("within(Point)") @Pointcut("within(Point)")
``` ```
- this匹配生成的代理对象为该类型的一个实例 - this匹配生成的代理对象为该类型的一个实例
- target匹配目标对象为该类型的一个实例 - target匹配目标对象为该类型的一个实例
@@ -65,7 +65,7 @@
- @annotation执行的方法具有指定注解 - @annotation执行的方法具有指定注解
- Spring AOP同样支持将JoinPoint匹配为具有特定name的Spring bean对象 - Spring AOP同样支持将JoinPoint匹配为具有特定name的Spring bean对象
```java ```java
@JoinPoint("bean(nameA) || bean(nameB))") @Pointcut("bean(nameA) || bean(nameB))")
``` ```
- ## Spring AOP中的Advice - ## Spring AOP中的Advice
- Advice和Pointcut Expresion相关联主要可以分为before、after、around等种类 - Advice和Pointcut Expresion相关联主要可以分为before、after、around等种类

View File

@@ -462,3 +462,40 @@ public class User {
} }
``` ```
### Model ### Model
#### @ModelAttribute注解用法
-@ModelAttribute注解标记在handler method的参数上用于创建或访问一个对象该对象从model中获取并且该对象通过WebDataBinder与http request绑定在一起。
-@ModelAttribute注解绑定在位于@Controller或@ControllerAdvice类中的方法上用于在任何handler method调用之前初始化model
-@ModelAttribute注解绑定在@RequestMapping方法上用于标注该方法的返回值是一个model attribute
#### @ModelAttribute作用于Controller类中普通方法上
对于上述的第二种使用一个controller类中可以含有任意数量个@ModelAttribute方法,所有的这些方法都会在@RequestMapping方法调用之前被调用。(**同一个@ModelAttribute方法,也可以通过@ControllerAdvice在多个controllers之间进行共享**)。
@ModelAttribute方法支持可变的参数形式,其参数形式可以和@RequestMapping方法中的参数形式一样。(**但是,@ModelAttribute方法的参数中不能含有@ModelAttribute注解本身参数也不能含有和http请求体相关的内容**)。
```java
@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
model.addAttribute(accountRepository.findAccount(number));
// add more ...
}
```
也可以通过如下方式向model中添加attribute
```java
// 只向model中添加一个属性
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountRepository.findAccount(number);
}
```
> 在向model中添加属性时如果attribute name没有显式指定那么attribute name将会基于attribute value的类型来决定。可以通过model.addAttribute(attributeName,attributeValue)来指定attribute name或者通过指定@ModelAttribute的name属性来指定attribute name
#### @ModelAttribute作用于@RequestMapping方法上
对于第三种使用,可以将@ModelAttribute注解标注在@RequestMapping方法之上在这种情况下方法的返回值将会被解释为model attribute。**在这种情况下@ModelAttribute注解不是必须的应为该行为是html controller的默认行为除非返回值是String类型此时返回值会被解释为view name。**
可以通过如下方式来自定义返回model attribute的attribute name如下图所示
```java
// 指定attribute name为“myAccount”
@GetMapping("/accounts/{id}")
@ModelAttribute("myAccount")
public Account handle() {
// ...
return account;
}
```