1. 程式人生 > 其它 >動態獲取springboot啟動類上的註解

動態獲取springboot啟動類上的註解

package com.demo.other;

import com.dtflys.forest.annotation.ForestScan;
import com.dtflys.forest.scanner.ClassPathClientScanner;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.ClassUtils; import
org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; public class TestFlagC2 implements ImportBeanDefinitionRegistrar, ResourceLoaderAware { private ResourceLoader resourceLoader; public TestFlagC2() { } @Override public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader; } @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(TestFlag2.class.getName())); List<String> basePackages = new ArrayList(); String[] var5 = annoAttrs.getStringArray("value"); System.out.println("values.len="+var5.length); for (String s :var5) { System.out.println("values=" + s); } int var6 = var5.length; int var7; String pkg; for(var7 = 0; var7 < var6; ++var7) { pkg = var5[var7]; if (StringUtils.hasText(pkg)) { basePackages.add(pkg); } } var5 = annoAttrs.getStringArray("basePackages"); var6 = var5.length; for(var7 = 0; var7 < var6; ++var7) { pkg = var5[var7]; if (StringUtils.hasText(pkg)) { basePackages.add(pkg); } } Class[] var9 = annoAttrs.getClassArray("basePackageClasses"); var6 = var9.length; for(var7 = 0; var7 < var6; ++var7) { Class<?> clazz = var9[var7]; basePackages.add(ClassUtils.getPackageName(clazz)); } String configurationId = annoAttrs.getString("configuration"); ClassPathClientScanner scanner = new ClassPathClientScanner(configurationId, registry); if (this.resourceLoader != null) { scanner.setResourceLoader(this.resourceLoader); } scanner.registerFilters(); scanner.doScan(StringUtils.toStringArray(basePackages)); } public void say(){ System.out.println("this is TestFlagC2.say22222"); } }
package com.demo.other;

import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({TestFlagC2.class})
public @interface TestFlag2 {
    String[] value() default {};
}
View Code
package com.demo;

import com.demo.model.Order;
import com.demo.other.TestFlag;
import com.demo.other.TestFlag2;
import com.dtflys.forest.annotation.ForestScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;


@ForestScan(basePackages = "com.demo")
@SpringBootApplication
@Import({Order.class})
@TestFlag
@TestFlag2(value={"ccccc01"})
public class SpringBootdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootdemoApplication.class, args);
    }
}
View Code
package com.demo.other;

import com.dtflys.forest.annotation.ForestScan;
import com.dtflys.forest.scanner.ClassPathClientScanner;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author :qint
* @date :Created in 2021/12/7 18:48
* @description:${description}
* @modified By:
* @version: $version$
*/
public class TestFlagC2 implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
private ResourceLoader resourceLoader;

public TestFlagC2() {
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(TestFlag2.class.getName()));
List<String> basePackages = new ArrayList();
String[] var5 = annoAttrs.getStringArray("value");
System.out.println("values.len="+var5.length);
for (String s :var5) {
System.out.println("values=" + s);
}
int var6 = var5.length;

int var7;
String pkg;
for(var7 = 0; var7 < var6; ++var7) {
pkg = var5[var7];
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}

var5 = annoAttrs.getStringArray("basePackages");
var6 = var5.length;

for(var7 = 0; var7 < var6; ++var7) {
pkg = var5[var7];
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}

Class[] var9 = annoAttrs.getClassArray("basePackageClasses");
var6 = var9.length;

for(var7 = 0; var7 < var6; ++var7) {
Class<?> clazz = var9[var7];
basePackages.add(ClassUtils.getPackageName(clazz));
}

String configurationId = annoAttrs.getString("configuration");
ClassPathClientScanner scanner = new ClassPathClientScanner(configurationId, registry);
if (this.resourceLoader != null) {
scanner.setResourceLoader(this.resourceLoader);
}

scanner.registerFilters();
scanner.doScan(StringUtils.toStringArray(basePackages));
}
public void say(){
System.out.println("this is TestFlagC2.say22222");
}
}