Spring复习:(1) DefaultListableBeanFactory和BeanDefinitionReader

一、beans11.xml:



    
        
        
    





二、Student类:

package cn.edu.tju.domain;

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

三、主类:

package cn.edu.tju;

import cn.edu.tju.domain.Student;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Start14 {
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("beans11.xml");
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(factory);
        beanDefinitionReader.loadBeanDefinitions(resource);

        Student student = factory.getBean("student", Student.class);
        System.out.println(student);

    }
}

你可能感兴趣的:(Spring,spring,java,后端)