【译】Spring 4 Hello World例子
前言
译文链接: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对象,就可以通过其
bean对象的方法执行一些操作。getBean
方法从spring容器中得到指定的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例子的更多相关文章
- [译]Spring构建微服务
此文为译文,原文地址 介绍 本文通过一个使用Spring.Spring Boot和Spring Cloud的小例子来说明如何构建微服务系统. 我们可以通过数个微服务组合成一个大型系统. 我们可以想象下 ...
- Spring Boot SOAP Webservice例子
前言 本文将学习如何利用Spring boot快速创建SOAP webservice服务: 虽然目前REST和微服务越来越流行,但是SOAP在某些情况下,仍然有它的用武之地: 在本篇 spring b ...
- c# spring aop的简单例子
刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...
- Spring Cloud Eureka Server例子程序
Spring-Cloud-Eureka-Server 及Client 例子程序 参考源代码:https://github.com/spring-cloud-samples/eureka 可以启动成功, ...
- (原创)Maven+Spring+CXF+Tomcat7 简单例子实现webservice
这个例子需要建三个Maven项目,其中一个为父项目,另外两个为子项目 首先,建立父项目testParent,选择quickstart: 输入项目名称和模块名称,然后创建: 然后建立子项目testInt ...
- spring的一个小例子(二)--解析前面的小例子
接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...
- Spring的一个入门例子
例子参考于:Spring系列教材 以及<轻量级JavaEE企业应用实战> Axe.class package com.how2java.bean; public class Axe { p ...
- spring的aop的例子
一个简单的Spring的AOP例子 2009-06-23 11:33:29| 分类: Spring | 标签: |举报 |字号大中小 订阅 package aop; /** * 目标对象的 ...
- 峰Spring4学习(6)spring AOP的应用例子
一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: p ...
随机推荐
- 游戏编程系列[1]--游戏编程中RPC协议的使用
RPC(Remote Procedure Call Protocol)--远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...
- springmvc注解事例
注解有简化 ,灵活性增强的功能 1.引入jar包 com.springsource.javax.validation-1.0.0.GA.jarcom.springsource.org.aopallia ...
- GitHub实战系列~4.把github里面的库克隆到指定目录+日常使用 2015-12-11
GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...
- CSS画图
The Shapes of CSS All of the below use only a single HTML element. Any kind of CSS goes, as long as ...
- 学习笔记之(console)
今天小颖在逛博客园时,发现一位帅锅写的有意思的Console小颖看了后,就自己敲了一遍嘻嘻,为了方便以后查看,小颖把它记录下来嘻嘻,有兴趣的小伙伴也可以自己试试哦. 格式占位符 作用 %s 字符串 % ...
- Beginners Guide To Web Development
Web Development Front End Development Back End Development
- JavaScript事件概览
JavaScript事件 JavaScript是单线程,在同一个时间点,不可能同时运行两个"控制线程". 事件句柄和事件对象 1.注册事件句柄 标准和非标准 var button= ...
- linux2.6 内存管理——逻辑地址转换为线性地址(逻辑地址、线性地址、物理地址、虚拟地址)
Linux系统中的物理存储空间和虚拟存储空间的地址范围分别都是从0x00000000到0xFFFFFFFF,共4GB,但物理存储空间与虚拟存储空间布局完全不同.Linux运行在虚拟存储空间,并负责把系 ...
- [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一)
[入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一) Date 周二 06 一月 2015 By 钟谢伟 Tags mvc4 / asp.net 示 ...
- ASP.NET Core 介绍
原文:Introduction to ASP.NET Core 作者:Daniel Roth.Rick Anderson.Shaun Luttin 翻译:江振宇(Kerry Jiang) 校对:许登洋 ...