读书人

spring容易配置_chapter01

发布时间: 2013-02-24 17:58:56 作者: rapoo

spring简单配置_chapter01

?6、在测试类中使用这个对象

?

6.1、创建Spring的工厂

package com.spring.test;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.modle.HelloWorld;public class TestSpring {//创建Spring的工厂private BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");@Testpublic void testHello(){//通过工厂获取Spring的对象//此处getBean中的helloWorld就是beans.xml配置文件中的id//HelloWorld hello = (HelloWorld)factory.getBean("helloWorld");HelloWorld hello = beanFactory.getBean("helloWorld",HelloWorld.class);//此时的hello对象就是被Spring所管理的对象System.out.println(hello.hello());}}

?

?注:

spring包:http://l19.yunpan.cn/lk/Q25bVK8QKdrt3

spring依赖包:http://l19.yunpan.cn/lk/Q25bK3grxwa7t

读书人网 >编程

热点推荐