Spring : @ComponentScan註解

1.美圖

在這裏插入圖片描述

2. 源碼

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) // 可以添加在類上
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {

    /**
     * scan對應的包路徑(可以指定多個)
     */
    @AliasFor("basePackages")
    String[] value() default {};
    @AliasFor("value")
    String[] basePackages() default {};

    /**
     * 可以指定多個類或接口的class,掃描時會 在這些指定的類和接口所屬的包進行掃面。
     */
    Class<?>[] basePackageClasses() default {};

    /**
     * 通過BeanNameGenerator來得到bean對應的名字,進而得到對應的Class
     */
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

    /**
     * 處理檢測到的bean的scope範圍
     */
    Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;

    /**
     * 是否爲檢測到的組件生成代理
     */
    ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;

    /**
     * 控制符合組件檢測條件的類文件   默認是包掃描下的(包括子包) *.class
     */
    String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN;

    /**
     * 是否對帶有@Component @Repository @Service @Controller註解的類開啓檢測,默認是開啓的
     */
    boolean useDefaultFilters() default true;

    /**
     * 指定某些定義Filter滿足條件的組件 FilterType有5種類型如:
     *  ANNOTATION, 註解類型 默認
     *  ASSIGNABLE_TYPE,指定固定類
     *  ASPECTJ, ASPECTJ類型
     *  REGEX,正則表達式
     *  CUSTOM,自定義類型
     *
     */
    Filter[] includeFilters() default {};

    /**
     * 排除某些過來器掃描到的類
     */
    Filter[] excludeFilters() default {};

    /**
     * 掃描到的類是都開啓懶加載 ,默認是不開啓的
     */
    boolean lazyInit() default false;

}

3.xml配置

實體類

package com.spring.bean;
 
@Data
public class Person {
    private String name;
    private Integer age;
}

xml配置

<!-- 包掃描、只要標註了@Controller、@Service、@Repository,@Component
          use-default-filters="false":關閉默認掃描規則
	 -->
	<context:component-scan base-package="com.spring"></context:component-scan>
	<!-- 初始化person -->
	<bean id="person" class="com.spring.bean.Person"  scope="prototype" >
		<property name="age" value="19"></property>
		<property name="name" value="zhangsan"></property>
	</bean>

4.註解

//配置類==配置文件
@Configuration  //告訴Spring這是一個配置類
@ComponentScan(value = "com.spring")
public class BeanConfig {
 
	//給容器中註冊一個Bean;類型爲返回值的類型,id默認是用方法名作爲id
	//initMethod 初始化方法
	//destroyMethod 銷燬方法
	@Bean(name = "Myperson",initMethod = "personInitMethod",destroyMethod = "personDestroyMethod")
	public Person Myperson(){
		return new Person("zhw", 20);
	}
}

測試類

 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        Person bean1 = applicationContext.getBean(Person.class);
        System.out.println(bean1);
 
        String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name : namesForType) {
            System.out.println("beanId------"+name);
        }
 
        System.out.println("容器中的組件");
 
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章