第一個Spring項目

1.創(chuàng)建項目

File/new/Other

2.申明Spring的基礎(chǔ)庫

這是 Spring的 HelloWorld 例子,所以我們只使用基本的Spring庫(核心)。打開pom.xml文件來將使用的庫聲明:
pom.xml 使用以下內(nèi)容重新覆蓋原上面的內(nèi)容。

<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.工程代碼

項目目錄結(jié)構(gòu)如下:

  • 接口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 文件來創(chuàng)建一個應(yīng)用程序上下文對象
ApplicationContext context = newClassPathXmlApplicationContext("beans.xml");

oC容器中,其作用是作為第三種角色,它的任務(wù)是創(chuàng)建 beans.xml 文件中聲明的 Java Bean 對象。并通過setter方法注入依賴
在這個例子中,HelloWorldService 是一個 java bean 注入依賴。

<bean id="helloWorldService"
    class="com.yibai.tutorial.spring.helloworld.HelloWorldService">
    
    <!-- Call: helloWorldService.setHelloWorld(springHelloWorld) -->  //再這里調(diào)用set方法
    <propertyname="helloWorld"ref="springHelloWorld"/>
 
</bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,562評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,493評論 2 7
  • 什么是Spring Spring是一個開源的Java EE開發(fā)框架。Spring框架的核心功能可以應(yīng)用在任何Jav...
    jemmm閱讀 16,772評論 1 133
  • 在我眼中我的媽媽是一個善良有溫順的一個媽媽,他每次從廣州回放假回來的時候他都會給我?guī)Ш芏嗪芏嗪贸缘模看味?..
    張博韜_9a1b閱讀 409評論 0 0

友情鏈接更多精彩內(nèi)容