1废话不多说,先新建一个Maven项目(不需要导入包进去在添加依赖就好)
1.1.然后导入其中的依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
1.在Java文件下创建一个Com包,写一个Person类
package com.Bean; public class Peson { private String name; private int age; public Peson(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Bean{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
2.写一个Controller类,加上注解Controller
package com.Controller; import org.springframework.stereotype.Controller; @Controller public class controller { }
3.写一个dao类,加上注解Repository
package com.Dao; import org.springframework.stereotype.Repository; @Repository public class dao { }
4.在写一个Service,加上注解Service
package com.Service; import org.springframework.stereotype.Service; @Service public class service { }
5.最后写一个Config类,加上Config注解和CompanScan注解
import com.Bean.Peson; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; @Configuration @ComponentScan(value = "com") public class config { @Bean public Peson peson(){ return new Peson("kangkang",20); } }
6.创建一个测试类
import com.Config.config; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestP { @Test public void test01(){ AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(config.class); String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames(); for (String name:beanDefinitionNames) { System.out.println(name); } } }
7.运行后
2.1 我们可以看到这里面管理了所有的Bean
这个时候开始我们利用@ComponentScan Filter的规则来进行操作
让我们先来了解一下@Filter一共有几种类型
@FilterType.AnnoTatition 使用注解表达式(最常用)
@FilterType.ASSIGNABLE_TYPE 使用ASPECTJ表达式
@FilterType.REGEX 正则表达式
@FilterType.Custom 自定义规则
componentscan value:指定扫描的包
@ComponentScan(value = "com",excludeFilters = { @ComponentScan.Filter(type=FilterType.ANNOTATION,classes = {Controller.class}) })
2.2ExcludeFilters Filter(排除里面指定的Bean)
利用@Filter的排除规则,用type指定类型用注解排除(ANNOTATION),指定怕排除Controller.class,运行后
发现在管理Bean中里面少了controller的
2.3IncludeFilters Filter(指定里面的Bean)
利用@Filter的排除规则,用type指定类型用注解排除(ANNOTATION),指定怕排除Controller.class,运行后
(注意,一定要先让ComponentScan里的useDefaultFilters方法改成false)
@ComponentScan(value = "com",includeFilters = { @ComponentScan.Filter(type=FilterType.ANNOTATION,classes = {Controller.class}) },useDefaultFilters = false)
2.4平常我用最多的都是用ANNOTATION,按照注解类型,还有一种也是我们需要用到的就是我们的自定义方法custom(按照自定义规则)
2.4.1首先在Config下创建一个类MyFilter,继承TypeFilter,实现其方法
import org.springframework.core.type.filter.TypeFilter; import java.io.IOException; public class MyFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return false; } }
2.4.2编辑方法
metadataReader 读取的是当前扫描类的的信息
metadataReaderFactory 读取包外扫描的信息
2.4.3自定义方法,扫描当前类信息,把包含dao字段的留下,其他排除
package com.Config; 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; import java.io.IOException; 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); if(className.contains("dao")){ return true; } return false; } }
2.4.4修改@Filter类型为Custom
@ComponentScan(value = "com",includeFilters = { @ComponentScan.Filter(type=FilterType.CUSTOM,classes = {MyFilter.class}) },useDefaultFilters = false)
2.4.5 运行得
最后注意,Config和Person是从开始就一直包含在这Bean里
因为Config类上加有注解@Configuration,Perosn是用@Bean定义的,所以一开始就包含在里面,不管你用什么方法,它们都会存在
版权声明:本文为weixin_59015876原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。