这个问题,原本是没有注意,主要是理解的不够深刻。
1.先看我的配置文件
package com.springBoot.ioc.config; import com.springBoot.ioc.controller.StudentController;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType; /**
* @Desciption 配置文件
*/
/*@Configuration
public class MySpringBootConfig {
@Bean(name="student")
public Student getStu(){
Student student=new Student();
student.setId(1L);
student.setUsername("Tom");
student.setPassword("123456");
return student;
}
}*/
@Configuration
@ComponentScan(basePackages="com.springBoot.ioc.*",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = StudentController.class)})
public class MySpringBootConfig{ }
2.然后运行程序,看报错信息
9:51:13.797 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@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
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
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:184)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:316)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:271)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:91)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:692)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
at com.springBoot.ioc.test.IocTestDemo.main(IocTestDemo.java:17)
Caused by: java.lang.IllegalArgumentException: @ComponentScan ANNOTATION type filter requires an annotation type: class com.springBoot.ioc.controller.StudentController
at org.springframework.util.Assert.assignableCheckFailed(Assert.java:655)
at org.springframework.util.Assert.isAssignable(Assert.java:586)
at org.springframework.context.annotation.ComponentScanAnnotationParser.typeFiltersFor(ComponentScanAnnotationParser.java:142)
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:102)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:288)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170)
... 8 more Process finished with exit code 1
3.原因
在excludedFilters中的class属性指定的是Controller,Service之类的,不是具体的类。
应该是如下的程序:
package com.springBoot.ioc.config; import com.springBoot.ioc.controller.StudentController;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller; /**
* @Desciption 配置文件
*/
/*@Configuration
public class MySpringBootConfig {
@Bean(name="student")
public Student getStu(){
Student student=new Student();
student.setId(1L);
student.setUsername("Tom");
student.setPassword("123456");
return student;
}
}*/
@Configuration
@ComponentScan(basePackages="com.springBoot.ioc.*",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class MySpringBootConfig{ }
4.注意点
导包的时候,也不要出错。