spring context 扫描自定义注解并注入到application context

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(new AnnotationTypeFilter(type));
        Set<BeanDefinition> definitions = scanner.findCandidateComponents("com.java.xxx");
        AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();

        for (BeanDefinition beanDefinition : definitions) {
            Map<String, Object> annotationAttributes = ((ScannedGenericBeanDefinition) beanDefinition).getMetadata().getAnnotationAttributes(type.getName());
            if (annotationAttributes.containsKey("name")) {
                String name = (String) annotationAttributes.get("name");
                beanFactory.registerBeanDefinition(name, beanDefinition);
            } else {
                beanFactory.registerBeanDefinition(beanDefinition.getBeanClassName(), beanDefinition);
            }
        }

原理:通过scanner扫描自定义注解,生成bean definition;

根据注解的name,注入到beanFactory里;

完整代码:

public class TestCaseRunner {
    protected ApplicationContext applicationContext;
    protected Class type = TestClass.class;

    public TestCaseRunner() {
        init(type);
    }

    protected void init(Class type) {
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(new AnnotationTypeFilter(type));
        Set<BeanDefinition> definitions = scanner.findCandidateComponents("com.java.xxx");
        AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();

        for (BeanDefinition beanDefinition : definitions) {
            Map<String, Object> annotationAttributes = ((ScannedGenericBeanDefinition) beanDefinition).getMetadata().getAnnotationAttributes(type.getName());
            if (annotationAttributes.containsKey("name")) {
                String name = (String) annotationAttributes.get("name");
                beanFactory.registerBeanDefinition(name, beanDefinition);
            } else {
                beanFactory.registerBeanDefinition(beanDefinition.getBeanClassName(), beanDefinition);
            }
        }

        this.applicationContext = applicationContext;
    }


  public void run() {
        Map<String, Object> testCaseMap = applicationContext.getBeansWithAnnotation(type);
        for (Object testCaseObj : testCaseMap.entrySet()) {
            Map.Entry testCaseEntry = (Map.Entry) testCaseObj;
            String name = testCaseEntry.getKey().toString();
            Object testCase = testCaseMap.get(name);
            execute(name, testCase);
        }
    }

    protected void execute(String name, Object testCase) {
        Method beforeMethod = null, afterMethod = null;
        List<Method> testMethods = new ArrayList<>();
        Method[] methods = testCase.getClass().getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(TestMethod.class)) {
                testMethods.add(method);
            } else if (method.isAnnotationPresent(BeforeTest.class)) {
                beforeMethod = method;
            } else if (method.isAnnotationPresent(AfterTest.class)) {
                afterMethod = method;
            }
        }

        for (Method method : testMethods) {
            try {
                Object instance = applicationContext.getBean(name);
                if (beforeMethod != null) {
                    beforeMethod.invoke(instance);
                }
                method.invoke(instance);
                if (afterMethod != null) {
                    afterMethod.invoke(instance);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestClass {
    public String name() default "";

    public String description () default "";
}
public class Main {
    public static void main(String[] args) {
        try {
            TestCaseRunner testCaseRunner = new TestCaseRunner();
            testCaseRunner.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
@TestScript(name = "v1_test")
public class Version1Test {
    @Autowired
    @Qualifier("testScriptService")
    private TestScriptService F;

    @TestMethod
    public void test() {
        try {
            Response response = F
                    .setUrl("xxx")
                    .sendRequest()
                    .getResponse();

            boolean result = F.judgeNot(response.getContent(), null);
            System.out.println("result : " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

转载于:https://my.oschina.net/wower/blog/903032

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值