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

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

回复
阅读 21.2k
7 个回答

在springmvc的配置文件中配

已经找到方法了。我用的是spring-boot。写一个类继承ApplicationListener<ContextRefreshedEvent>
然后用event.getApplicationContext().getBeansWithAnnotation(TableBind.class);即可
多谢大家

自定义注解在spring里面只能用aop实现。

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

spring-boot 的话
在启动类中 注进去就好
例如:
@ComponentScan(basePackages = "customer.common") //这里指定你要扫描的 controller
@SpringBootApplication
public class CustomerMain{

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

}

新手上路,请多包涵

其实最终的package 和类 都是要存储抽象成文件的,所以遍历类就变成了遍历文件夹及文件。

有个例子,可以看看
http://changhongbao.iteye.com...

推荐问题
宣传栏