基于配置文件方式的 包扫描配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 包扫描、只要标注了@Controller、@Service、@Component、@Repository -->
<context:component-scan base-package="com.tl"></context:component-scan>
</beans>
那么基于注解方式的包扫描,怎么配置呢?跟我向下看
@Configuration
@ComponentScan(value="com.tl", includeFilters = {
@Filter(type=FilterType.ANNOTATION, classes = {Controller.class, Service.class})
}, useDefaultFilters = false)
//@ComponentScan value:指定要扫描的包
//excludeFilters = Filter[]:指定扫描的时候按照什么规则排除哪些组件
//includeFilters = Filter[]: 指定扫描的时候只包含哪些组件
//useDefaultFilters = false:禁用掉默认的扫描范围(默认是全范围扫描)
//@ComponentScans:可以指定多个扫描策略
public class MainConfig {
//给容器中注册一个Bean;类型为返回值的类型,id默认用方法名作为id
@Bean(value = "person")
public Person person01() {
return new Person("23", "李四");
}
}