前言

译文链接:http://websystique.com/spring/spring-4-hello-world-example-annotation-tutorial-full-example/

这个教程将展示一个基于Spring注解配置的Spring 4 Hello world例子,解释Spring 4的基本概念和使用方法。

同样也会提供基于XML配置的示例作为两者的一个比较,我们将创建一个基于maven工程,使用spring版本为4.0.6。

涉及的相关技术及开发工具

  • Spring 4.0.6.RELEASE
  • Maven 3
  • JDK 1.6
  • Eclipse JUNO Service Release 2

工程结构目录

如下是本工程的最终结构目录:

如果想要了解如何创建一个maven工程,请参考以下链接:Maven tutorial。里面包含了详细的步骤来说明如何创建maven工程。

那么,接下来,就让我们在以上目录结构上添加具体内容吧。

步骤一:在maven的pom.xml里添加spring依赖

我们可以通过pom.xml给maven工程添加所有依赖,如下:

<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.websystique.spring</groupId>
<artifactId>Spring4HelloWorldExample</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>Spring4HelloWorldExample</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.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build> </project>

对于这个示例,我们只需要添加Spring core 和 Spring context依赖,其中spring注解是在spring-context里定义的。

步骤二:创建POJO类

Spring提倡低耦合和基于接口编程。

所以,我们首先创建一个POJO接口和它的实现类,这个POJO将作为一个spring bean。

package com.websystique.spring.domain;

public interface HelloWorld {

    void sayHello(String name);
}
package com.websystique.spring.domain;

public class HelloWorldImpl implements HelloWorld{

    public void sayHello(String name) {
System.out.println("Hello "+name);
} }

步骤三:创建Spring配置文件

Spring配置类包含工程中要用到的一些bean的定义。@Configuration注解声明这个类是一个Spring配置类,该类可以包含@Bean注解的方法,该方法创建bean交给spring容器管理。

package com.websystique.spring.configuration;

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 a sample HelloWorld Bean")
public HelloWorld helloWorld() {
return new HelloWorldImpl();
} }

@Description是Spring 4引入的新注解,用于描述bean的定义

以上基于注解的配置与如下的Spring XML配置达到的效果一样(我们把这个文件命名为helloworld-config.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-4.0.xsd"> <bean id="helloWorldBean" class="com.websystique.spring.domain.HelloWorldImpl"> </beans>

步骤四:创建main方法执行该程序

package com.websystique.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext; import com.websystique.spring.configuration.HelloWorldConfig;
import com.websystique.spring.domain.HelloWorld; public class AppMain { public static void main(String args[]) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld bean = (HelloWorld) context.getBean("helloWorldBean");
bean.sayHello("Spring 4");
context.close();
} }

AnnotationConfigApplicationContext以我们的配置类(用@Configuration标注)作为入参创建spring应用上下文环境,在程序运行时注册所有在配置类中定义的bean。一旦我们创建了AnnotationConfigApplicationContext对象,就可以通过其getBean方法从spring容器中得到指定的bean对象,并且可以调用该bean对象的方法执行一些操作。

HelloWorld bean = (HelloWorld) context.getBean("helloWorldBean");
bean.sayHello("Spring 4");

运行以上程序,会得到如下结果:

Hello Spring 4

如果使用基于XML的配置,上面的main方法应该这样写:

package com.websystique.spring;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.websystique.spring.domain.HelloWorld; public class AppMain { public static void main(String args[]) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("helloworld-config.xml");
HelloWorld bean = (HelloWorld) context.getBean("helloWorldBean");
bean.sayHello("Spring 4");
context.close(); } }

其中helloworld-config.xml文件是我们在步骤三中创建的,位于工程classpath下(如/src/main/resources)。

结语

下篇文章,我们将会看到一个bean的自动检测例子,该特性基于注解,可以自动找到应用环境中的所有bean,而不需要在配置类中一个个声明它们。

本例源码

http://websystique.com/?smd_process_download=1&download_id=778

