欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring注解之@Configuration、@Bean、@ComponentScan

程序员文章站 2022-05-21 22:38:25
...

基础知识:基于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"
       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">
    <!--配置包扫描,扫描如@Controller,@Service,@Repository 等注解,会加入容器中-->
    <context:component-scan base-package="com.hx"/>
    <bean id="person" class="com.hx.bean.Person">
        <property name="id" value="18"/>
        <property name="name" value="黄俊伟"/>
    </bean>
</beans>

获取配置中的bean对象

定义一个bena对象

@Data
public class Person {
    private int id;
    private String name;
}

加载配置文件获取bena对象

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Person person = (Person) context.getBean("person");

@Configuration和@Bean注解

写一个配置类

在配置类上添加@Configuration,注解告诉了Spring这是一个配置类,相当于是一个xml文件

添加@Bean注解,相当于在xml文件里面写了一个bean标签,id是方法名getPerson() 同时,可以通过@Bean("getInfo")来修改, class就是返回值类型Person

@Configuration
public class MyConfig {
   @Bean("getInfo")
   public Person getPerson(){
      return new Person(18,"张洪浩");
   }
}

通过注解的方式获取bean对象

AnnotationConfigApplicationContext context =
                                         new AnnotationConfigApplicationContext(MyConfig.class);
Person person = context.getBean(Person.class);

@ComponentScan注解

这个注解相当于xml中的中的 e.g.<context:component-scan base-package="com.hx"/>标签

在配置类上添加该注解,@ComponentScan(value = "com.hx"),扫描配置包下如@Controller,@Service,@Repository 等注解,会加入容器中。

排除扫描

e.g.不扫描@Controller类型的注解,classes是一个数组,可以添加多个。

@ComponentScan(value = "com.hx",excludeFilters ={ @ComponentScan.Filter(type= FilterType.ANNOTATION,classes={Controller.class}) )

拦截规则FilterType,有四种:

    ANNOTATION, #注解类型
    ASSIGNABLE_TYPE,#按照给定类型
    ASPECTJ,#ASPECTJ类型
    REGEX,#正则表达式类型
    CUSTOM,#自定义类型

指定扫描

只扫描@Controller类型注解,注意要添加useDefaultFilters = false,才能生效

@ComponentScan(value = "com.hx",includeFilters ={ @ComponentScan.Filter(type= FilterType.ANNOTATION,classes={Controller.class}),useDefaultFilters = false )

自定义扫描规则

先写一个类实现TypeFilter接口,e.g:规则为扫描包下的类的类名是否包含"s"字母,符合才会注入Spring

public class MyTypeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        //获取注解信息
        //AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        //获取类信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //获取类路径
        //Resource resource = metadataReader.getResource();
        String className = classMetadata.getClassName();
        System.out.println(">>>>>>>>>>>"+className);
        if (className.contains("s")) {
            return true;
        }
        return false;
    }
}

配置类的写法:

@Configuration
@ComponentScan(value = "com.hx",includeFilters = {
        @ComponentScan.Filter(type= FilterType.CUSTOM,classes = {MyTypeFilter.class})
},useDefaultFilters = false
)

扫描结果部分示例:所有添加或未添加注解的类,只要类名有“s”字母,都会扫描出来

myConfig
cats
person
contrllerTest
daoTest
serviceTest
configBeanTest
scanTest
xmlBeanTest
getInfo

修改自定义规则为:

if (className.contains("er")) {
            return true;
  }

扫描结果为:类名包含“er"的类会扫描出来

myConfig
person
myTypeFilter
contrllerTest
serviceTest
getInfo