zoukankan      html  css  js  c++  java
  • spring注解第02课 包扫描@ComponentScan、@ComponentScans

    1.配置文件形式:

      <context:component-scan base-package="com.atguigu" />

      spring会扫描此包下的@Service @Repository  @Component @Autoware @Resource 等注解

    2.注解形式

      在配置文件注解类(@Configuration)上声明@ComponentScans,里面包含多个@ComponentScan,

      或是只声明@ComponentScan

    package com.atguigu.config;
    
    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.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.ComponentScans;
    
    import com.atguigu.bean.Person;
    
    //配置类==配置文件
    @Configuration  //告诉Spring这是一个配置类
    
    @ComponentScans(
            value = {
                    @ComponentScan(value="com.atguigu",includeFilters = {
    /*                        @Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
                            @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/
                            @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class})
                    },useDefaultFilters = false)    
            }
            )
    //@ComponentScan  value:指定要扫描的包
    //excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
    //includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
    //FilterType.ANNOTATION:按照注解
    //FilterType.ASSIGNABLE_TYPE:按照给定的类型;
    //FilterType.ASPECTJ:使用ASPECTJ表达式
    //FilterType.REGEX:使用正则指定
    //FilterType.CUSTOM:使用自定义规则
    public class MainConfig {
        
        //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
        @Bean("person")
        public Person person01(){
            return new Person("lisi", 20);
        }
    
    }

    3.自定义过滤器(用于注解)

    package com.atguigu;
    
    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 MyTypeFilter implements TypeFilter {
    
        public boolean match(MetadataReader metadataReader,
                MetadataReaderFactory metadataReaderFactory) throws IOException {
            //获取当前类的注解信息
            AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
            //获取当前类的类信息
            ClassMetadata classMetadata = metadataReader.getClassMetadata();
            
            System.out.println("------------->" + classMetadata.getClassName());
            //资源信息
            Resource resource = metadataReader.getResource();
            
            if(classMetadata.getClassName().contains("Controller")){
                return true;
            }
            return false;
        }
    
    }
  • 相关阅读:
    When to Partition a Table and an Index
    Hello, world
    提交
    SubmitOncePage:解决刷新页面造成的数据重复提交问题
    压缩ASP.NET中的ViewState
    asp.net千奇百怪的日历
    ICallbackEventHandler实现
    xml數據
    CrystalReports
    [转]Microsoft Visual Studio 2005中使用水晶报表
  • 原文地址:https://www.cnblogs.com/guchunchao/p/9691921.html
Copyright © 2011-2022 走看看