Section IV - Interpretation of configuration files

Keywords: Java Hibernate Spring

1, scope interpretation

  1. scope has two values, singleton and prototype
  2. single is the default value, which means that the UserDaoImpl class retrieved from the xml is the only one each time, and the class is created when the app object is created
//Test code
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
userDao dao1 = (userDao) app.getBean("UserDao");
userDao dao2 = (userDao) app.getBean("UserDao");
System.out.println(dao1);
System.out.println(dao2);

  1. prototype means that the same object is not fetched every time, and the creation time is created every time the value is fetched through the app

2, Lifecycle configuration

  1. Init method: Specifies the initialization method of the class
  2. Destroy method: Specifies the destroy method in the class
  3. give an example:
    1) Create init and destroy in the UserDaoImpl class
    2) Configure init method and destroy method parameters in applicationContext
public class UserDaoImpl  implements userDao {

    @Override
    public void save() {
        System.out.println("save running..........");
    }

    public void init(){
        System.out.println("Initialization method");
    }

    public void destroy(){
        System.out.println("Destruction method");
    }
}

3, Dependency injection -- set injection

  1. Premise of use: when A.class object is nested in B.class, when we use B's object, we can execute A's method through B's method
  2. Create class A
public class A {
    public void test_a(){
        System.out.println("A");
    }
}
  1. Create class B
private class B {
    public A a_impl;

    public void setA_impl(A a_impl) {
        this.a_impl = a_impl;
    }

    public void test_b(){
        a_impl.test_a();
    }
}
  1. Configure the applicationContext file, where the name in property is the same as the name in the set function, but the initial letter is lowercase, and ref is the same as the bean id value in a
<bean id="a" class="A"/>
    <bean id="b" class="B">
        <property name="a_impl" ref="a"></property>
    </bean>

  1. Create a B object in Demo01 and call test_b. implement the method of class A
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
B b_impl = (B) app.getBean("b");
b_impl.test_b();

3, 2 simplify applicationContext file configuration

  1. Introduce p namespace
xmlns:p="http://www.springframework.org/schema/p"
  1. Simplify b's bean configuration
<bean id="b" class="B" p:a_impl-ref="a"></bean>

4, Dependency injection -- parametric structure injection

  1. Modify class B code to generate parameterized construction and nonparametric construction
public class B {
    private A a_impl;
    
    public B(A a_impl) {
        this.a_impl = a_impl;
    }

    public B() {
    }

    public void test_b(){
        a_impl.test_a();
    }
}
  1. Modify the applicationContext file, where name is consistent with the passed in parameters of the constructor, and ref is consistent with the id ID id in a's bean
<bean id="b" class="B">
	<constructor-arg name="a_impl" ref="a"></constructor-arg>
</bean>

  1. implement
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
B b_impl = (B) app.getBean("b");
b_impl.test_b();

5, Other types of injection

  1. Injection is divided into
    1) Reference data type injection (previous objects also belong to reference type)
    2) Common data type injection
    3) Collection data type injection
  2. Common data type injection example - set injection (parametric structure injection)
    1) Declare int type variables in the B.class file and create the corresponding set function
    2) Modify applicationContext configuration file
public class B {
    private int age;

    public void setAge(int age) {
        this.age = age;
    }

    public void test_b(){
        System.out.println(age);
    }
}

  1. Collection data type injection
    1) Create A.class file
    2) Create a B.class file, an A-type list and a string type list
    3) Modify applicationContext
//A.class
public class A {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
//B.class
import java.util.List;

public class B {
    private List<String> stringArrayList;
    private List<A> aArrayList;

    public void setStringArrayList(List<String> stringArrayList) {
        this.stringArrayList = stringArrayList;
    }

    public void setAArrayList(List<A> aArrayList) {
        this.aArrayList = aArrayList;
    }

    public void test_b(){
        System.out.println(stringArrayList);
        System.out.println(aArrayList);
    }
}

6, Use the import tag to separate the configuration files

  1. Create a new spring config named applicationContext-b

  2. Annotate the original bean of B, transfer it to applicationContext-b, and import it with the import tag

Posted by w00kie on Sat, 02 Oct 2021 16:49:34 -0700