spring framework入门

来源:互联网 发布:mac可以玩跑跑卡丁车吗 编辑:程序博客网 时间:2024/05/20 03:40
参照:http://www.yiibai.com/spring/spring-tutorial-for-beginners.html
示例代码下载:http://download.csdn.net/detail/u010476739/9921767

1.工具

maven
spring framwork 4.2.4.RELEASE

2.使用eclipse创建maven工程

新建工程:
选择maven工程:




创建工程后如下:

2:引入spring framework

使用pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.jackletter</groupId>  <artifactId>hellospring</artifactId>  <version>0.0.1-SNAPSHOT</version>  <name>this is name</name>  <description>this is desc</description>    <properties>  <spring.version>4.2.4.RELEASE</spring.version>  </properties>    <dependencies>         <!-- Spring Core -->        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>                 <!-- Spring Context -->        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>             </dependencies></project>

3.java代码

HelloProgram
package hellospring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import hellospring.service.HelloWorld;import hellospring.service.HelloWorldService;public class HelloProgram {public static void main(String[] args) {@SuppressWarnings("resource")ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");HelloWorld hw = service.getHelloWorld();hw.sayHello();}}

HelloWorld

package hellospring.service;public interface HelloWorld {public void sayHello();}

HelloWorldService
package hellospring.service;public class HelloWorldService {      private HelloWorld helloWorld;      public HelloWorldService() {      }      public void setHelloWorld(HelloWorld helloWorld) {        this.helloWorld = helloWorld;    }      public HelloWorld getHelloWorld() {        return this.helloWorld;    }  }


StrutsHelloWorld
package hellospring.service.impl;import hellospring.service.HelloWorld;public class StrutsHelloWorld implements HelloWorld {      @Override    public void sayHello() {        System.out.println("Struts Say Hello!!");    }  }

SpringHelloWorld
package hellospring.service.impl;import hellospring.service.HelloWorld;public class SpringHelloWorld implements HelloWorld {      @Override    public void sayHello() {        System.out.println("Spring Say Hello!!");    }  }


4.运行




原创粉丝点击