首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring容易配置_chapter01

2013-02-24 
spring简单配置_chapter01?6、在测试类中使用这个对象?6.1、创建Spring的工厂package com.spring.testimpor

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

热点排行