【译】Spring 4 Hello World例子的更多相关文章

  1. [译]Spring构建微服务

    此文为译文,原文地址 介绍 本文通过一个使用Spring.Spring Boot和Spring Cloud的小例子来说明如何构建微服务系统. 我们可以通过数个微服务组合成一个大型系统. 我们可以想象下 ...

  2. Spring Boot SOAP Webservice例子

    前言 本文将学习如何利用Spring boot快速创建SOAP webservice服务: 虽然目前REST和微服务越来越流行,但是SOAP在某些情况下,仍然有它的用武之地: 在本篇 spring b ...

  3. c# spring aop的简单例子

    刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...

  4. Spring Cloud Eureka Server例子程序

    Spring-Cloud-Eureka-Server 及Client 例子程序 参考源代码:https://github.com/spring-cloud-samples/eureka 可以启动成功, ...

  5. (原创)Maven+Spring+CXF+Tomcat7 简单例子实现webservice

    这个例子需要建三个Maven项目,其中一个为父项目,另外两个为子项目 首先,建立父项目testParent,选择quickstart: 输入项目名称和模块名称,然后创建: 然后建立子项目testInt ...

  6. spring的一个小例子(二)--解析前面的小例子

    接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...

  7. Spring的一个入门例子

    例子参考于:Spring系列教材 以及<轻量级JavaEE企业应用实战> Axe.class package com.how2java.bean; public class Axe { p ...

  8. spring的aop的例子

    一个简单的Spring的AOP例子 2009-06-23 11:33:29|  分类: Spring |  标签: |举报 |字号大中小 订阅     package aop; /** * 目标对象的 ...

  9. 峰Spring4学习(6)spring AOP的应用例子

    一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: p ...

随机推荐

  1. .NET中使用Redis

    Redis是一个用的比较广泛的Key/Value的内存数据库,新浪微博.Github.StackOverflow 等大型应用中都用其作为缓存,Redis的官网为http://redis.io/. 最近 ...

  2. ASP.NET MVC 描述类型(一)

    ASP.NET MVC 描述类型(一) 前言 在前面的好多篇幅中都有提到过ControllerDescriptor类型,并且在ASP.NET MVC 过滤器(一)篇幅中简单的描述过,今天我们就来讲一下 ...

  3. Entity Framework 6 Recipes 2nd Edition(13-8)译 -> 把昂贵的属性移到其它实体

    问题 你想把一个昂贵的属性移到另一个实体,这样你就可以延迟加载当前这个实体.对于一个加载昂贵的而且很少用到的属性尤其有用. 解决方案 模型和上一节(Recipes 13-7)的一致,如Figure13 ...

  4. 气泡 弹出 bootstrap-popover的配置与灵活应用

    <script src="/assets/addons/bootstrap-select/bootstrap-select.min.js"></script> ...

  5. VS 2015相当不错的功能:C#交互窗口

    按照惯例,老周是先吹牛后讲正事.今天就给大伙吹吹这个事. 有网友不知道是不是昨晚喝高了,居然研究起老周来了.实话告诉你,老周没什么好研究的,老周又不是编译器,老周只是一个游离于大善大恶之间的平凡人,说 ...

  6. git did not exit cleanly

    exit code 1 1.鼠标右键 -> TortoiseGit -> Settings -> Network 2.SSH client was pointing to C:\Pr ...

  7. 锋利的jQuery--表单等(读书笔记三)

    1.input元素中的多选的,单选,不选,涉及属性checked   2.select元素中的选中,涉及selected   3.表单的验证   4.表格隔行变色 $("tr:odd&quo ...

  8. JavaScript -Array.form方法

    Array.from方法可以把一个类数组或者课遍历对象转换为一个正真的数组 语法 Array.from(arrayLike[, mapFn[, thisArg]]) 参数 arrayLike 想要转换 ...

  9. 4. ValueStack 和 OGNL

    1. 属性哪来的 当我们通过Action处理完用户请求以后,可以直接在页面中获取到 action 的属性值. 如果我们在页面中尝试遍历四个域中的属性,会发现域中并没有username之类的Action ...

  10. Swift 必备开发库 (高级篇) (转)

    1.CryptoSwift swift加密库, 支持md5,sha1,sha224,sha256... github地址: https://github.com/krzyzanowskim/Crypt ...