第一个Spring项目

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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,907评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,126评论 6 342
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan阅读 9,763评论 2 7
  • 什么是Spring Spring是一个开源的Java EE开发框架。Spring框架的核心功能可以应用在任何Jav...
    jemmm阅读 16,697评论 1 133
  • 在我眼中我的妈妈是一个善良有温顺的一个妈妈,他每次从广州回放假回来的时候他都会给我带很多很多好吃的,每次都...
    张博韬_9a1b阅读 2,860评论 0 0

友情链接更多精彩内容