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

news/2024/7/8 3:08:31

一,配置文件版

<?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

 


http://www.niftyadmin.cn/n/1639683.html

相关文章

java从数据库读取菜单递归生成菜单树

首先看一下菜单的样子根据这个样子我们定义菜单类 public class Menu {// 菜单idprivate String id;// 菜单名称private String name;// 父菜单idprivate String parentId;// 菜单urlprivate String url;// 菜单图标private String icon;// 菜单顺序private int order;// 子菜单…

最大熵图像分割matlab,两个matlab实现最大熵法图像分割程序

%两个程序&#xff0c;亲测可用clear allaimread(moon.tif);figure,imshow(a)countimhist(a);[m,n]size(a);Nm*n;L256;countcount/N;%%每一个像素的分布概率countfor i1:Lif count(i)~0sti-1;break;endendstfor iL:-1:1if count(i)~0ndi-1;break;endendndfcount(st1:nd1); %f是…

Mysql8在Centos7.2上面安装

1&#xff0c;cd /usr/local/software 这个目录下面 2&#xff0c;wget -i -c https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm 下载mysql8.0的rpm 3&#xff0c;先安装 yum install -y mysql80-community-release-el7-1.noarch.rpm&#xff…

python php class 类,PHP如何自定义类Class的示例代码(OOP面向对象教程)

PHP如何自定义类Class的示例代码(OOP面向对象教程)分类&#xff1a;PHP_Python| 发布&#xff1a;佚名| 查看&#xff1a; | 发表时间&#xff1a;2014/9/16那怎么开始设计一个合格的类呢&#xff0c;一开始就写class{}的都错了&#xff0c;正确的是什么都不写&#xff0c;而是…

centos7.2安装nginx

1&#xff0c;先执行 yum -y install gcc gcc-c autoconf pcre pcre-devel make automake 2&#xff0c;vim /etc/yum.repos.d/nginx.repo 在下面添加 [nginx] namenginx repo baseurlhttp://nginx.org/packages/centos/7/$basearch/ gpgcheck0 enabled1 3&#xff0…

php关于钱这类怎么处理,PHP-MYSQL怎么实现买东西扣钱流程(细节问题一大堆)?...

假设用户花费金钱余额兑换1个‘糖’这种虚拟商品&#xff0c;金钱余额和用户拥有‘糖’的个数分别记录在不同的表上&#xff0c;那么我们要做的是&#xff0c;扣除用户金钱的同时&#xff0c;在用户的虚拟物品表中‘糖’的数量1。假设任何用户糖数上限为99先说下目前小弟的思路…

UDT协议详解

基于UDP的数据传输协议&#xff08;UDP-based Data Transfer Protocol&#xff0c;简称UDT&#xff09;是一种互联网数据传输协议。UDT的主要目的是支持高速广域网上的海量数据传输&#xff0c;而互联网上的标准数据传输协议TCP在高带宽长距离网络上性能很差。 顾名思义&#x…

php里的stdclass,PHP中的stdClass是什么?

stdClass是另一个很好的PHP特性。您可以创建一个匿名PHP类。让我们检查一个例子。$pagenew stdClass();$page->nameHome;$page->status1;现在&#xff0c;假设您有另一个类&#xff0c;它将使用页面对象初始化并在其基础上执行。<?phpclass PageShow {public $curre…