spring 之 自动扫描

环境搭建
新建一个maven项目
在这里插入图片描述
在pom.xml中引入响应的依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.yu</groupId>
  <artifactId>spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <properties>
        <!--可以在spring官网看最新版本 https://spring.io/projects/spring-framework#learn-->
        <!-- 用最新的spring5  201910521:21:39最新版 -->
        <spring.version>5.2.0.RELEASE</spring.version>
    </properties>
    
  <dependencies>
  		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-context</artifactId>
		    <version>5.0.16.RELEASE</version>
		</dependency>
        <!--spring 核心包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring web包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- springMVC-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>

        <!--jstl for jsp page-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        
        <dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.12</version>
		    <scope>test</scope>
		</dependency>
    </dependencies>
  
</project>

@Component – 指示自动扫描组件。
@Repository – 表示在持久层DAO组件。
@Service – 表示在业务层服务组件。
@Controller – 表示在表示层控制器组件。

自动扫码在xml的配置

<?xml version="1.0" encoding="UTF-8" ?>
<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
         
     <!-- 自动扫描会自动去扫描@Component @Repository @Service @Controller -->
	 <context:component-scan base-package="com.*" /> 
     <!-- bean的配置 -->
    <bean id="posnse" class="com.bean.Ponse">
        <property name="name" value="jion"></property>
    </bean>
</beans>

使用注解的方式
在配置类上添加上@ComponentScan注解
在添加几个类dao、control、server

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.bean.Ponse;

@Configuration   //告诉spring这是一个配置类
//注意这里没有value就只能扫描当前类
@ComponentScan(value="com.*") 
public class MianConfig {
	@Bean("ponse")   //也方法名来作为bean的id
	public Ponse getPonse() {
		// TODO Auto-generated method stub
		return new Ponse("你好",12);
	}
}

测试类

package com;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.config.MianConfig;

public class DemoTest {
	@Test
	public void Test01() {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MianConfig.class);
		
		String[] naStrings = applicationContext.getBeanDefinitionNames();
		for (String string : naStrings) {
			System.out.println(string);
		}
	}
}

测试结果
在这里插入图片描述
excludeFilters是一个filter数组 ,并且是一个排除类型组件的扫描

/**
	 * Specifies which types are not eligible for component scanning.
	 * @see #resourcePattern
	 */
	Filter[] excludeFilters() default {};
package com.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 org.springframework.stereotype.Repository;

import com.bean.Ponse;

@Configuration   //告诉spring这是一个配置类
/**
 * excludeFilters是一个Filter类型的数组 ,不包含组件的扫描
 * 
 *
 */
@ComponentScan(value="com.*",excludeFilters={
		@Filter(type=FilterType.ANNOTATION,classes={
				Repository.class,Controller.class	
		})
})
public class MianConfig {
	@Bean("ponse")   //也方法名来作为bean的id
	public Ponse getPonse() {
		// TODO Auto-generated method stub
		return new Ponse("你好",12);
	}
}

结果排除了dao和controller
在这里插入图片描述
includeFilters是一个filter数组 ,并且是一个包含类型组件的扫描

/**
	 * Indicates whether automatic detection of classes annotated with {@code @Component}
	 * {@code @Repository}, {@code @Service}, or {@code @Controller} should be enabled.
	 */
	boolean useDefaultFilters() default true;

	/**
	 * Specifies which types are eligible for component scanning.
	 * <p>Further narrows the set of candidate components from everything in {@link #basePackages}
	 * to everything in the base packages that matches the given filter or filters.
	 * <p>Note that these filters will be applied in addition to the default filters, if specified.
	 * Any type under the specified base packages which matches a given filter will be included,
	 * even if it does not match the default filters (i.e. is not annotated with {@code @Component}).
	 * @see #resourcePattern()
	 * @see #useDefaultFilters()
	 */
	Filter[] includeFilters() default {};
package com.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 org.springframework.stereotype.Repository;

import com.bean.Ponse;


@Configuration   //告诉spring这是一个配置类
/**
 * excludeFilters是一个Filter类型的数组 ,不包含组件的扫描
 * includeFilters是一个filter数组 ,并且是一个包含类型组件的扫描
 *
 */
@ComponentScan(value="com.*",includeFilters={
		@Filter(type=FilterType.ANNOTATION,classes={
				Repository.class,Controller.class	
		})
},useDefaultFilters = false)
public class MianConfig {
	@Bean("ponse")   //也方法名来作为bean的id
	public Ponse getPonse() {
		// TODO Auto-generated method stub
		return new Ponse("你好",12);
	}
}

在这里插入图片描述
其中的FilterType有如下类型

public enum FilterType {

	/**
	 * Filter candidates marked with a given annotation.
	 * 用给定注释标记的筛选候选。
	 * @see org.springframework.core.type.filter.AnnotationTypeFilter
	 */
	ANNOTATION,

	/**
	 * Filter candidates assignable to a given type.
	 * 可分配给给定类型的筛选候选对象。
	 * @see org.springframework.core.type.filter.AssignableTypeFilter
	 */
	ASSIGNABLE_TYPE,

	/**
	 * Filter candidates matching a given AspectJ type pattern expression.
	 * 筛选与给定AspectJ类型模式表达式匹配的候选项。
	 * @see org.springframework.core.type.filter.AspectJTypeFilter
	 */
	ASPECTJ,

	/**
	 * Filter candidates matching a given regex pattern.
	 * 筛选与给定正则表达式模式匹配的候选项。
	 * @see org.springframework.core.type.filter.RegexPatternTypeFilter
	 */
	REGEX,

	/** Filter candidates using a given custom
	使用给定的自定义筛选候选
	 * {@link org.springframework.core.type.filter.TypeFilter} implementation.
	 */
	CUSTOM

总结:
注解中的自动扫描用@ComponentScan
过滤组件有两种方式
一、excludeFilters 返回一个 Filters[] 数组 ,扫描时是不包含组件
二、includeFilters 返回一个 Filters[] 数组, 扫描时是包含组件

注意:在用includeFilters时,要将useDefaultFilters改为 false,默认是true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值