spring组件---2

@ComponentScan

以前写法:
person类不变
配置文件中添加扫描标签:

<context:component-scan base-package="com.atguigu"></context:component-scan>
<?xml version="1.0" encoding="UTF-8"?>

<context:component-scan base-package="com.atguigu"></context:component-scan>
<bean id="person" class="com.atguigu.bean.Person">
    <property name="age" value="21"></property>
    <property name="name" value="xm"></property>
</bean>

注解实现包扫描:
person类不变

在配置类上添加@Componnent注解,在value中指定要扫描的包:@ComponentScan(value = “com.atguigu”)
@Configuration //告诉spring这是一个配置类
@ComponentScan(value = “com.atguigu”)
public class MainConfig {
@Bean(“person”) //给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
public Person person01(){
return new Person(“li”,20);
}
}

增加几个组件;
建立三个包,在里面添加三个类,再分别加上三个注解

第一个: controller包:BookController类,@Controller注解
package com.atguigu.controller;
@Controller
public class BookController {

}

第二个: service包,BookService类,@Service注解
package com.atguigu.service;
@Service
public class BookService {
}

第三个: dao包,BookDao类,@Reposity注解
package com.atguigu.dao;
@Repository
public class BookDao {
}

编写一个测试类:先指定配置类所在的位置,将其加入到容器中,再去获取这些组件的名称
public class IOCTest {
@Test
public void IOCTest01(){
//指定配置类所在的位置,返回ioc容器
AnnotationConfigApplicationContext applicationContext= new AnnotationConfigApplicationContext(MainConfig.class);
//查看该容器中有哪些bean
String[] definitionNames=applicationContext.getBeanDefinitionNames();
for(String name:definitionNames){
System.out.println(name);
}
}
}

结果:
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
mainConfig
bookController
bookDao
bookService
person

排除要扫描的包和只扫描某些包:通过@ComponentScan注解的excludeFilters属性,includeFilter实现

先指定按照什么规则排除—按照类型排除/按照注解排除,再指明要排除的类
@ComponentScan(value = “com.atguigu”,excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
})

@Configuration //告诉spring这是一个配置类
@ComponentScan(value = “com.atguigu”,excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
})
public class MainConfig {
@Bean(“person”) //给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
public Person person01(){
return new Person(“li”,20);
}
}

包扫描时排除了controller注解,service注解,测试时,打印容器中的组件名称,则不会出现这两个组件的名称
测试方法不变
测试结果:
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
mainConfig
bookDao
person

配置扫描时只扫描哪些组件:includeFilters———只扫描controller组件,service组件
@ComponentScan(value = “com.atguigu”,includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
})

注意:这样写不对的

原来用xml配置扫描包,要指定只扫描某些组件时,需要禁用默认的过滤规则:默认的过滤规则时扫描所有
<context:component-scan base-package=“com.atguigu” use-default-filters=“false”></context:component-scan>

注解方法:
@ComponentScan(value = “com.atguigu”,includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
},useDefaultFilters = false)

测试方法不变
测试结果: ——只包含controller,service组件 (spring中的四个基本注解)
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
mainConfig
bookController
bookService
person

指定多个@ComponentScan过滤规则——@ComponentScans
@ComponentScans(value = {
@ComponentScan(value = “com.atguigu”,includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
},useDefaultFilters = false)
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值