Spring 通过xml获得容器的三种方法比较

249 阅读1分钟

applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!--<bean id="student" class="test.Student">
    <property name="name" value="zhangsan"/>
    <property name="age" value="1"/>
</bean>-->
    <context:component-scan base-package="test2"></context:component-scan>

</beans>

Studnet.java

package test2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component(value = "student1")
public class Student1 {

    @Value("lisi")
    private String name ;

    @Value("123")
    private int age;

    public String getName() {
        return name;
    }

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

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

    public int getAge() {
        return age;
    }

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

run_test

package test2;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class run_test {
    public static void main(String[] args) {
        fun3();
    }

    public static void fun1(){
        Resource resource = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinitionReader  reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) factory);
        reader.loadBeanDefinitions(resource);
        Student1 student1 = (Student1) factory.getBean("student1");
        System.out.println(student1);

    }

    public static void fun2(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student1 student1 = (Student1) ac.getBean("student1");
        System.out.println(student1);

    }

    public static void fun3(){
        Resource resource1 = new ClassPathResource("applicationContext.xml");
        BeanFactory beanFactory1 = new XmlBeanFactory(resource1);
        Student1 student1 = (Student1) beanFactory1.getBean("student1");
        System.out.println(student1);
    }
}

只有第二种才能正确的创建studnent1的bean,fun1() 和 fun3()结果都是

Student{name='null', age=0}