spring使用xml文件来获取初始扫描的bean:
添加了maven依赖,在maven compile时成功,可以获取依赖项中的类名,但是当使用@Autowired注入时报错,找不到bean,此时要在spring-context.xml文件中添加spring要扫描的其他项目的包。添加以下:
<context:component-scan base-package="com.yourclass.package.is"/>
如果是springboot使用注解来获取初始扫描的bean方式:
1. springbootapplication 这个注解默认扫描springboot启动类所在的包及其子包,需要先将不用的包排除掉,此处排除的是jdbc下连接数据库的包。
@SpringBootApplication(scanBasePackages = {要扫描的包路径},
exclude = {DataSourceAutoConfiguration.class})
2.如果“要扫描的包路径”中有需要排除掉的bean。用@ComponentScan注解扫描
@ComponentScan(basePackages = {要扫描的包路径},
excludeFilters = {
@ComponentScam.Filter(type = FilterType.ASSIGNABLE_TYPE, classes= {要排除的bean名.class}),
@ComponentScam.Filter(type = FilterType.REGEX, pattern = "要排除的路径")})
springboot的自动装配的包中不用的照常使用@springbootApplication注解排除。