spring Learning 6 - Assembly Beans (Automatic Assembly Beans)

Keywords: Spring xml Junit Java

spring provides three main assembly mechanisms when assembling bean s:

  • Automated assembly of bean s
  • Assembling bean s with java code
  • Assembling bean s through xml
  1. Automated assembly of bean s

Steps:

(1) Create bean s

package com.study;

import org.springframework.stereotype.Component;

@Component
public class Book {
    private String bookName;
    private String bookAuthor;

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", bookAuthor='" + bookAuthor + '\'' +
                '}';
    }
}

The Book class uses the @Component annotation, which indicates that this class is grouped into component classes and tells spring to create bean s for this class, but component scanning is not enabled by default. There are two ways to enable component scanning:

(1)adopt xml Enable component scanning

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.study"/>
</beans>

Test code:

package com.test;

import com.study.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBean {
    @Test
    public void testBean() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book book = (Book) context.getBean("book");
        book.setBookName("The Dream of Red Mansion");
        book.setBookAuthor("Cao Xueqin");
        System.out.println(book.getBookAuthor() + "writed" + book.getBookName());
    }
}

Test results:

(2) @ComponentScan annotation enables component scanning

Code:

package com.study;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class BookConfig {
}

@ ComponentScan does not declare any bean s, but uses @ComponentScan, which enables component scanning in sprig; if there are no other configurations, @ComponentScan defaults to scan the same package as the configuration class, because the BookConfig class is in the com.study package, so spring will scan the package and all subpackages under it.

Test code:

package com.test;

import com.study.Book;
import com.study.BookConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class testBean1 {
    @Test
    public void bookTest(){
        ApplicationContext context =

                new AnnotationConfigApplicationContext(BookConfig.class);
        Book book = (Book) context.getBean("book");
        System.out.println(book);
    }

}

Test results:

(2) Naming bean s scanned for components

There is no id for the Book bean in (1), but spring specifies an id for it based on the class name, that is, to change the first letter of the class name into lowercase, but we can also set an id for it according to our own wishes. eg:@Component(value="myBook")

(3) Setting up the basic package for component scanning

In (1) there is no property set for @ComponentScan. By default, it scans components with the package where the configuration class resides as the base package. To specify different base packages, the name of the package can be specified in the value attribute of @ComponentScan.

eg:@ComponentScan(basePackages={"com.study","com.study1"})

(4) Automatic assembly by adding annotations to bean s

A MyBook is injected into the Book through automatic assembly

package com.study;

import org.springframework.stereotype.Component;

@Component
public class MyBook {
    public void test(){
        System.out.println("this is MyBook class");
    }
}
package com.study;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Book {
    private String bookName;
    private String bookAuthor;
    private MyBook myBook;

    public void test() {
        System.out.println("this is book class");
        myBook.test();
    }

    public MyBook getMyBook() {
        return myBook;
    }
    @Autowired
    public void setMyBook(MyBook myBook) {
        this.myBook = myBook;
    }

   //Other get() set().....
    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", bookAuthor='" + bookAuthor + '\'' +
                '}';
    }
}

Test code:

package com.test;

import com.study.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBean {
    @Test
    public void testBean() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Book book = (Book) context.getBean("book");
        book.test();
        book.setBookName("The Dream of Red Mansion");
        book.setBookAuthor("Cao Xueqin");
        System.out.println(book.getBookAuthor() + "writed" + book.getBookName());
    }
}

Test results:

@ Autowire can be used not only on the Setter method of attributes but also on the constructor, @Autowire is spring-specific annotation, but you can also use @Inject.

 

Write this is not easy a, originally almost finished, go to a toilet browser turned off, no fire, no fire...

Posted by tlchung on Fri, 10 May 2019 08:00:52 -0700