1、bean加载方式:xml+<bean/>

2、bean加载方式:

xml:context+注解(@Configuration和@Bean,@Component,@Controller,@Service,@Repository)

3、bean加载方式:

配置类+扫描注解(@ComponentScan)+注解(@Configuration和@Bean,@Component,@Controller,@Service,@Repository)

4、bean加载方式:通过注解@ImportResource加载配置类并加载配置文件(既能通过配置文件定义bean也能通过注解配置bean)

@Configuration
@ComponentScan("com.imall")
@ImportResource("applicationContext-config.xml")
public class SpringConfig {
}

5、bean加载方式:@Import注解导入配置类

@Import(Order.class)
public class SpringConfig {
}

6、bean加载方式:使用AnnotationConfigApplicationContext注册bean

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
ctx.registerBean("order", Order.class);

7、bean加载方式:使用@Import和ImportSelector配合,实现选择模式加载

@Configuration
@Import(MyImportSelector.class)
public class SpringConfig {
}
public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        boolean b = importingClassMetadata.hasAnnotation("org.springframework.context.annotation.Configuration");
        if(b){
            return new String[]{"com.imall.s2c.order.entity.Order"};
        }
        return new String[0];
    }
}

8、bean加载方式:使用@Import和ImportBeanDefinitionRegistrar配合,实现选择模式加载,并重命名加载的类名称

@Configuration
@Import(MyImportRegistrar.class)
public class SpringConfig {
}
public class MyImportRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean b = importingClassMetadata.hasAnnotation("org.springframework.context.annotation.Configuration");
        if(b){
            AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Order.class).getBeanDefinition();
            //注册Order,并重命名为myOrder
            registry.registerBeanDefinition("myOrder", beanDefinition);
        }
    }
}

9、bean加载方式:使用@Import和BeanDefinitionRegistryPostProcessor配合,实现加载并重命名加载的类名称,如果容器存在相同class文件,则最终加载BeanDefinitionRegistryPostProcessor中注册的class文件。

public interface BookService {
    void check();
}

@Service("bookServiceImpl1")
public class BookServiceImpl1 implements BookService{
    @Override
    public void check() {
        System.out.println("bookServiceImpl1");
    }
}

@Service("bookServiceImpl2")
public class BookServiceImpl2 implements BookService{
    @Override
    public void check() {
        System.out.println("bookServiceImpl2");
    }
}

@Service("bookServiceImpl3")
public class BookServiceImpl3 implements BookService{
    @Override
    public void check() {
        System.out.println("bookServiceImpl3");
    }
}
@Configuration
@Import({BookServiceImpl3.class,MyImportRegistrar.class,MyPostProcessor.class})
public class SpringConfig {
}

public class MyImportRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean b = importingClassMetadata.hasAnnotation("org.springframework.context.annotation.Configuration");
        if(b){
            AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(BookServiceImpl1.class).getBeanDefinition();
            //注册Order,并重命名为myOrder
            registry.registerBeanDefinition("bookService", beanDefinition);
        }
    }
}

public class MyPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(BookServiceImpl2.class).getBeanDefinition();
        //注册Order,并重命名为myOrder
        registry.registerBeanDefinition("bookService", beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}

10、配置类中的带有@Bean注解的方法,方法之间调用会从IOC容器去获取实例对象,这样的获取方式是通过代理方法从容器中获取的,保证了对象是单例的

@Configuration
public class SpringConfig {
    @Bean
    public Order order(){
        return new Order();
    }
}

11、使用@Import实现使用功能再加载bean,使用@EnableConfigurationProperties和@ConfigurationProperties实现使用指定属性类再加载bean

@Configuration
@Import(CartoonCatAndMouse.class)
public class SpringConfig {
}

@Data
@EnableConfigurationProperties(CartoonProperties.class)
public class CartoonCatAndMouse {

    private Cat cat;
    private Mouse mouse;

    public CartoonCatAndMouse(CartoonProperties cartoonProperties) {
        cat=new Cat();
        mouse=new Mouse();
        if(cartoonProperties.getCat()!=null){
            cat.setName(cartoonProperties.getCat().getName().isEmpty()?"tom":cartoonProperties.getCat().getName());
            cat.setAge(cartoonProperties.getCat().getAge()==null?3:cartoonProperties.getCat().getAge());
        }
        if(cartoonProperties.getMouse()!=null){
            mouse.setName(cartoonProperties.getMouse().getName().isEmpty()?"jerry":cartoonProperties.getMouse().getName());
            mouse.setAge(cartoonProperties.getMouse().getAge()==null?4:cartoonProperties.getMouse().getAge());
        }
    }

    public void play(){
        System.out.println(cat.getAge()+"岁的"+cat.getName()+"和"+mouse.getAge()+"岁的"+mouse.getName()+"打起来了");
    }
}

@Data
@ConfigurationProperties(prefix = "cartoon")
public class CartoonProperties {
    private Cat cat;
    private Mouse mouse;
}