spring框架的搭建小demo

来源:互联网 发布:斗牛怎么快速算点数js 编辑:程序博客网 时间:2024/05/16 18:03

这几天一直在学习研究spring和struts2,看到网上也有不少教程以及demo,我把我学习的也写一下,让网上的小伙伴参开一下。小弟我是第一次在CSDN上学东西,此文也是之前在培训的时候老师的demo例子,拿过来让大家学习一下。

1.首先新建一个web工程spring_test

2.在lib包下,引入一些jar包。

commons-logging.jar
spring-beans-3.2.8.RELEASE.jar
spring-context-3.2.8.RELEASE.jar
spring-core-3.2.8.RELEASE.jar
spring-expression-3.2.8.RELEASE.jar

这些包是跑起一个spring项目最基本的包,当然一个spring项目肯定不止这些包,还有一些commons-io-2.4.jar之类的,各位读者在实际过程中再逐步添加。我引得这些spring的包是3.2.8的。这些jar包可以去spring官网下载,都是免费开源的。

3.在src下新建这几个java文件

Computer1.java
Computer2.java
HardPan.java
TestComputer1.java
UPan.java
USB.java

这些都是基本的类,为了讲述spring的作用和演示。


其中代码部分

public class Computer1 {public USB usb;public void setUsb(USB usb){this.usb = usb;}public void execute(){System.out.println("使用计算机1");usb.input();usb.out();}}

public class Computer2 {public USB usb;public void setUsb(USB usb){this.usb = usb;}public void execute(){System.out.println("使用计算机2");usb.input();usb.out();}}

定义USB接口:

public interface USB {public void input();public void out();}

public class HardPan implements USB{@Overridepublic void input() {System.out.println("从移动硬盘里进入");}@Overridepublic void out() {System.out.println("从移动硬盘里出来");}}

public class UPan implements USB{@Overridepublic void input() {System.out.println("从U盘里进入");}@Overridepublic void out() {System.out.println("从U盘里出来");}}

写完这些java文件之后,还要添加一个spring的配置文件,applicationContext.xml

1.在applicationContext.xml中使用<bean>定义程序组件

2.使用ApplicationContext对象获取bean对象

<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">    <bean id="computer" class="com.unis.spring.test.demo1.Computer1">        <property name="usb" ref="u1"></property>    </bean>    <bean id="u1" class="com.unis.spring.test.demo1.HardPan">    </bean>    <bean id="u2" class="com.unis.spring.test.demo1.UPan">    </bean></beans>


这里的java中的class都是以bean的形式在applicationContext.xml申明。


下面是测试函数:

public class TestComputer1 {public static void main(String[] args) {System.out.println("进入TestComputer1");String conf = "applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(conf);Computer1 c1 = ac.getBean("computer", Computer1.class);c1.execute();}}


如果运行此main函数,大家猜下结果应该输出什么?

结果如下:

进入TestComputer1

使用计算机1

从移动硬盘里进入

从移动硬盘里出来

因为在applicationContext.xml里面ref配置的是u1,所以执行的是HardPand的内容,在c1.execute();中使用set注入的方式把HardPan注入。

自此,一个简单的spring Demo就生成了。

学习spring,主要学习spring的IOC和AOP,这里只是用到了简单的IOC,管理程序中组件,建立组件关系,我觉得IOC最大的作用就是为了降低程序开发过程中的耦合性,可以使用配置文件来管理我们的组件,淡然spring不止这么一点功能,还有注解等等,AOP功能也很强大,请各位小伙伴多加学习。

第一次在CSDN写博客,为了方便大家学习和我自己的积累,谢谢!!

1 0
原创粉丝点击