关于spring框架的用法

1.导包:

myeclipse软件都是自带spring框架的架包的,所以无须去特意下载,具体做法:右击项目-myeclipse-Add MyEclipse Spring and User libraries to project,进入页面 选中前四个和spring3.0web libraries  最后finish。

关于spring框架的用法

2.配置xml文件:

在web.xml文件里面加入如下固定代码:

<!-- applicationContext -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
  <!-- ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.建立三层:

开始写你所需要的三层,但这次三次和以往不同,不再使用 接口new 方法,还是使用私有的属性进行传值。

关于spring框架的用法

4.配置applicationContext.xml文件:

关于spring框架的用法

5.实现类:

在实现类里面加入如下代码:

ClassPathXmlApplicationContext spring=
new ClassPathXmlApplicationContext("applicationContext.xml");

然后在你要用的方法里面调用这个spring方法,这样三层就和spring链接起来了:
Action a=(Action) spring.getBean("action");

a.action();

以上程序都是为了实现spring而写的,并没有涉及到数据库,页面以及其他框架,所以还是非常简单的。