Spring configuration bean process based on XML file


This article mainly introduces the spring based on XML file configuration bean process detailed explanation, the article through the example code introduction is very detailed, to everybody’s study or the work has certain reference study value, needs the friend may refer to

Configure by full class name:

Class: the full class name of the bean. The bean is created in the IOC container by reflection, so it is required that there must be a non parameter constructor in the bean.

    <property name="name" value="jack"></property>

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

Beanfactory: basic implementation of IOC container.

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

The main implementation classes of ApplicationContext are as follows:

  • Classpathxmlapplicationcontext: loads the configuration file from the classpath.
  • Filesystemxmlapplicationcontext: loads the configuration file from the file system.
  • Configurable ApplicationContext is extended to ApplicationContext by adding two methods, refresh() and close(), to enable ApplicationContext to start, refresh and close the context.

Applicaiotncontex instantiates all singleton beans in context at initialization time.

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

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 >, which is set one by one according to the initialization parameters in the constructor. At the same time, overloaded constructors can be distinguished according to the order of parameters index and the type of parameters.

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

< bean > // the first way is to inject attribute values
    <constructor-arg value="tom" index="0" type="java.lang.String"></constructor-arg>
    < constructor Arg value = 12 "index =" 1 "type =" int "> < / constructor Arg > // the second way is to inject attribute values
    <constructor-arg index="2" type="double">

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;
  public String toString() {
    return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
public static void main(String[] args) {
    //1. Create spring IOC container object
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    //2. Get bean instance from container
    Student student = (Student) ctx.getBean("student"); 


When attribute values have special symbols, the following methods should be used:

    <constructor-arg index="0" type="java.lang.String">

Use <! [CDATA [attribute value]] >.