• 自定义注解通过spring获取Bean


    自定义注解:

    package com.example.demo.ann;
    
    import org.springframework.stereotype.Repository;
    
    import java.lang.annotation.*;
    
    //注解运行的位置
    @Target(ElementType.TYPE)
    //运行的时机
    @Retention(RetentionPolicy.RUNTIME)
    //DOC
    @Documented
    @Repository
    public @interface FirstRespoistory {
        String value() default "";
    }

    要获取的类:

    package com.example.demo.ann;
    
    @FirstRespoistory(value = "firstRe")
    public class FirstReo {
    
        public void test(){
            System.out.println("ceshislaishi");
        }
    }

    获取方式:

    package com.example.demo.ann;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.WebApplicationType;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    
    @ComponentScan("com.example.demo.ann")
    public class AnnoTest {
        public static void main(String[] args) {
            ConfigurableApplicationContext context = new SpringApplicationBuilder(AnnoTest.class).web(WebApplicationType.NONE).run(args);
            FirstReo firstRe = context.getBean("firstRe", FirstReo.class);
            firstRe.test();
            context.close();
    
        }
    }
  • 相关阅读:
    javascript模拟枚举
    javascript实现命名空间效果
    js数组去重
    文件上传插件
    script标签的defer属性
    js数组复制
    更改visual studio2010的主题
    关于json格式在.net前后台传值的详细示例
    where T : class泛型类型约束
    JavaScript模仿鼠标拖动选择功能
  • 原文地址:https://www.cnblogs.com/leeego-123/p/12493768.html
Copyright © 2020-2023  润新知