博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
注解的使用
阅读量:4189 次
发布时间:2019-05-26

本文共 3378 字,大约阅读时间需要 11 分钟。

注解的使用

@Retention(RetentionPolicy.RUNTIME)

1、RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;

2、RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
3、RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;

这3个生命周期分别对应于:Java源文件(.java文件) —> .class文件 —> 内存中的字节码。

那怎么来选择合适的注解生命周期呢?

首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。

@Target注解的作用目标

@Target(ElementType.TYPE) //接口、类、枚举

@Target(ElementType.FIELD) //字段、枚举的常量

@Target(ElementType.METHOD) //方法

@Target(ElementType.PARAMETER) //方法参数

@Target(ElementType.CONSTRUCTOR) //构造函数

@Target(ElementType.LOCAL_VARIABLE)//局部变量

@Target(ElementType.ANNOTATION_TYPE)//注解

@Target(ElementType.PACKAGE) ///包

@Document:说明该注解将被包含在javadoc中

@Inherited:说明子类可以继承父类中的该注解

@Import注解 —— 导入资源,将对象导入的容器里面,也可以导入配置文件,如果配置文件里面配置了来两个对象,使用@Bean.

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)与

@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class }) 的区别

@ConditionalOnWebApplication

@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
WebMvcConfigurerAdapter.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration

其中@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)说明,当没有WebMvcConfigurationSupport对应的bean时,才会使用该配置,所以当我们使用继承WebMvcConfigurationSupport的方式类扩展mvc时,原有的配置则无效。

Import(DelegatingWebMvcConfiguration.class)

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {   private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();   @Autowired(required = false)   public void setConfigurers(List
configurers) { if (!CollectionUtils.isEmpty(configurers)) { this.configurers.addWebMvcConfigurers(configurers); } }

说明@EnableWebMvc最终导入了WebMvcConfigurationSupport。

可以看到,该类也是WebMvcConfigurationSupport的子类,但是相对而言,添加了自己的扩展配置,同时从setConfigurers可以看到,所有WebMvcConfigurer的子类也会被添加到配置中。

其中WebMvcConfigurerAdapter,也是WebMvcConfigurer的子类,这就是为什么我们使用@EnableWebMvc+WebMvcConfigurer的方式可以实现EnableWebMvc的配置加上自己的配置了。

1、springboot默认可以访问以下路径文件(见ResourceProperties):

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
当使用了@EnableWebMvc时,默认的静态资源访问无效了因为默认情况下mvc使用的配置是WebMvcAutoConfiguration,加入该配置变成了WebMvcConfigurationSupport
2、@EnableWebMvc、WebMvcConfigurationSupport、WebMvcConfigurationAdapter
@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法
@EnableWebMvc+extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的WebMvcAutoConfiguration中的设置
@EnableWebMvc+extends WebMvcConfigurationSupport 只会使用@EnableWebMvc
extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@WebMvcAutoConfiguration中的设置
extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式依旧使用springboot的WebMvcAutoConfiguration中的设置
在springboot2.x中,WebMvcConfigurationAdapter已经过时,通过实现接口WebMvcConfigurer可以替代原有规则

转载地址:http://pysoi.baihongyu.com/

你可能感兴趣的文章
《给初学者的Windows Vista的补遗手册》之062
查看>>
有道 值得一道
查看>>
《给初学者的Windows Vista的补遗手册》之057
查看>>
《给初学者的Windows Vista的补遗手册》之056
查看>>
《给初学者的Windows Vista的补遗手册》之055
查看>>
《给初学者的Windows Vista的补遗手册》之045
查看>>
域名1元价,我也来注册一个
查看>>
《给初学者的Windows Vista的补遗手册》之037
查看>>
《给初学者的Windows Vista的补遗手册》之036
查看>>
《给初学者的Windows Vista的补遗手册》之035
查看>>
Spring开发指南 0.8 发布
查看>>
Mac OS X 10.4.7 DMG 文件如何转化成ISO文件
查看>>
线程的优先级
查看>>
Khronos 官方新闻 Windows Vista 和 OpenGL 的事实 ZT
查看>>
HLSL编程实现PhotoShop滤镜效果
查看>>
如果我恨一个人,我就领他到中关村买相机。
查看>>
装ubuntu碰到一件BT的事情
查看>>
关于NVIDIA 的 OpenGL回退到软件模式的问题。
查看>>
OpenGL和D3D中Cubemap的图象方向问题
查看>>
XREAL3D开发转移到csdn的svn服务器上。
查看>>