Bean production

@Bean
@Configuration // Config class == config file, telling Spring that this is a config class
public class MainConfig {

    // Register a Bean for the container; Type is the type of the return value. Id defaults to the method name as id
    @Bean("PersonX")
    public Person person(a){
        return new Person("lisi".20); }}Copy the code
    @Test
    public void test01(a){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

        Person person1 = applicationContext.getBean(Person.class);
        System.out.println(person1);

        Person person2 = (Person) applicationContext.getBean("PersonX");
        System.out.println(person2);
    }
Copy the code
@ComponentScan
  1. Package scan: @Controller, @Service, @Repository, @Component will be scanned and added to the container. The bean ID will be lowercase
@Configuration
@ComponentScan(value = "cn.ccb")
public class MainConfig {}Copy the code
@Test
public void test02(a){
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

    String[] names = applicationContext.getBeanDefinitionNames();
    for(String name : names) { System.out.println(name); }}Copy the code

Results:

mainConfig
bookController
bookDao
bookService
Copy the code
  1. Packet scan, ruled outexcludeFilters
@Configuration
@ComponentScan(value = "cn.ccb",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes={Controller.class,Service.class})})
public class MainConfig {}Copy the code
@Test
public void test02(a){
    // Paste the test02() code above here
}
Copy the code

Results:

mainConfig
bookDao
Copy the code
  1. Packet scanning, includingincludeFilters
@Configuration
@ComponentScan(value = "cn.ccb",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes={Controller.class,Service.class})},useDefaultFilters=false)
public class MainConfig {}Copy the code
@Test
public void test02(a){
    // Paste the test02() code above here
}
Copy the code

Results:

mainConfig
bookController
bookService
Copy the code
@Scope
@Configuration
public class MainConfig {
    
    // singleton: singleton (default). When the IOC container is started, methods are called to create objects to be placed in the IOC container
    // prototype: multi-instance,ioc container does not call method to create object in the container, every time fetch method to create object
    @Scope("prototype")
    @Bean
    public Person person(a){
        return new Person("Zhang".22); }}Copy the code
@Import
@Configuration
@Import({Color.class,Red.class})  // Import the component. The default id is the full class name of the component
public class MainConfig {}Copy the code

Results:

cn.ccb.entities.Color
cn.ccb.entities.Red
Copy the code

Bean assignment

@Value
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    
    @value (" Sichuan girl ")
    private String name;

    @Value("1234")
    private int age;
}
Copy the code
@Configuration
public class MainConfigOfPropertyValues {

    @Bean
    public Person person(a){
        return newPerson(); }}Copy the code
@Test
public void test1(a){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);

    Object person = applicationContext.getBean("person");
    System.out.println(person);
}
Copy the code

Results:

Person(name= name, age=1234)Copy the code
@Autowired
@Service
public class BookService {

/ * * * the @autowired: automatic injection * 1, the default according to the type to find corresponding container components: applicationContext. GetBean (BookController. Class); @qualifier: specifies the id of the component to be installed. */ @autoWired Private BookController BookController; }Copy the code
@Resource
@Service
public class BookService {

    / * * *@ResourceYou can and@AutowiredBy default, the variable name is used as the component ID to find the component in the container. * Not supported@PrimaryFunction and@Autowired(required=false)
     * @Autowired: spring defined,@ResourceIs the Java specification */
    @Resource(name = "InnerConfig")
    private BookController bookController;
}
Copy the code

FactoryBean

// Create a FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {

    public Color getObject(a) throws Exception {
        System.out.println("Called ----------");
        return new Color();
    }

    publicClass<? > getObjectType() {return Color.class;
    }

    public boolean isSingleton(a) {
        return false; }}Copy the code
@Configuration
public class MainConfig {
    @Bean
    public ColorFactoryBean colorFactoryBean(a){
        return newColorFactoryBean(); }}Copy the code
@Test
public void test03(a){
    /** * Spring provides FactoryBean: * by default, the FactoryBean gets the object created by calling getObject(). * to get the FactoryBean itself, you need to add &-- > &colorfactorybean */ to the Bean id
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

    Object bean = applicationContext.getBean("colorFactoryBean");
    System.out.println(bean.getClass());  // class cn.ccb.entities.Color

    Object bean2 = applicationContext.getBean("&colorFactoryBean");
    System.out.println(bean2.getClass()); // class cn.ccb.entities.ColorFactoryBean

}
Copy the code