Spring的脚手架工程搭建
一 下载
https://repo.spring.io/libs-release-local/org/springframework/spring/4.0.4.RELEASE/
下载文件
spring-framework-4.0.4.RELEASE-dist.zip
二 下载内容说明
1 docs:该文档夹下存放Spring的相关文档,包含开发指南、API参考文档。
2 libs:包括三类Jar包:Spring框架class文件的JAR包,Spring框架源文件的压缩包,Spring框架API文档压缩包。
3 schemas:包含了Spring的各种配置文件的XML Schema文档
4 其他:readme.txt
三 Eclipse创建项目
1 创建myspring项目

2 创建Spring4.0.4库

3 创建common-logging库

四 源代码编写
1 配置文件
2 编写Bean
Person
package org.crazyit.app.service;public class Person
{private Axe axe;// 设值注入所需的setter方法public void setAxe(Axe axe){this.axe = axe;}public void useAxe(){System.out.println("我打算去砍点柴火!");// 调用axe的chop()方法,// 表明Person对象依赖于axe对象System.out.println(axe.chop());}
}
Axe
package org.crazyit.app.service;public class Axe
{public String chop(){return "使用斧头砍柴";}
}
五 测试
package lee;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import org.crazyit.app.service.*;public class BeanTest
{public static void main(String[] args)throws Exception{// 创建Spring容器ApplicationContext ctx = newClassPathXmlApplicationContext("beans.xml");// 获取id为person的BeanPerson p = ctx.getBean("person" , Person.class);// 调用useAxe()方法p.useAxe();}
}
六 测试结果
我打算去砍点柴火!
使用斧头砍柴
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
