代码改变世界

创建Spring工程的基本步骤

2017-05-05 15:49  fengna  阅读(613)  评论(0编辑  收藏  举报

第一步:加入jar包

第二步:创建配置文件

-申明javabean 配置

<?xml version="1.0" encoding="UTF-8"?>

<beans  xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="hw" class="com.amaker.test.HelloWorld">        

      <property name="name" value="amaker"/>    

      </bean>

</beans>

第三步:获取bean工厂

Resource r = new ClassPathResource("applicationContext.xml");

BeanFactory factory = new XmlBeanFactory(r);

第四步:获取对象 HelloWorld h =(HelloWorld) factory.getBean("hw");