无注解方式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,标注这些类:

图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要自动加载的配置类以外也显示了刚才添加的配置类:

图2

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

图3

@ComponentScan的扫描规则

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

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

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

图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容器中了:

图5

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

图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容器去管理了:

图7

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

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

从源码看,自定义扫描注解类型需要实现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容器:

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基本不用,不用了解。

以上就是@ComponentScan的具体用法,该兴趣的话可以看一下源码。

到此这篇关于详解Spring系列之@ComponentScan自动扫描组件的文章就介绍到这了,更多相关Spring @ComponentScan内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

详解Spring系列之@ComponentScan自动扫描组件的更多相关文章

  1. Spring JdbcTemplate执行数据库操作详解

    JdbcTemplate是Spring框架自带的对JDBC操作的封装,目的是提供统一的模板方法使对数据库的操作更加方便、友好,效率也不错,这篇文章主要介绍了Spring JdbcTemplate执行数据库操作,需要的朋友可以参考下

  2. Spring Batch批处理框架操作指南

    Spring Batch 是 Spring 提供的一个数据处理框架。企业域中的许多应用程序需要批量处理才能在关键任务环境中执行业务操作,这篇文章主要介绍了Spring Batch批处理框架操作指南,需要的朋友可以参考下

  3. Spring详细讲解@Autowired注解

    @Autowired注解可以用在类属性,构造函数,setter方法和函数参数上,该注解可以准确地控制bean在何处如何自动装配的过程。在默认情况下,该注解是类型驱动的注入

  4. 使用Spring AOP实现用户操作日志功能

    这篇文章主要介绍了使用Spring AOP实现了用户操作日志功能,功能实现需要一张记录日志的log表,结合示例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. Spring Security认证器实现过程详解

    一些权限框架一般都包含认证器和决策器,前者处理登陆验证,后者处理访问资源的控制,这篇文章主要介绍了Spring Security认证器实现过程,需要的朋友可以参考下

  6. spring学习JdbcTemplate数据库事务管理

    这篇文章主要为大家介绍了spring学习JdbcTemplate数据库事务管理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  7. Spring Boot 集成Redisson实现分布式锁详细案例

    这篇文章主要介绍了Spring Boot 集成Redisson实现分布式锁详细案例,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下

  8. Spring Security实现接口放通的方法详解

    在用Spring Security项目开发中,有时候需要放通某一个接口时,我们需要在配置中把接口地址配置上,这样做有时候显得麻烦。本文将通过一个注解的方式快速实现接口放通,感兴趣的可以了解一下

  9. 如何利用Spring把元素解析成BeanDefinition对象

    这篇文章主要介绍了如何利用Spring把元素解析成BeanDefinition对象,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

  10. Spring解决循环依赖问题及三级缓存的作用

    这篇文章主要介绍了Spring解决循环依赖问题及三级缓存的作用,所谓的三级缓存只是三个可以当作是全局变量的Map,Spring的源码中大量使用了这种先将数据放入容器中等使用结束再销毁的代码风格

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部