Spring之@ComponentScan自动扫描组件怎么使用

发布时间:2022-06-14 14:58:31 作者:iii
来源:亿速云 阅读:145

这篇文章主要讲解了“Spring之@ComponentScan自动扫描组件怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring之@ComponentScan自动扫描组件怎么使用”吧!

无注解方式component-scan使用

之前,我们需要扫描工程下一些类上所标注的注解,这些常用注解有:

@Controller,@Service,@Component,@Repository

通过在Spring的配置文件中配置<context:component-scan>扫描对应包下扫描这些注解的方式:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	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-2.5.xsd">
    <!--@Controller,@Service,@Component,@Repository-->
	<context:component-scan base-package="com.jektong.spring"/>
</beans>

注解方式@ComponentScan使用

建三个类,依次将

@Controller,@Repository,@Service,标注这些类:

Spring之@ComponentScan自动扫描组件怎么使用

图1

现在通过使用注解@ComponentScan的方式来扫描所在包下面的这些类:之前定义的PersonConfig修改:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.jektong.spring.Person;
@Configuration
@ComponentScan("com.jektong")
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

测试,看是否扫描到这些注解所标注的类:PersonTest.java

@Test
public  void test02() {
	ApplicationContext ac = new AnnotationConfigApplicationContext(PersonConfig.class);
	Person bean = ac.getBean(Person.class);
	System.out.println(bean);
	String[] beanDefinitionNames = ac.getBeanDefinitionNames();
	for (String string : beanDefinitionNames) {
		System.out.println(string);
	}
}

测试效果:除了Spring要自动加载的配置类以外也显示了刚才添加的配置类:

Spring之@ComponentScan自动扫描组件怎么使用

图2

为何会出现PersonConfig,因为@Configuration本 身就是@Component注解的:

Spring之@ComponentScan自动扫描组件怎么使用

图3

@ComponentScan的扫描规则

如果需要指定配置类的扫描规则,@ComponentScan提供对应的扫描方式@Filter进行配置类的过滤:

// 扫描包的时候只规定扫描一些注解配置类。
Filter[] includeFilters() default {};
// 扫描包的时候可以排除一些注解配置类。 
Filter[] excludeFilters() default {};

Filter其实也是一个注解,相当于@ComponentScan的子注解,可以看图4:

Spring之@ComponentScan自动扫描组件怎么使用

图4

Filter对应的过滤规则如下:

第一种:扫描包的时候只规定扫描一些注解配置类【includeFilters】。

使用这个includeFilters过滤规则,必须解除默认的过滤规则,

使用【useDefaultFilters = false】:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",includeFilters  = {
		@Filter(type = FilterType.ANNOTATION,value= {Controller.class})
},useDefaultFilters = false )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

这样就只会扫描用@Controller,标注的配置类交给Spring容器中了:

Spring之@ComponentScan自动扫描组件怎么使用

图5

第二种:扫描包的时候可以排除一些注解配置类【excludeFilters】。

Spring之@ComponentScan自动扫描组件怎么使用

图6

@Filter看上图,有5种不同类型的过滤策略。拿第一种举例,我们需要过滤使用@Controller注解的配置类:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",excludeFilters = {
		@Filter(type = FilterType.ANNOTATION,value= {Controller.class})
} )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

测试看一下发现图2中的personController不会交给Spring容器去管理了:

Spring之@ComponentScan自动扫描组件怎么使用

图7

上面的图6展示出5种不同类型的过滤策略,上面介绍了注解类型(FilterType.ANNOTATION),还有四种:

重点看一下CUSTOM自定义扫描策略。

Spring之@ComponentScan自动扫描组件怎么使用

从源码看,自定义扫描注解类型需要实现TypeFilter接口,下面就写一个实现类MyFilter.java:在实现类中可以自定义配置规则:

package com.jektong.config;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
public class MyFilter implements TypeFilter {
	@Override
	public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
			throws IOException {
		// 查看当前类的注解。
		AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
		// 查看当前扫描类的信息
		ClassMetadata classMetadata = metadataReader.getClassMetadata();
		// 获取当前类资源
		Resource resource = metadataReader.getResource();
		String className = classMetadata.getClassName();
		System.out.println("className===>" + className);
		// 只要类名包含er则注册Spring容器
		if(className.contains("er")) {
			return true;
		}
		return false;
	}
}

测试:

PersonConfig 中进行扫描:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.service.PersonService;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",includeFilters  = {
		@Filter(type = FilterType.CUSTOM,value= {MyFilter.class})
},useDefaultFilters = false )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

可以看出扫描出包下面的类只要带“er”的全部扫描出来,并配置给Spring容器:

Spring之@ComponentScan自动扫描组件怎么使用

ASSIGNABLE_TYPE:按照指定的类型去加载对应配置类:

package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import com.jektong.service.PersonService;
import com.jektong.spring.Person;
@Configuration
@ComponentScan(value = "com.jektong",includeFilters  = {
		@Filter(type = FilterType.ASSIGNABLE_TYPE,value= {PersonService.class})
},useDefaultFilters = false )
public class PersonConfig {
	@Bean("person01")
	public Person person() {
		return new Person("李四",21);
	}
}

尽管我们将PersonService.java上的注解去掉,使用ASSIGNABLE_TYPE依然会加载出来(自行测试)。

ASPECTJ与REGEX基本不用,不用了解。

感谢各位的阅读,以上就是“Spring之@ComponentScan自动扫描组件怎么使用”的内容了,经过本文的学习后,相信大家对Spring之@ComponentScan自动扫描组件怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. spring中怎么自动注入组件
  2. Spring Framework 组件注册 之 FactoryBean

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring @componentscan

上一篇:C语言如何实现电子秒表

下一篇:Go语言开发编程规范命令风格代码格式是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》