spring学习2之组件注册-@ComponentScan-自动扫描组件&指定扫描规则

一,配置文件版

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫包 加入此配置后,会自动扫描com。dream下面中带有@Controller,@Service,@Repository,@Component-->
    <!--<context:component-scan base-package="com.dream"></context:component-scan>-->
    <bean id="student" class="com.dream.bean.Student">
        <property name="age" value="18"></property>
        <property name="name" value="lisi"></property>
    </bean>
</beans>

在配置文件中加入<context:component-scan base-package="com.dream"></context:component-scan>,spring容器在启动的时候就会自动扫描com.dream包下面所有带@Controller,@Service,@Repository,@Component注解的类,并加载到容器。

二,注解版

1,在Config类中加入

@ComponentScan(value = "com.dream")
package com.dream.config;

import com.dream.bean.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//配置类(等价原来的配置文件)
@Configuration
@ComponentScan(value = "com.dream")
public class BeansConfig {

    //给容器中注册一个bean;类型为返回值类型,id默认是方法名作为id
    @Bean
    public Student student(){
        return new Student("王五",28);
    }
}

2,添加下面三个类

package com.dream.controller;

import org.springframework.stereotype.Controller;

@Controller
public class BookController {
}
package com.dream.service;

import org.springframework.stereotype.Service;

@Service
public class BookService {
}
package com.dream.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookDao {
}

3,在pom文件加入junit依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

4,测试类

package com.dream;

import com.dream.config.BeansConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IOCTest {

    @Test
    public void test1(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeansConfig.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();

        for(String bean:beanDefinitionNames){
            System.out.println(bean);
        }
    }
}

5,测试结果  红色字段代表我们自己注入容器的类,绿色部分是spring启动时,spring中需要注入的部分

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory

beansConfig
bookController
bookDao
bookService
student

6,测试  excludeFilters,配置类如下

package com.dream.config;

import com.dream.bean.Student;
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;
import org.springframework.stereotype.Service;

//配置类(等价原来的配置文件)
@Configuration
@ComponentScan(value = "com.dream",excludeFilters = {
        @ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class, Service.class})//排除Controller和Service以外的其他需要注入的类
})
public class BeansConfig {

    //给容器中注册一个bean;类型为返回值类型,id默认是方法名作为id
    @Bean
    public Student student(){
        return new Student("王五",28);
    }
}

结果中没有带@Controller和@Service的类

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
beansConfig
bookDao
student

7,测试  includeFilters,配置如下,使用includeFilters ,需要将useDefaultFilters 设置为false才生效

package com.dream.config;

import com.dream.bean.Student;
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;
import org.springframework.stereotype.Service;

//配置类(等价原来的配置文件)
@Configuration
@ComponentScan(value = "com.dream",includeFilters = {
        @ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class, Service.class})//排除Controller和Service以外的其他需要注入的类
},useDefaultFilters = false)
public class BeansConfig {

    //给容器中注册一个bean;类型为返回值类型,id默认是方法名作为id
    @Bean
    public Student student(){
        return new Student("王五",28);
    }
}

测试结果

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
beansConfig
bookController
bookService
student

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dream21st

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值