Spring 基础:HelloWorld 实例

本教程显示了使用Spring Annotation配置的Spring 4 Hello world Annotation示例,解释了Spring 4的基本概念和用法。

环境:

Spring 4

Maven 3

JDK 1.8

IDEA 2018


1.Maven提供Spring依赖项

基于Maven的项目,我们将通过Maven 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.learn.spring</groupId>

    <artifactId>learn-spring4</artifactId>

    <version>1.0.0</version>

    <packaging>jar</packaging>



    <name>learn-spring4</name>



    <properties>

        <springframework.version>4.0.6.RELEASE</springframework.version>

    </properties>



    <dependencies>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>${springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>${springframework.version}</version>

        </dependency>

    </dependencies>

    <build>

        <pluginManagement>

            <plugins>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-compiler-plugin</artifactId>

                    <version>3.7.0</version>

                    <configuration>

                        <source>1.8</source>

                        <target>1.8</target>

                    </configuration>

                </plugin>

            </plugins>

        </pluginManagement>

    </build>



</project>


我们只需要Spring核心和Spring上下文依赖关系即可使用此示例。


2.创建实体类

Spring促进了接口的松耦合。创建一个接口及其实现,将充当spring bean。

package com.learn.spring;



public interface HelloWorldInterface {



    void sayHello(String name);

}


package com.learn.spring;



public class HelloWorldImpl implements HelloWorldInterface {

    @Override

    public void sayHello(String name) {

        System.out.println("Hello " + name);

    }

}


3.创建Spring配置类

Spring配置类包含应用程序需要的bean定义。 @Configuration将类声明为Spring Configuration类,其中可以包含@Bean注释方法,这些方法生成由Spring容器管理的Bean。

package com.learn.spring;



import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Description;



@Configuration

public class HelloWorldConfig {



    @Bean(name = "helloWorldBean")

    @Description("This is SpringMain hello world.")

    public HelloWorldInterface getHelloWorld() {

        return new HelloWorldImpl();

    }

}


@Description是Spring 4中引入的新注释,用于提供bean的文本描述。


4.运行Java应用程序

package com.learn.spring;



import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.Profile;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class SpringMain {

    public static void main(String[] args) {

        // Annocation

        AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(HelloWorldConfig.class);

        HelloWorldInterface helloWorldBean = (HelloWorldInterface) applicationContext.getBean("helloWorldBean");

        helloWorldBean.sayHello("spring");

        applicationContext.close();

    }

}


AnnotationConfigApplicationContext创建一个Spring Application Context,接受我们的配置类(以@Configuration注释),并在Spring运行时中注册由配置类生成的所有bean。一旦配置了上下文,我们就可以使用getBean方法从Spring应用程序上下文中获取特定的bean并对其执行一些操作。

程序运行输出:

Hello spring

 

 

展开阅读全文