美文网首页
第一个Spring项目

第一个Spring项目

作者: stutterr | 来源:发表于2017-05-26 23:24 被阅读710次

1.创建项目

File/new/Other

2.申明Spring的基础库

这是 Spring的 HelloWorld 例子,所以我们只使用基本的Spring库(核心)。打开pom.xml文件来将使用的库声明:
pom.xml 使用以下内容重新覆盖原上面的内容。

<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.yiibai</groupId>
  <artifactId>HelloSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  
      <dependencies>
 
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
    </dependencies>
  
</project>

3.工程代码

项目目录结构如下:

  • 接口HelloWorld.java
package com.yibai.tutorial.spring.helloworld;

public interface HelloWorld {

    public void sayHello();
}
  • HelloWorldService
package com.yibai.tutorial.spring.helloworld;

public class HelloWorldService {

    
    private HelloWorld helloWorld;
    public HelloWorldService(){
        
    }
    
    public void setHelloWorld(HelloWorld helloWorld){
        this.helloWorld=helloWorld;
        
    }
    
    public HelloWorld getHelloWorld() {
        return this.helloWorld;
    }
}
  • SpringHelloWorld.java
package com.yibai.tutorial.spring.helloworld.impl;

import com.yibai.tutorial.spring.helloworld.HelloWorld;

public class SpringHelloWorld implements HelloWorld {

    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Spring Say Hello!!");
    }
}
  • StrutsHelloWorld.java
package com.yibai.tutorial.spring.helloworld.impl;

import com.yibai.tutorial.spring.helloworld.HelloWorld;

public class StrutsHelloWorld implements HelloWorld{

    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Struts Say Hello!!");
    }
}
  • HelloProgram.java
package com.yibai.tutorila.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yibai.tutorial.spring.helloworld.HelloWorld;
import com.yibai.tutorial.spring.helloworld.HelloWorldService;

public class HelleProgram {
    public static void main(String[] args) {
        
        ApplicationContext context=
                new ClassPathXmlApplicationContext("beans.xml");
        
        HelloWorldService service=
                (HelloWorldService) context.getBean("helloWorldService");
        
        HelloWorld hw=service.getHelloWorld();
        
        hw.sayHello();
    }
}
  • beans.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   
    <bean id="springHelloWorld"
        class="com.yibai.tutorial.spring.helloworld.impl.SpringHelloWorld"></bean>
    <bean id="strutsHelloWorld"
        class="com.yibai.tutorial.spring.helloworld.impl.StrutsHelloWorld"></bean>
  
  
    <bean id="helloWorldService"
        class="com.yibai.tutorial.spring.helloworld.HelloWorldService">
        <property name="helloWorld" ref="springHelloWorld"/>
    </bean>
</beans>

4.运行示例

之后打开beans.xml 修改如下

<!-- Original -->
<beanid="helloWorldService"
    class="com.yibai.tutorial.spring.helloworld.HelloWorldService">
   <propertyname="helloWorld"ref="springHelloWorld"/>
</bean>
 
<!-- Change to: -->
<bean id="helloWorldService"
    class="com.yibai.tutorial.spring.helloworld.HelloWorldService">
   <property name="helloWorld" ref="strutsHelloWorld"/>
</bean>

重新运行

5.工作原理

可以通过读取beans.xml 文件来创建一个应用程序上下文对象
ApplicationContext context = newClassPathXmlApplicationContext("beans.xml");

oC容器中,其作用是作为第三种角色,它的任务是创建 beans.xml 文件中声明的 Java Bean 对象。并通过setter方法注入依赖
在这个例子中,HelloWorldService 是一个 java bean 注入依赖。

<bean id="helloWorldService"
    class="com.yibai.tutorial.spring.helloworld.HelloWorldService">
    
    <!-- Call: helloWorldService.setHelloWorld(springHelloWorld) -->  //再这里调用set方法
    <propertyname="helloWorld"ref="springHelloWorld"/>
 
</bean>

相关文章

网友评论

      本文标题:第一个Spring项目

      本文链接:https://www.haomeiwen.com/subject/dicffxtx.html