Spring annotation driven development (II) - injection assembly

I. Introduction

  Previous We built a simple Spring projects, and using simple components registration. This one, we explain in detail the components of the injection.

Second, the components injection

1. @ComponentScan

  In the last article, we use the components to achieve a @Configuration and @Bean injection. But if a lot of cases need to inject components, each component needs to be injected through a @Bean notes, which would be very troublesome. So Spring provides @ComponentScan comment.

  @ComponentScan packages need to be scanned can be specified, in these packages, @ Component annotation components are denoted by injection into Spring container.

  Note, @ Controller, @ Service, @ Repository These notes contain @Component annotation features.

@Controller
public class PersonController {
}
@Service
public class PersonService {
}
@Repository
public class PersonDao {
}
@ComponentScan(basePackages = "indi.zqc")
@Configuration
public class MainConfig {

    @Bean(name = "person")
    public Person person() {
        return new Person("张三", 13);
    }

}
public class MainConfigTest {

    @Test
    public void test() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        // 获取容器中的所有Bean描述
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
        }
    }
}

2. Custom TypeFilter

  In use @ComponentScan annotations, the package will scan all of the components are injected into the marked @Component Spring container. But sometimes we do not need to inject one or some components, you'll probably want to get rid of @Component notes just fine. But such a scenario, if you want to inject a third-party package package, then you can not modify the code third-party package. @ComponentScan annotations have an excludeFilters property can exclude components. Below, the PersonController excluded.

@ComponentScan(basePackages = "indi.zqc",
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = PersonController.class)
        })
@Configuration
public class MainConfig {

    @Bean(name = "person")
    public Person person() {
        return new Person("张三", 13);
    }

}

  There are several types @ ComponentScan.Filter

  • FilterType.ANNOTATION by matching annotation, annotation type fitting parameters specify classes
  • By matching FilterType.ASSIGNABLE_TYPE class, classes with specified class parameter type
  • FilterType.ASPECTJ by the expression AspectJ type matching, fitting parameters specify an expression pattern
  • FilterType.REGEX by regular expression matching the class name, with parameter specifies the expression pattern
  • FilterType.CUSTOM custom, complex class parameters specify classes, class needs to implement custom interfaces org.springframework.core.type.filter.TypeFilter

  Component may be specified by the attribute excludeFilters excluded, of course, may be introduced through the specified component includeFilters.

  We first note by userDefaultFilters = false closed @CompenentScan default function. The assembly is then introduced through the specified property includeFilters.

@ComponentScan(basePackages = "indi.zqc",
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = PersonController.class)
        },
        useDefaultFilters = false)
@Configuration
public class MainConfig {

    @Bean(name = "person")
    public Person person() {
        return new Person("张三", 13);
    }

}

3. @Scope

4. @Lazy

5. @Conditional

Third, the link 

"Spring annotation-driven development (a) - Project to build"

"Spring annotation-driven development (b) - Component injection"

Guess you like

Origin www.cnblogs.com/zhuqianchang/p/11407642.html