Spring的Hello World工程

通过Spring的Hello World工程研究以下几个点:

0、如何创建工程及引入依赖。

1、通过Spring的beans.xml实现依赖注入,动态创建实例。

2、了解Spring的工作原理。

具体实现步骤:

1、创建工程

创建工程有两种,命令行和IDE(Eclipse),如果通过命令行创建最后还是要引入Eclipse中去开发,所以下面的创建主要是通过Eclipse去实现。

提示:由于是基于Application的测试,所以这里选择quickstart。

2、配置POM,引入Spring的依赖包

打开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.jsoft.test</groupId>
    <artifactId>HelloSpring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>HelloSpring</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        
        <!-- 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.jsoft.test.hellospring.helloworld;

public interface HelloWorld {
    public void sayHello();
}

SpringHelloWorld.java(接口实现):

package com.jsoft.test.hellospring.helloworld.impl;

import com.jsoft.test.hellospring.helloworld.HelloWorld;

public class SpringHelloWorld implements HelloWorld {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Spring say HelloWorld!");
    }

}

StrutsHelloWorld.java(接口实现):

package com.jsoft.test.hellospring.helloworld.impl;

import com.jsoft.test.hellospring.helloworld.HelloWorld;

public class StrutsHelloWorld implements HelloWorld {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Struts say HelloWorld!");
    }

}

提示:在Java开发中,通常将后台分成几层,常见的是三层mvc:model、view、controller,模型视图控制层三层,而impl通常处于controller层的service下,用来存放接口的实现类,impl的全称为implement,表示实现的意思。所以这里的包名叫做impl。

HelloWorldService.java(创建实例):

package com.jsoft.test.hellospring.helloworld;

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

beans.xml:

<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.xsd">
  
    <bean id="springHelloWorld" class="com.jsoft.test.hellospring.helloworld.impl.SpringHelloWorld"></bean>
    <bean id="strutsHelloWorld" class="com.jsoft.test.hellospring.helloworld.impl.StrutsHelloWorld"></bean>
 
    <bean id="helloWorldService" class="com.jsoft.test.hellospring.helloworld.HelloWorldService">
        <property name="HelloWorld" ref="strutsHelloWorld"/>
    </bean>
  
</beans>

注意:新建beans.xml时,是放置在src/main/resources目录下,如果没有这个目录需要自行新建。

App.java(程序入口):

package com.jsoft.test.hellospring;

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

import com.jsoft.test.hellospring.helloworld.HelloWorld;
import com.jsoft.test.hellospring.helloworld.HelloWorldService;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        //IoC获取beans的上下文
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //通过上下文获取beans创建的Service,此时已经注入了是创建哪个的接口实现
        HelloWorldService helloWorldService = (HelloWorldService)context.getBean("helloWorldService");
        //Service调用统一接口方法获取注入的接口实现
        HelloWorld helloWorld = helloWorldService.getHelloWorld();
        //输出接口实现的内容
        helloWorld.sayHello();
    }
}

4、运行工程

【项目右键】->【Run As】->【Java Application】

输出如下:

5、工作原理总结:

1、beans.xml是Spring的一切,通过beans.xml的依赖注入去实例化类,已经传入指定的参数。

2、可以看出在App.java类中的main方法,先是获取beans实现上下文,然后通过上下文获取指定的Bean,而此时这个Bean已经在beans.xml中注入了一个StrutsHelloWorld的类实现。

3、在beans.xml中,通过<property name="HelloWorld" ref="strutsHelloWorld"/>属性去注入HelloWorldService.java中的setHelloWorld方法。经过测试,HelloWorld的开头字母大小写不区分。

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test1/HelloSpring

以上参考:http://www.yiibai.com/spring/spring-tutorial-for-beginners.html

原文地址:https://www.cnblogs.com/EasonJim/p/6874821.html