Detailed explanation of spring bean configuration process based on XML file

This article mainly introduces the detailed process of configuring bean based on XML file in spring. The example code is introduced in great detail, which has certain reference value for everyone's study or work. Friends in need can refer to it

Configure by full class name:

Class: the full class name of the bean. The bean is created in the IOC container through reflection, so there must be a parameterless constructor in the bean.

  <bean id="helloWorld" class="com.gong.spring.beans.HelloWorld">
    <property name="name" value="jack"></property>
  </bean>

Before the spring IOC container reads the bean configuration and creates an instance of the bean, it needs to instantiate the container. Spring provides two types of IOC container implementations:

Beanfactory: the basic implementation of IOC container.

ApplicationContext: it provides more advanced features and is a sub interface of beanfactory.

ApplicationContext mainly implements the following classes:

Applicaiotncontex instantiates all singleton beans at initialization time.

Webapplicationcontext is specially used for web applications. It allows initialization from the path relative to the web root directory.

Three ways of dependency injection

(1) Property injection: through the setter method: < property name = "name" value = "Jack" > < / property >, that is, there is a setter method in the bean.

(2) Constructor injection: < constructor Arg value = "" index = "0" type = "" > < / constructor Arg >, set one by one according to the initialized parameters in the construction method. At the same time, overloaded constructors can be distinguished according to the order of parameters, index and type of parameters.

(3) Factory method injection (rarely used, not recommended)

<bean id="student" class="com.gong.spring.beans.Student">    //第一种方式注入属性值
    <constructor-arg value="tom" index="0" type="java.lang.String"></constructor-arg>
    <constructor-arg value="12" index="1" type="int"></constructor-arg>    //第二种方式注入属性值
    <constructor-arg index="2" type="double">
      <value>99.00</value>
    </constructor-arg>
  </bean>
package com.gong.spring.beans;

public class Student {
  private String name;
  private int age;
  private double score;
  public Student(String name,int age,double score) {
    this.name = name;
    this.age = age;
    this.score = score;
  }
  @Override
  public String toString() {
    return "Student [name=" + name + ",age=" + age + ",score=" + score + "]";
  }

}
public static void main(String[] args) {
    //1.创建spring的IOC容器对象
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.从容器中获取Bean实例
    Student student = (Student) ctx.getBean("student");
    System.out.println(student.toString());
  }

Output:

When the attribute value has a special symbol, use the following method:

    <constructor-arg index="0" type="java.lang.String">
      <value><![CDATA[<tom>]]></value>
    </constructor-arg>

Use .

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>