search

Home  >  Q&A  >  body text

java - 利用spring如何扫描到所有自定义注解,并进行某些操作

hi,各位好
我在自己实现一个controller框架的时候遇到一个问题,我想和spring mvc 一样利用注解来实现url mapping。但是前提是我必须在程序加载的时候必须要扫扫描到所有的自定义注解。然后进行url mapping的处理。
现在问题是我如何通过spring扫描指定package下的所有类,然后判断是不是有该注解进而去做相应的处理。
判断有没有注解什么的我都清楚,现在不清楚的就是如何通过spring来扫描指定package下的所有类,然后用自己的方法处理这些类。
多谢大家

巴扎黑巴扎黑2766 days ago450

reply all(7)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 09:48:44

    http://m.yl1001.com/group_article/3061468659121307.htm?shcd=kadkdundaub

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:48:44

    Configure in springmvc configuration file

    reply
    0
  • 阿神

    阿神2017-04-18 09:48:44

    Already found a way. I am using spring-boot. Write a class that inherits ApplicationListener<ContextRefreshedEvent>
    Then use event.getApplicationContext().getBeansWithAnnotation(TableBind.class); That’s it
    Thank you everyone

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:48:44

    Custom annotations can only be implemented using aop in spring.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:48:44

    <context:component-scan base-package="com.togeek.**.service.imple" />

    reply
    0
  • PHPz

    PHPz2017-04-18 09:48:44

    For spring-boot
    just inject it in the startup class
    For example:
    @ComponentScan(basePackages = "customer.common") //Specify the controller you want to scan here
    @SpringBootApplication
    public class CustomerMain{

    public static void main(String[] args){
        SpringApplication.run(CustomerMain.class, args);
    }

    }

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:48:44

    In fact, the final package and class must be stored and abstracted into files, so traversing classes becomes traversing folders and files.

    There is an example, you can take a look
    http://changhongbao.iteye.com...

    reply
    0
  • Cancelreply