Previously On:
在写demo的时候,发现对于细节,把握的不到位,现在就springMVC在注解方面和@Transactional事务读一遍官方文档,写一下查找方便快速
1、只把前后端分离相关的常用注解就官方doc撸一下,非前后端分离的注解尽量不讲,官方网址:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc
2、常见的SpringMVC和tx注解(方便快速查找)
Annotated Controllers
-
"Spring MVC provides an annotation-based programming model where @Controller and @RestController components use annotations to express request mappings, request input, exception handling, and more"
springMVC使用了基于注解的编程方式,来处理request的映射、request的传值、异常处理等
-
"Annotated controllers have flexible method signatures and do not have to extend base classes nor implement specific interfaces"
springMVC再也不需要继承基类也不需要继承接口,仅需要加上注解就可以使用
1.Declaration
-
"The @Controller stereotype allows for auto-detection, aligned with Spring general support for detecting @Component classes in the classpath and
auto-registering bean definitions for them."
加上了@Controller注解的类,是可以被自动检测到的,这就是spring中的普遍做饭,类似被标注了@Component的类在类路径下可以被自动的注册一样
-
" It also acts as a stereotype for the annotated class, indicating its role as a web component."
它同样可以被当做一个web的注解组件(这里更深的意思是,我们往往是把controller进行单独扫描的,不与service和dao放在一个容器,springMVC是spring的子容器)
-
" To enable auto-detection of such @Controller beans, you can add component scanning to your Java configuration"
官方doc说明既可以用java代码的方式进行springMVC的扫描配置,也可以用xml,我们通常使用xml的方式
-
" @RestController is a composed annotation that is itself meta-annotated with @Controller and @ResponseBody to indicate a controller whose every
method inherits the type-level @ResponseBody annotation and, therefore, writes directly to the response body versus view resolution and rendering with an HTML template."
@RestController = @Controller + @Responsebody,被注解了@RestController的类的所有的方法都会继承@Responsebody,被标注了@ResponseBody的方法,会直接把返回值写到response的body区,而不会返回一个view或者一个html模板
AOP Proxies
-
" In some cases, you may need to decorate a controller with an AOP proxy at runtime. One example is if you choose to
have @Transactional annotations directly on the controller. When this is the case, for controllers specifically, we recommend using class-based proxying."
有时候你会需要使用到AOP动态代理,如果你用@Transactional注解直接标注你的controller上,我们推荐使用基于类的代理方式(动态代理分为基于接口的JDK动态代理&基于类的cglib的动态代理,有兴趣的小伙伴可以去查一下)
-
" This is typically the default choice with controllers. However, if a controller must implement an interface that is not a Spring Context callback
(such as InitializingBean, *Aware, and others), you may need to explicitly configure class-based proxying. For example, with <tx:annotation-driven/>, you can change to <tx:annotation-driven proxy-target-class="true"/>