Spring注解驱动开发——组件注册 @ComponentScan

2021/7/14 23:05:29

本文主要是介绍Spring注解驱动开发——组件注册 @ComponentScan,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1 之前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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
 <!-- 包扫描、只要标注了@Controller、@Service、@Repository,@Component 这些组件,都会被扫描加进容器中 -->
<context:component-scan base-package="com.mangoubiubiu"></context:component-scan>
    <bean id="person" class="com.mongoubiubiu.bean.Person" >
       <property name="age" value="19"> </property>
       <property name="name" value="丽水"></property>
    </bean>


</beans>

2 注解版

一  Value

@ComponentScan(value="com.mongoubiubiu") 

Valve:指定要扫描的包

 a. 在conf 配置类下指定扫描规则

package com.mongoubiubiu.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.mongoubiubiu.bean.Black;
import com.mongoubiubiu.bean.ColorFactoryBean;
import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.bean.Red;
import com.mongoubiubiu.bean.Yellow;
import com.mongoubiubiu.condition.MyInportSelect;
import com.mongoubiubiu.dao.UserDao;
import com.mongoubiubiu.service.UserService;

//配置类==配置文件
@Configuration  //告诉spring 这是一个配置类
@ComponentScan(value="com.mongoubiubiu")
public class MyConf {
    
    //给容器中注册一个bean; 类型为返回值的类型 id默认是用方法名做为id
    @Bean("xixi")
    public Person person222(){
        return new Person("王五", 19);
    }
    
    @Bean
    public ColorFactoryBean colorFactoryBean(){
        return new ColorFactoryBean();
    }
    
}

b. 新增Controller Service Dao 类 并标注相关组件(@Controller、@Service、@Repository,@Component )

package com.mongoubiubiu.service.serviceImpl;

import org.springframework.stereotype.Service;

import com.mongoubiubiu.service.UserService;

@Service
public class UserServiceImpl implements UserService {

}
package com.mongoubiubiu.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {

    
}
package com.mongoubiubiu.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDao {

}

C. Junit测试

package com.mongougbiubiu.test;

import java.util.Map;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.conf.MainConfigOfLifeCycle;
import com.mongoubiubiu.conf.MyConf;
import com.mongoubiubiu.conf.MyConf2;


public class IocTest {
    ApplicationContext applica=    new     AnnotationConfigApplicationContext(MyConf.class);

    @Test
    public void test01(){
        //获得容器中所有bean定义的名字
        String [] af=applica.getBeanDefinitionNames();
        System.out.println("==========start=========");
        for (String string : af) {
            System.out.println(string);
        }        
        System.out.println("==========end=========");

    }
}

发现确实被注入进来了

 

 

 

二  excludeFilters

excludeFilters指定扫描的时候排除哪些组件
@ComponentScan(value="com.mongoubiubiu",excludeFilters={
//除了@Controller @Service标注的 其他都注入到容器中
        @Filter(type=FilterType.ANNOTATION,classes={Controller.class,Service.class})    
})
 type=FilterType.ANNOTATION 要排除的类型   classes=Controller.class 要排除哪个注解

 a. 同样指定扫描规则:排除@Controller @Service 标注的类, 不注入到ioc容器中。

package com.mongoubiubiu.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.mongoubiubiu.bean.Black;
import com.mongoubiubiu.bean.ColorFactoryBean;
import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.bean.Red;
import com.mongoubiubiu.bean.Yellow;
import com.mongoubiubiu.condition.MyInportSelect;
import com.mongoubiubiu.dao.UserDao;
import com.mongoubiubiu.service.UserService;

//配置类==配置文件
@Configuration  //告诉spring 这是一个配置类
//excludeFilters指定扫描的时候排除哪些组件
@ComponentScan(value="com.mongoubiubiu",excludeFilters={
        //type=FilterType.ANNOTATION 要排除的类型   classes=Controller.class 要排除哪个注解
        //除了controller 标注的 其他都注入到容器中
        @Filter(type=FilterType.ANNOTATION,classes={Controller.class,Service.class})    
        
}
public class MyConf {
    
    //给容器中注册一个bean; 类型为返回值的类型 id默认是用方法名做为id
    @Bean("xixi")
    public Person person222(){
        return new Person("王五", 19);
    }
    
    @Bean
    public ColorFactoryBean colorFactoryBean(){
        return new ColorFactoryBean();
    }
    
}

B. Junit测试

 

 

三  includeFilters

includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件

 

@ComponentScan(value="com.mongoubiubiu",includeFilters={
        //type=FilterType.ANNOTATION 要包含的类型   classes=Controller.class 要包含哪个注解
        @Filter(type=FilterType.ANNOTATION,classes={Controller.class,Service.class})    
        
},useDefaultFilters=false)

 

useDefaultFilters=false:禁用包扫描默认规则(扫描所有)等同于xml标签中的  use-default-filters="false"

 a. 同样指定扫描规则:只有@Controller @Service 标注的类, 注入到ioc容器中 @Repository 不注入。

package com.mongoubiubiu.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.mongoubiubiu.bean.Black;
import com.mongoubiubiu.bean.ColorFactoryBean;
import com.mongoubiubiu.bean.Person;
import com.mongoubiubiu.bean.Red;
import com.mongoubiubiu.bean.Yellow;
import com.mongoubiubiu.condition.MyInportSelect;
import com.mongoubiubiu.dao.UserDao;
import com.mongoubiubiu.service.UserService;

//配置类==配置文件
@Configuration  //告诉spring 这是一个配置类
@ComponentScan(value="com.mongoubiubiu",includeFilters={
        //type=FilterType.ANNOTATION 要排除的类型   classes=Controller.class 要排除哪个注解
        //除了controller 标注的 其他都注入到容器中
        @Filter(type=FilterType.ANNOTATION,classes={Controller.class,Service.class})    
        
},useDefaultFilters=false)
public class MyConf {
    
    //给容器中注册一个bean; 类型为返回值的类型 id默认是用方法名做为id
    @Bean("xixi")
    public Person person222(){
        return new Person("王五", 19);
    }
    
    @Bean
    public ColorFactoryBean colorFactoryBean(){
        return new ColorFactoryBean();
    }
    
}

B. Junit测试



这篇关于Spring注解驱动开发——组件注册 @ComponentScan的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程