zoukankan      html  css  js  c++  java
  • spring装配注解(IOC容器加载控制)ComponentScan及ComponentScans使用

      ComponentScan,只写入value,可扫描路径下装配的@Contrller、@Service、@Repository

    1 @ComponentScan(value = "com.test")

    测试类:

     1 @Test
     2     @SuppressWarnings("resource")
     3     public void test01() {
     4         AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
     5                 MainConfig.class);
     6 
     7         String[] definitionNames = applicationContext.getBeanDefinitionNames();//获取spring装配的bean
     8 
     9         for (String name : definitionNames) {
    10             System.out.println(name);
    11         }
    12     }

    ComponentScan中还有参数excludeFilters、includeFilters、useDefaultFilters

    ANNOTATION, // 按照注解 比如Controller Service
    ASSIGNABLE_TYPE,// 按照给定的类型 比如某个类 like this

    CUSTOM,//自定义验证

    其中excludeFilters是设置屏蔽,excludeFilters可通过设置type进行过滤,classes进行详细的排除。其中使用方法如下:

    1 @ComponentScan(value = "com.test", excludeFilters = 
    2 { @Filter(type = FilterType.ASSIGNABLE_TYPE ,
    3         classes={BookService.class,Book.class}) })

    通过注解类型屏蔽BookService及Book类

    1 @ComponentScan(value = "com.test", excludeFilters = 
    2 { @Filter(type = FilterType.ASSIGNABLE_TYPE ,
    3         classes={BookService.class,Book.class}) })

     通过注解类型屏蔽service,controller

    1 @ComponentScan(value = "com.test", excludeFilters = 
    2 { @Filter(type = FilterType.ANNOTATION ,
    3         classes={Controller.class,Service.class}) })

    而includeFilters是设置非屏蔽,只装配自己设定的类,要注意的是,在使用是要将useDefaultFilters关闭,默认时开启的。

    只装配配置了service,controller的类

    1 @ComponentScan(value = "com.test", useDefaultFilters=false,includeFilters = 
    2 { @Filter(type = FilterType.ANNOTATION ,
    3         classes={Controller.class,Service.class}) })

     ComponentScans的使用就更明了,里面放的是多个ComponentScan,直接放进去就可以直接使用

    1 @ComponentScans(
    2 value={
    3         @ComponentScan(value = "com.test", useDefaultFilters=false,includeFilters = 
    4             { @Filter(type = FilterType.ANNOTATION ,
    5                     classes={Controller.class}) })
    6 }
    7         )

     CUSTOM自定义验证,需要我们写一个类,去实现TypeFilter的方法,下面测试类,对className中包含了“ser”的允许装配进容器

     1 public class MyTypeFilter implements TypeFilter {
     2 
     3     public boolean match(MetadataReader metadataReader,
     4             MetadataReaderFactory metadataReaderFactory) throws IOException {
     5         // TODO Auto-generated method stub
     6 //        获取当前类注解的信息
     7         AnnotationMetadata annotationMetadata=metadataReader.getAnnotationMetadata();
     8         //获取当前正在扫描的类信息
     9         ClassMetadata classMetadata=metadataReader.getClassMetadata();
    10         //获取当前类资源(类的路径)
    11         Resource resource=metadataReader.getResource();
    12         
    13         String className=classMetadata.getClassName();
    14         
    15         System.out.println("--->"+className);
    16         if(className.contains("ser")){
    17             return true;
    18         }
    19         
    20         return false;
    21     }
    22 
    23 }
    1 @ComponentScans(value={@ComponentScan
    2         (value = "com.test", useDefaultFilters=false,
    3         includeFilters = {@Filter(type=FilterType.CUSTOM ,classes={MyTypeFilter.class})}
    4         )}
    5         )
  • 相关阅读:
    When You Get Troubles
    CentOS 6.8升级到7+
    Tomcat服务器搭建
    Percona Server 安装
    VirtualBox中如何使虚拟机能够上网?
    CentOS
    xen安装
    SSH免密码设置
    打造绿色版的RobotFramework
    零散知识记录-Jira的安装
  • 原文地址:https://www.cnblogs.com/locker777/p/10313500.html
Copyright © 2011-2022 走看看