03 -- framework summary SS+M(Spring) -- function test (Part 2)

Posted by gwolgamott on Thu, 03 Feb 2022 06:44:17 +0100

Part I: 03---Spring function test (Part I)

catalogue

1. loc write configuration file to develop spring config xml

2.loc annotation development

3. Simulate the underlying principle of spring IOC

---1. Create bean; Class, record the class name and the full path of the class

---2. Create MyIOC class ---- > to simulate your own IOC

1. Tell the Spring framework who the bean needs to be managed is

2. Initialize beanfactory factory

3. Conduct IOC core map put

4.getBean returns the created object

3. Create User class and Dept class

4. Create TestMyioc class for testing

4.DI injection (use, the underlying principle is in the next chapter)

Test: create a new package and create two classes in the package

5. One picture: a complete illustration of the specific functions ssm of these frameworks

1. loc write configuration file to develop spring config xml

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

    <bean class="cn.tedu.pojo.Hello" id="hello"></bean>
</beans>

Create a Hello class to distinguish whether there are annotations or not

public class Hello {
    public void hi(){
        System.out.println("hello!!!!");
    }
}

Create a TestIOC class in the test package (for testing)

 @Test
    public void iocxml(){
        ClassPathXmlApplicationContext spring =
                new ClassPathXmlApplicationContext("spring-config.xml");
       //Get by id
        Object o = spring.getBean("hello");
        System.out.println(o);

        Hello h1= (Hello) o;//Downward modeling, calling from subclass method
        h1.hi();
    }

2.loc annotation development

@component: let the spring container know, create a Person class and attach annotations (the variable is public, otherwise it can't be accessed)

@Component //spring framework is used to complete IOC (help new as long as there are annotations)
//This is stored in the map {"person"=new Person()}
public class Person {
    public  String name="Zhang San";
}

 

Modify the configuration file to configure the scanner

<context:component-scan base-package="cn.tedu.pojo"></context:component-scan>

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="cn.tedu.pojo"></context:component-scan>

</beans>

Then call in TestIOC, which is roughly the same as the above process,

1. Read the core configuration file

2. Get the object} principle of new in Spring framework: put it into the map in this way, as shown in the following figure

Get the name of the Person class

@Test
    public void  ioc(){
        ClassPathXmlApplicationContext spring=
                new ClassPathXmlApplicationContext("spring-config.xml");
        
        Object o = spring.getBean("person");//The default class name in the annotation is lowercase
        Person p= (Person) o;
        System.out.println(p);//cn.tedu.pojo.Person@57cf54e1
        String name = p.name;
        System.out.println(name);
    }

3. Simulate the underlying principle of spring IOC

---1. Create bean; Class, record the class name and the full path of the class

Create a new package myioc with the same level as before, and create a class Bean

Declare two attributes name and path (class name and full path of class)

Add construction method and get set method

---2. Create MyIOC class ---- > to simulate your own IOC

1. Tell the Spring framework who the bean needs to be managed is

(omit the scanning annotation process and set the fixed class)

    //1. Tell the spring framework who needs to manage the bean s
    private List<Bean> beanfactory = new ArrayList<>();

2. Initialize beanfactory factory

//2. Initialize beanfactory
    public MyIOC() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        Bean b1 = new Bean("user", "cn.tedu.myioc.User");
        Bean b2 = new Bean("dept", "cn.tedu.myioc.Dept");
        Collections.addAll(beanfactory, b1, b2);//Put it in one time with the collection method
        //Perform IOC and split a method
        createObj();
    }

3. Conduct IOC core map put

(the name attribute of the bean creates an object list set according to the path value of the bean, which needs to be traversed to obtain the name and path)

(the map should be placed outside the method to pave the way for the following getBean methods,

At the same time, it is necessary to package it, add a private and modify it to ConcurrentHashMap to ensure the security of inserted data)

private Map<String,Object> map=new ConcurrentHashMap<>();
 //Use reflection acquisition to create new objects according to the full path of the class - reflection
Object path=Class.forName(b.getPath()).newInstance();

The most important thing is to reflect knowledge

 Map<String,Object> map=new HashMap<>();

    public void createObj() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
       for(Bean b: beanfactory){
           String name = b.getName();
            //Get the full path of the class using reflection
           Object path=Class.forName(b.getPath()).newInstance();
           map.put(name,path);
       }
        System.out.println(map);
       //{dept=cn.tedu.myioc.Dept@54bedef2, user=cn.tedu.myioc.User@5caf905d}
    }

4.getBean returns the created object


Get value(object type) according to key(name)

public Object getBean(String name){
        return map.get(name);
    }

3. Create User class and Dept class

(you can write or not, there must be these two types)

4. Create TestMyioc class for testing

(get value through key; get new User() through user ------ >)

public class TestMyioc {
    public static void main(String[] args) throws IllegalAccessException, ClassNotFoundException, InstantiationException {
        //Test the underlying ioc
        MyIOC my=new MyIOC();
        Object o = my.getBean("user");
        Object o1 = my.getBean("dept");
        System.out.println(o);//cn.tedu.myioc.User@5caf905d
        System.out.println(o1);//cn.tedu.myioc.Dept@54bedef2
    }
}

4.DI injection (use, the underlying principle is in the next chapter)

Only IOC --------- can be used for DI injection

Add an annotation to use, ---- > an annotation @ Autowired

Test: create a new package and create two classes in the package

@Component//Although there are comments, ioc can't be scanned, just change the configuration file
public class Student {
  
    String sname="Forget to tear the onion";

    @Autowired//(DI injected)
    Teacher teacher;
    
    @Override
    public String toString() {
        return "Student{" +
                "sname='" + sname + '\'' +
                ", teacher=" + teacher +
                '}';
    }
    //Use ioc to new --- and then call through bean
}
Teacher Code in class

@Component
public class Teacher {
    String tname="Zhang Sanfeng";

    @Override
    public String toString() {
        return "Teacher{" +
                "tname='" + tname + '\'' +
                '}';
    }
}

At this time, if the configuration file is not changed, you need to change the configuration file and scan the superior package

<context:component-scan base-package="cn.tedu"></context:component-scan>

Test, create a new class TestDI and add @ test annotation

public class TestDI {
    @Test
    public void di(){
        //1. Read the core configuration file
        ClassPathXmlApplicationContext spring
                =new ClassPathXmlApplicationContext("spring-config.xml");

        //2. Get directly from spring container
        Student s  = (Student) spring.getBean("student");
      //  s.teacher.tname = "Zhang San"; You have to deal with null pointers
        System.out.println(s);
//Student{sname = 'forget to tear the onion', teacher=Teacher{tname = 'Zhang Sanfeng'}}

    }
}

Summary: IOC is the help new object (unique object)

DI: you can see both class A data and class B data related to it

 

5. One picture: a complete illustration of the specific functions ssm of these frameworks

 


 

 

Topics: Java Spring Spring Boot Java framework ioc