首先下载官方文件(spring-framework-2.5.6-with-dependencies)
1.导入两个最常用的jar包
(spring-framework-2.5.6/dist/spring.jar,spring-framework-2.5.6/lib/jakarta-commons/commons-logging)
2.创建beans.xml文件
3.实例化spring容器
(1)在类路径下寻找配置文件来实例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
4.用Junit建立测试代码,测试框架是否部署成功(该步也可略去)
5.建立业务Bean
**注意要接口编程
6.修改beans.xml文件
**如果本机没有联网,当输入"<"时,不会有相应的提示,这时需要进行一些配置
打开windows-preferences-myeclipse-files and editors-xml-xmlcatalog
点"add",在出现的窗口中的Key Type中选择URL,在location中选"File system",
然后在spring解压目录的dist/resources目录中选择spring-beans-2.5.xsd,回到设置
窗口时,把Key Type改为Schemalocation,Key改为http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
beans.xml中的<bean>元素是用于配置我们要交给Spring管理的bean类。
id属性:是唯一的,通过它获得该Bean,该值不能包含特殊字符的
name属性:该值可以包含特殊字符的(如:/ss/ss)
如果没有特殊字符,建议用id
class属性:指定要交给Spring管理的Bean类
当bean创建好以后,就会由Spring容器帮我们创建和维护,当我们用到
该bean时,只需从Spring容器中获取就可以了。