zoukankan      html  css  js  c++  java
  • spring mvc 注解详解(springMvc mybatis spring)


    1. 声明Bean的注解:
    1.1 @Component : 组件,没有明确的角色
    1.2 @Service : 在业务逻辑层(service层)使用
    1.3 @Repository : 在数据访问层(dao层)使用
    1.4 @Controller : 在展现层(MVC--SpringMVC)使用

    2. 注入Bean的注解:
    2.1 @Autowired : Spring提供的注解
    对类成员变量、方法及构造函数进行标注,完成自动装配的工作。通过 @Autowired的使用来消除set ,get方法
    默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,
    如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用

    2.2 @Inject : JSR-330(Java的依赖注入标准)提供的注解
    和@Aautowired一样

    2.3 @Resource : JSR-250提供的注解
    默认安照名称进行装配,名称可以通过name属性进行指定,
    如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

    2.4 三者的区别
    @Autowired和@Inject,都是通过AutowiredAnnotationBeanPostProcessor类实现的依赖注入,二者具有可互换性。
    @Resource通过CommonAnnotationBeanPostProcessor类实现依赖注入
    @Autowired and @Inject
    Matches by Type
    Restricts by Qualifiers
    Matches by Name
    @Resource
    Matches by Name
    Matches by Type
    Restricts by Qualifiers (ignored if match is found by name)

    3. 配置文件的注解:
    3.1 @Configuration : 声明当前类是个配置类,相当于一个Spring配置的xml文件.
    3.2 @ComponentScan (cn.test.demo): 自动扫描包名下所有使用 @Component @Service @Repository @Controller 的类,并注册为Bean
    3.3 @WiselyConfiguration : 组合注解 可以替代 @Configuration和@ComponentScan
    3.4 @Bean : 注解在方法上,声明当前方法的返回值为一个Bean.
    3.5 @Bean(initMethod="aa",destroyMethod="bb")--> 指定 aa和bb方法在构造之后.Bean销毁之前执行.

    4. AOP切面编程注解:
    @Aspect : 声明这是一个切面
    @After @Before. @Around 定义切面,可以直接将拦截规则(切入点 PointCut)作为参数
    @PointCut : 专门定义拦截规则 然后在 @After @Before. @Around 中调用
    @Transcational : 事务处理
    @Cacheable : 数据缓存
    @EnableAaspectJAutoProxy : 开启Spring 对 这个切面(Aspect )的支持
    @Target (ElementType.TYPE):元注解,用来指定注解修饰类的那个成员 -->指定拦截规则
    @Retention(RetentionPolicy.RUNTIME) --->当定义的注解的@Retention为RUNTIME时,才能够通过运行时的反射机制来处理注解.-->指定拦截规则

    5. @Enable*注解:
    @EnableAsync : 开启异步任务的支持(多线程)
    @EnableScheduling : 开启对计划任务的支持(定时器)
    @EnableWebMVC : 开启对Web MVC 的配置支持
    @EnableAaspectJAutoProxy : 开启Spring 对 这个切面(Aspect )的支持
    @EnableConfigurationProperties 开启对@ConfigurationProperties注解配置Bean的支持
    @EnableJpaRepositories : 开启对Spring Data JAP Repository 的支持
    @EnableTransactionManagement 开启对注解式事物的支持
    @EnableCaching开启注解是缓存的支持.
    @EnableDiscoveryClient 让服务发现服务器,使用服务器.Spring cloud 实现服务发现
    @EnableEurekaServer 注册服务器 spring cloud 实现服务注册@
    @EnableScheduling 让spring可以进行任务调度,功能类似于spring.xml文件中的命名空间<task:*>
    @EnableCaching 开启Cache缓存支持;

    6. SpringMVC 常用注解:
    @Controller : 注解在类上 声明这个类是springmvc里的Controller,将其声明为一个spring的Bean.
    @RequestMapping :可以注解在类上和方法上 映射WEB请求(访问路径和参数)
    @RequestMapping(value= "/convert",produces+{"application/x-wisely"}) 设置访问URL 返回值类型
    @ResponseBody : 支持将返回值放入response体内 而不是返回一个页面(返回的是一个组数据)
    @RequestBody : 允许request的参数在request体中,而不是直接连接在地址后面 次注解放置在参数前
    @Path Variable : 用来接收路径参数 如/test/001,001为参数,次注解放置在参数前
    @RestController : @Controller + @ResponseBody 组合注解
    @ControllerAdvice : 通过@ControllerAdvice可以将对已控制器的全局配置放置在同一个位置
    @ExceptionHandler : 用于全局处理控制器的异常
    @ExceptionHandier(value=Exception.class) -->通过value属性可过滤拦截器条件,拦截所有的异常
    @InitBinder : 用来设置WebDataBinder , WebDataBinder用来自动绑定前台请求参数到Model中.
    @ModelAttrbuute : 绑定键值对到Model中,
    @RunWith : 运行器
    @RunWith(JUnit4.class)就是指用JUnit4来运行
    @RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
    @RunWith(Suite.class)的话就是一套测试集合,
    @WebAppConfiguration("src/main/resources") : 注解在类上,用来声明加载的ApplicationContex 是一个WebApplicationContext ,它的属性指定的是Web资源的位置,默认为 src/main/webapp ,自定义修改为 resource
    @Before : 在 xxx 前初始化

  • 相关阅读:
    luogu_1009 阶乘之和
    codevs_3117 高精度练习之乘法(my struct)
    luogu_2142 高精度减法
    luogu_1601 A+B Problem(高精)
    luogu_1007 独木桥
    luogu_1006 传纸条
    luogu_1004 方格取数
    luogu_1003 铺地毯
    luogu_1002 过河卒
    BZOJ 2560: 串珠子 (状压DP+枚举子集补集+容斥)
  • 原文地址:https://www.cnblogs.com/wangpenglen/p/6060914.html
Copyright © 2011-2022 走看看