3.2 project startup process analysis (understanding)
Find the entry class of the project (described with @ SpringBootApplication annotation), then run the startup class to detect the startup process. When SpringBoot is started, the console will show the identification as shown in figure-14:
Figure-14
What happened when the project started? I drew a picture here to help you analyze it, as shown in figure-15:
Figure-15
When the SpringBoot project is started, it is automatically configured based on the annotation description on the startup entry class, and the classes in the specified package and sub package are scanned for loading, Then check whether there are annotation descriptions specified in the Spring framework on the class (for example, @ Component,@Controller,@Service, etc.). If so, give the class to the implementation class object of the BeanFactory factory interface in the Spring framework. This factory object will create an instance of the Bean based on reflection. If the Bean specifies a lifecycle method, it will also call the lifecycle method. After the instance is created, the Spring framework will also call the lifecycle method based on the scope description of the class, Store instances in containers with different scopes. To realize the scientific application of Bean objects.
@Retention(RetentionPolicy.RUNTIME) //Set the Component annotation to be valid at runtime @Target(ElementType.TYPE) //Set the objects that Component annotations can describe @interface Component{} @Component class Container{} public class TestScanClass01 { static Map<String,Object> beanPool=new HashMap<>(); public static void main(String[] args) throws Exception{ //1. Get the bytecode object of the startup class Class<?> cls=TestScanClass01.class; //2. Get the package where the startup class is located String pkgName=cls.getPackage().getName(); System.out.println("pkgName="+pkgName); //3. Convert package structure to directory structure String clsDir=pkgName.replace(".", "/"); System.out.println(clsDir); //4. Get the directory where the class file corresponding to the directory is located URL url=ClassLoader.getSystemResource(clsDir); System.out.println("path="+url.getPath()); //5. Get all classes in the directory corresponding to the url File file=new File(url.getPath()); String[] classNames=file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".class"); } }); //6. Load the class and create an instance of the class based on the configuration information of the class for(String className:classNames) {//AAA.class String classShortName= className.substring(0,className.indexOf(".")); Class<?> clsObject= ClassLoader.getSystemClassLoader().loadClass( pkgName+"."+classShortName); System.out.println(clsObject.getName()); //Detect whether there is a component on the class Class annotation //boolean flag=clsObject.isAnnotationPresent(Component.class); Annotation an=clsObject.getAnnotation(Component.class); if(an==null)continue; Object instance=clsObject.newInstance();//The underlying constructs instances of classes by constructing method objects beanPool.put(classShortName.substring(0, 1).toLowerCase()+classShortName.substring(1), instance); } //7. Output objects in beanPool System.out.println(beanPool); } }
- Preliminary implementation of project business and test business implementation: manage and implement Bean objects through spring framework based on SpringBoot scaffold. Step 1: create a DefaultCache class and give it to spring management. package com.cy.pj.common.cache; @Component / / is defined by spring and is mainly used to describe some types of objects managed by the spring framework / / public class DefaultCache {} where @ component is an annotation in spring that describes Bean classes. It is used to tell spring that the instance of this class is created by spring. When this object is created and managed by spring, By default, objects will be stored in the pool (Bean pool). Step 2: add a sringboot test class to obtain and test beans, which should be placed in the test directory: package com.cy.pj.common.cache; @SpringBootTest / / describe that this class is a test class, and give it to the spring framework to manage public class defaultcachetests {/ * * * @ Autowired describes the attribute, indicating that the value of this attribute should be injected by the spring framework * / @ Autowired private DefaultCache defaultcache; @ test public void testcache() {system. Out. Println (DefaultCache);}} Wherein: the @ SpringBootTest annotation is used to tell the spring framework that this test class is managed by spring. When the @ Autowired annotation describes the attribute, it is used to tell the spring framework to inject a value into this attribute? (as for the injection rule, it will be gradually strengthened in later courses)
Step 3: code design and operation analysis, as shown in figure-16: figure-16 describes the relationship between DefaultCacheTests class and DefaultCache class, These two classes are described by specifying annotations (@ SpringBootTest,@Component). The intention is to tell the spring framework that the creation of instances of these two classes is the responsibility of spring, and the spring framework completes the injection (DI) of values of DefaultCache type in the DefaultCacheTests instance based on the description of @ Autowired annotation.