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

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}
复制代码

转载于:https://juejin.im/post/5b669d22e51d451924734c3e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值