Spring-context 实现Hello World

Spring-context 实现Hello World

本文作为Spring入门笔记,用Spring-context实现控制台的hello world

Spring简介

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。

项目结构

➜  main tree
.
├── java
│   └── hello
│       ├── impl
│       │   ├── SpringHelloWorld.java
│       │   └── StrutsHelloWorld.java
│       └── myspring
│           ├── HelloProgram.java
│           └── helloworld
│               ├── HelloWorld.java
│               └── HelloWorldService.java
└── resources
    └── beans.xml

6 directories, 6 files

POM文件

可以直接参考Spring官网

<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>fanghao</groupId>
    <artifactId>myspring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>myspring</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 Context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

    </dependencies>
</project>

一、定义接口

package hello.myspring.helloworld;

public interface HelloWorld {
    public void sayHello();
}

二、定义JavaBean

package hello.myspring.helloworld;

public class HelloWorldService {
    private HelloWorld helloWorld;

    public HelloWorldService() {
    }
    public void setHelloWorld(HelloWorld helloWorld){
        this.helloWorld = helloWorld;
    }

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }
}

三、定义实现类

package hello.impl;

import hello.myspring.helloworld.HelloWorld;

public class SpringHelloWorld implements HelloWorld {
    public void sayHello() {
        System.out.println("Spring Say Hello!!!");
    }
}
package hello.impl;

import hello.myspring.helloworld.HelloWorld;

public class StrutsHelloWorld implements HelloWorld {
    public void sayHello() {
        System.out.println("Struts Say Hello!!");
    }
}

四、定义Java Bean配置文件

beans.xml会搜索项目中的类,其中

<property name="helloWorld" ref="strutsHelloWorld"/>会调用引用类strutsHelloWorld的get和set方法,要保持命名一致(与属性名相同)

<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="hello.impl.SpringHelloWorld"/>
    <bean id="strutsHelloWorld"
          class="hello.impl.StrutsHelloWorld"/>

    <bean id="helloWorldService"
          class="hello.myspring.helloworld.HelloWorldService">
        <property name="helloWorld" ref="strutsHelloWorld"/>
    </bean>

</beans>

五、定义应用文件

读取beans.xml 文件来创建一个应用程序上下文对象

package hello.myspring;

import hello.myspring.helloworld.HelloWorld;
import hello.myspring.helloworld.HelloWorldService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloProgram {
    public static void main(String[] args){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");
        HelloWorldService service =
                (HelloWorldService) context.getBean("helloWorldService");
        HelloWorld hw = service.getHelloWorld();
        hw.sayHello();
    }
}

六、运行结果

四月 08, 2018 10:49:52 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46ee7fe8: startup date [Sun Apr 08 10:49:52 HKT 2018]; root of context hierarchy
四月 08, 2018 10:49:52 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
Struts Say Hello!!
posted @ 2018-04-08 10:56  潇雨危栏  阅读(211)  评论(0编辑  收藏  举报