构建后端第3篇之---springb @Alias注解使用
阅读原文时间:2023年07月08日阅读:1

write by 张艳涛 in 202210210,after today i will  write aritles by english,because english is so diffent from chiness that i can't

understand english in java,spring doc notice ,so i begin to use english for a lifelong time. atthought  is hard for me ,but i am a man would like learn little by little , yes  is the only way to learn language by use it in common daily time

Tody I would lile recode usage of spring annotion @Aliasfor


ToIntroduce @Aliasfor , you master kown the  @SpringBootApplication ,it uses start Main Class, then show the context

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

/\*\*  
 \* Exclude specific auto-configuration classes such that they will never be applied.  
 \* @return the classes to exclude  
 \*/  
@AliasFor(annotation = EnableAutoConfiguration.class)  
Class<?>\[\] exclude() default {};

/\*\*  
 \* Exclude specific auto-configuration class names such that they will never be  
 \* applied.  
 \* @return the class names to exclude  
 \* @since 1.3.0  
 \*/  
@AliasFor(annotation = EnableAutoConfiguration.class)  
String\[\] excludeName() default {};

/\*\*  
 \* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}  
 \* for a type-safe alternative to String-based package names.  
 \* @return base packages to scan  
 \* @since 1.3.0  
 \*/  
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")  
String\[\] scanBasePackages() default {};

/\*\*  
 \* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to  
 \* scan for annotated components. The package of each class specified will be scanned.  
 \* <p>  
 \* Consider creating a special no-op marker class or interface in each package that  
 \* serves no purpose other than being referenced by this attribute.  
 \* @return base packages to scan  
 \* @since 1.3.0  
 \*/  
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")  
Class<?>\[\] scanBasePackageClasses() default {};

}

and  with

@SpringBootApplication(scanBasePackages ={ "com.zyt" } )//这个是rouyi的方案
public class ZytAuthApplication {
public static void main(String[] args) {
SpringApplication.run(ZytAuthApplication.class, args);
System.out.println("(◠‿◠)ノ゙ 授权认证中心启动成功 ლ(´ڡ`ლ)゙ \n" );
}
}

you can know the attibute  (scanBasePackages ={ "com.zyt" } ) is for

@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")  
String\[\] scanBasePackages() default {};

then the  SpringBootApplication Annotation attibute scanBasePackages ={ "com.zyt" } , so we can undestand

@AliasFor isfor give scanBasePackages ={ "com.zyt" } to ComponentScan.class ,

ComponentScan overrite its attibute with {"com.zyt"}

In One World like meta-annotation get attibute value from Top Annotation 's attibute
this is call Explicit alias for attribute in meta-annotation:


Explicit aliases within an annotation:

public @interface ContextConfiguration {

  @AliasFor("locations")  
  String\[\] value() default {};

  @AliasFor("value")  
  String\[\] locations() default {};

  // ...  

}

for more


Example: Implicit Aliases within an Annotation

@ContextConfiguration
public @interface MyTestConfig {

  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")  
  String\[\] value() default {};

  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")  
  String\[\] groovyScripts() default {};

  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")  
  String\[\] xmlFiles() default {};  

}

@MyTestConfig
public @interface GroovyOrXmlTestConfig {

  @AliasFor(annotation = MyTestConfig.class, attribute = "groovyScripts")  
  String\[\] groovy() default {};

  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")  
  String\[\] xml() default {};  

}

for more

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器