Bean的实例化

前端之家收集整理的这篇文章主要介绍了Bean的实例化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一.构造器实例化

      One.class类:

public class One {
    public void say(){
        System.out.println("我是构造器实例化");
    }
}

  applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    <bean id="one" class="cn.chauncey.ioc.One"/>
</beans>

  Test.class测试类:

public class OneTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        One one = (One) applicationContext.getBean("one");
        one.say();
    }
}

  运行结果:

分享图片

猜你在找的XML相关文章