Spring-IoC Series - Assembly bean

We define a user class:

@Data
@Builder
public class User {
    private Long id;
    private String name;
}

And then define a java configuration file AppConfig:

@Configuration
public class AppConfig {

    @Bean
    public User user(){
        return User.builder().id(1L).name("谢飞").build();
    }
}

Note here that @Configuration, which is representative of this is a java configuration file, spring containers will be based on it to generate IoC container to assemble bean, @ Bean on behalf of the user method returns pojo fitted to the ioc container, you can do these use

AnnotationConfigApplicationContext to build your own ioc container.
    @Test
    public void test(){
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        User user = ctx.getBean(User.class);
        log.info(user.toString());
    }

operation result: 

This is the simplest method, but also notes @bean is not the only way to create a bean.

If one found that the use of the use of annotations @bean injected into the spring ioc container is a very troublesome thing from the above method, good spring also allows us to use the scan to assemble bean ioc container, use the comment is @Component and @ComponentScan .

@Component mark which is to be scanned into the class ioc container, and @ComponentScan is indicated what strategy employed to scan assembly bean.

user:

@Component
@Data
public class User {

    @Value("2")
    private Long id;
    @Value("谢飞2")
    private String name;
}
AppConfig:
@Configuration
@ComponentScan
public class AppConfig {
}

Here to join @ComponentScan, meaning that it will be scanned, but the current package will only scan where the class AppConfig and its sub packages

test:

    @Test
    public void test(){
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        User user = ctx.getBean(User.class);
        log.info(user.toString());
    }

effect:

 @ComponentScan also allows us to custom scan packages

Look Source:

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.core.annotation.AliasFor;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
//在一个类中可重复定义
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    //定义扫描的包
    @AliasFor("basePackages")
    String[] value() default {};
    //定义扫描的包
    @AliasFor("value")
    String[] basePackages() default {};

    //定义扫描的类
    Class<?>[] basePackageClasses() default {};
    //bean  name 生成器
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
    //作用域解析器
    Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
    //作用域代理模式
    ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
    //资源匹配模式
    String resourcePattern() default "**/*.class";
    //是否启用默认的过滤器
    boolean useDefaultFilters() default true;
    //当满足过滤条件时扫描
    ComponentScan.Filter[] includeFilters() default {};
    //当不满足过滤条件的时候扫描
    ComponentScan.Filter[] excludeFilters() default {};
    //是否延迟初始化
    boolean lazyInit() default false;
    //定义过滤器
    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    public @interface Filter {
        //过滤器类型,可以按注解类型或者正则式等过滤
        FilterType type() default FilterType.ANNOTATION;
        //定义过滤的类
        @AliasFor("classes")
        Class<?>[] value() default {};
        //定义过滤的类
        @AliasFor("value")
        Class<?>[] classes() default {};
        //匹配方式
        String[] pattern() default {};
    }
}

 

Guess you like

Origin blog.csdn.net/qq_36850813/article/details/94731787