Spring again learning (3)

After more than a year, he mastered the Spring, SpringBoot, SpringCloud after

Once again, I go back and re-learn the Spring Framework

 

@Import notes:

Rapid import components, ID default is a component comprehensive, easy to use:

public class Demo {
}
@Configuration
@Import({Demo.class})
public class MainConfig {
......
}

 

Test categories: print all Bean

public class IOCTest {
    @Test
    public void test() {
        printBeans();
    }

    private void printBeans() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
    }
}

 

 

ImportSelector selector: YES underlayer Application SpringBoot

@Configuration
@Import({MyImportSelector.class})
public class MainConfig {
}
package org.dreamtech.condition;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportSelector implements ImportSelector {
    /**
     * 重写
     * @param annotationMetadata 标注@Import类其他的注解
     * @return 注册对象
     */
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"org.dreamtech.bean.Demo","org.dreamtech.bean.Person"};
    }
}

 

Print Demo discovery and Person were successfully registered

 

ImportBeanDefinitionRegistrar:

@Configuration
@Import({MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class MainConfig {
}
package org.dreamtech.condition;

import org.dreamtech.bean.Demo;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     * 注册
     *
     * @paramannotationMetadata all annotations current class information 
     * @param BeanDefinitionRegistry register class
      * / 
    public  void registerBeanDefinitions (AnnotationMetadata annotationMetadata, the BeanDefinitionRegistry BeanDefinitionRegistry) {
         Boolean Definition = beanDefinitionRegistry.containsBeanDefinition ( "org.dreamtech.bean.Demo" );
         // container if org .dreamtech.bean.Demo, then a manual registration mydemo 
        IF (Definition) { 
            the BeanDefinition BeanDefinition = new new RootBeanDefinition (Demo. class ); 
            beanDefinitionRegistry.registerBeanDefinition ( "mydemo"  , BeanDefinition);
        }
    }
}

After the test print as follows:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
org.dreamtech.bean.Demo
org.dreamtech.bean.Person
MyDemo

 

The last vessel registered to Bean's way:

FactoryBean : the Spring Framework integration with other widely used to

Package org.dreamtech.bean; 

Import org.springframework.beans.factory.FactoryBean; 

public  class DemoFactoryBean the implements the FactoryBean <Demo> {
     / ** 
     * Demo Object 
     * 
     * @return returns an object, it will be added to the vessel 
     * @throws Exception exception
      * / 
    public Demo the getObject () throws Exception { 
        System.out.println ( "Object Create Demo ......" );
         return  new new Demo (); 
    } 

    / ** 
     * Get type 
     * 
     * @returnClass
      * / 
    public class <?> GetObjectType () {
         return Demo. Class ; 
    } 

    / ** 
     * whether singleton 
     * true: Single Instance 
     * false: multiple instances 
     * 
     * @return Boolean
      * / 
    public  Boolean isSingleton () {
         return  to true ; 
    } 
}
package org.dreamtech.config;

import org.dreamtech.bean.DemoFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 */
@Configuration
public class MainConfig {
    @Bean
    public DemoFactoryBean demoFactoryBean(){
        return new DemoFactoryBean();
    }
}

 

Bean assembly is DemoFactoryBean, then the container Bean is it right?

package org.dreamtech.test;

import org.dreamtech.config.MainConfig;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class IOCTest {

    private static AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

    @Test
    public void test() {
        printBeans();
        Object bean=getBeanByName("demoFactoryBean");
        System.out.println(bean.getClass());
    }

    private void printBeans() {
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
    }

    private Object getBeanByName(String name) {
        return applicationContext.getBean(name);
    }
}

 

Print: Bean found that although there is a name demoFactoryBean, but it is the type of Demo

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
demoFactoryBean
创建Demo对象......
class org.dreamtech.bean.Demo

 

Multi modified embodiment modes:

    public boolean isSingleton() {
        return false;
    }
    @Test
    public void test() {
        printBeans();
        Object bean1=getBeanByName("demoFactoryBean");
        Object bean2=getBeanByName("demoFactoryBean");
        System.out.println(bean1==bean2);
    }

Print: every acquisition will live call getObject

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
demoFactoryBean
创建Demo对象......
创建Demo对象......
false

 

If I want to register DemoFactoryBean force itself?

View source BeanFactory this section:

public interface BeanFactory {
    String FACTORY_BEAN_PREFIX = "&";
}

Then try adding & prefix:

    @Test
    public void test() {
        printBeans();
        Object bean = getBeanByName("&demoFactoryBean");
        System.out.println(bean.getClass());
    }

Printing is as follows:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
demoFactoryBean
class org.dreamtech.bean.DemoFactoryBean

 

Guess you like

Origin www.cnblogs.com/xuyiqing/p/11301701.html