041 Spring Boot中排除功能的處理
阿新 • • 發佈:2018-11-11
這個問題,原本是沒有注意,主要是理解的不夠深刻。
1.先看我的配置檔案
1 package com.springBoot.ioc.config; 2 3 4 import com.springBoot.ioc.controller.StudentController; 5 import org.springframework.context.annotation.ComponentScan; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.FilterType;8 9 /** 10 * @Desciption 配置檔案 11 */ 12 /*@Configuration 13 public class MySpringBootConfig { 14 @Bean(name="student") 15 public Student getStu(){ 16 Student student=new Student(); 17 student.setId(1L); 18 student.setUsername("Tom"); 19 student.setPassword("123456");20 return student; 21 } 22 }*/ 23 @Configuration 24 @ComponentScan(basePackages="com.springBoot.ioc.*",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = StudentController.class)}) 25 public class MySpringBootConfig{ 26 27 }
2.然後執行程式,看報錯資訊
1 9:51:13.797 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]67b92f0a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,mySpringBootConfig]; root of factory hierarchy 2 Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.springBoot.ioc.config.MySpringBootConfig]; nested exception is java.lang.IllegalArgumentException: @ComponentScan ANNOTATION type filter requires an annotation type: class com.springBoot.ioc.controller.StudentController 3 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184) 4 at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316) 5 at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233) 6 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271) 7 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91) 8 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:692) 9 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530) 10 at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88) 11 at com.springBoot.ioc.test.IocTestDemo.main(IocTestDemo.java:17) 12 Caused by: java.lang.IllegalArgumentException: @ComponentScan ANNOTATION type filter requires an annotation type: class com.springBoot.ioc.controller.StudentController 13 at org.springframework.util.Assert.assignableCheckFailed(Assert.java:655) 14 at org.springframework.util.Assert.isAssignable(Assert.java:586) 15 at org.springframework.context.annotation.ComponentScanAnnotationParser.typeFiltersFor(ComponentScanAnnotationParser.java:142) 16 at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:102) 17 at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:288) 18 at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) 19 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202) 20 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170) 21 ... 8 more 22 23 Process finished with exit code 1
3.原因
在excludedFilters中的class屬性指定的是Controller,Service之類的,不是具體的類。
應該是如下的程式:
1 package com.springBoot.ioc.config; 2 3 4 import com.springBoot.ioc.controller.StudentController; 5 import org.springframework.context.annotation.ComponentScan; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.FilterType; 8 import org.springframework.stereotype.Controller; 9 10 /** 11 * @Desciption 配置檔案 12 */ 13 /*@Configuration 14 public class MySpringBootConfig { 15 @Bean(name="student") 16 public Student getStu(){ 17 Student student=new Student(); 18 student.setId(1L); 19 student.setUsername("Tom"); 20 student.setPassword("123456"); 21 return student; 22 } 23 }*/ 24 @Configuration 25 @ComponentScan(basePackages="com.springBoot.ioc.*",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)}) 26 public class MySpringBootConfig{ 27 28 }
4.注意點
導包的時候,也不要出錯。