Spring 的介绍和目标
1、 Spring介绍 打开Spring 官网查看对 Spring 的介绍和目标 http://www.springsource.org/about We believe that: · J2EE should be easier to use · It is best to program to interfaces, rather than classes. Spring reduces the complexity cost of using interfaces to zero. · JavaBeans offer a great way of configuring applications. · OO design is more important than any implementation technology, such as J2EE. · Checked exceptions are overused in Java. A platform shouldn't force you to catch exceptions you're unlikely to be able to recover from. · Testability is essential, and a platform such as Spring should help make your code easier to test. Our philosophy is summarized in Expert One-on-One J2EE Design and Development by Rod Johnson. We aim that: · Spring should be a pleasure to use · Your application code should not depend on Spring APIs · Spring should not compete with good existing solutions, but should foster integration.(For example, JDO, Toplink, and Hibernate are great O/R mapping solutions. We don't need to develop another one.) 首先Spring 的目的是为了让 Java 的企业开发变得更简单容易,提倡面向接口编程,依赖注入,面向对象,使用 POJO 编程,使单元测试更加简单。 Spring 的目标不是具体的解决方案,而是一个平台,整合现有的很多开源框架。 2、 使用Spring 的好处 解低组件间的偶合度 面向接口编程 使用Spring 提供的其它功能,如事务管理、 JMS 、 MVC ,数据持久服务等 提供单例模式魂魄 提供AOP 支持 提供众多辅助类 提供跟其它主流框架集合支持 3、 依赖注入(Dependency Injection) 在运行期由外部容器动态的将依赖对象注入到组件中,也就是一个对象对另一个对象的引用,不在自己内部实现,而是交给第三方实现完后再注入到该对象中。 4、 重量级与轻量级概念 框架默认提供的服务多,可以理解为重理级的,提供的服务单一而简单为轻量级的 5、 搭建基于Maven 和 Spring3.0 的环境 5.1 、 新建 Maven Project 选择File -> New -> Other ,在 New 窗口中选择 Maven -> Maven Project 。点击 next ,如下图: 5.2 、 选择默认工作空间,点击 next ,选择类目类型为 maven-archetype-webapp : 5.3 、 点击 next ,填写相应的 Group ID 和 Artifact ID ,点 next ,建立好的项目结构如下: 把项目的JDK 环境改成 1.6 ,如果在此步骤出现红X ,则跳到 4.6 把 java 版本也改成 1.6 。 5.4 、 添加 Source 文件夹 添加 src/main/java , src/test/java , src/test/resources三个文件夹 ( Source Folder ) 5.5 、 更新 class 路径 右键项目属性,Java Build Path -> Source ,下面有四个文件夹: src/main/java 、 src/main/resources 、 src/test/java 、 src/test/resources : 勾上 Allow output folders for source folders , 双击每个文件夹的Output folder ,选择路径 分别为: src/main/java和 src/main/resources : target/classes Src/test/java和 src/test/resources : target/test-class 5.6 、 把项目变成 Dynamic Web 项目 5.7、 设置部署程序集 Web Deployment Assembly 如下: 6、 配置pom.xml 文件,加入 spring 依赖 com.springsource.repository.maven.release http://maven.springframework.org/release/ false 7、 创建spring 的配置文件 在src/main/resources 文件夹中创建文件 applicationContext.xml ,从 spring 文档中找到模板内容添加进去,如下:
8、 创建测试代码 8.1 、 在 src/main/java 中创建接口 com.chenzehe.spring.service.HelloWorld ,声明 sayHelloWorld() 方法,如下: package com.chenzehe.spring.service; public interface HelloWorld { void sayHelloWorld(); } 8.2 、 创建上面接口的实现类 com.chenzehe.spring.service.impl.HelloWorldImpl ,如下: package com.chenzehe.spring.service.impl; import com.chenzehe.spring.service.HelloWorld; public class HelloWorldImpl implements HelloWorld { public void sayHelloWorld(){ System.out.println("Hello World!"); } } 8.3 、 在 spring 配置文件 applicationContext.xml 文件中注册该 bean ,如:
8.4 、使用 junit 测试 在pom.xml 文件中把 junit 依赖改到 4.8.1 版本,在 src/main/test 中创建测试类 com.chenzehe.spring.test.junit.HelloWorldTest ,如下: package com.chenzehe.spring.test.junit; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.chenzehe.spring.service.HelloWorld; public class HelloWorldTest { @Test public void instanceApplicationContext() { ApplicationContext cxt = new ClassPathXmlApplicationContext( "applicationContext.xml"); HelloWorld helloWorld = (HelloWorld) cxt.getBean("helloWorld"); helloWorld.sayHelloWorld(); } } 运行该测试类输出HelloWorld
Spring 的介绍和目标的更多相关文章
- Spring Batch 中文参考文档 V3.0.6 - 1 Spring Batch介绍
1 Spring Batch介绍 企业领域中许多应用系统需要采用批处理的方式在特定环境中运行业务操作任务.这种业务作业包括自动化,大量信息的复杂操作,他们不需要人工干预,并能高效运行.这些典型作业包括 ...
- Spring 框架介绍
Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Control – I ...
- Spring AOP介绍与使用
Spring AOP介绍与使用 AOP:Aspect Oriented Programming 面向切面编程 OOP:Object Oriented Programming 面向对象编程 面向切面 ...
- [翻译]Spring框架参考文档(V4.3.3)-第二章Spring框架介绍 2.1 2.2 翻译--2.3待继续
英文链接:http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/overview.ht ...
- Spring的介绍与搭建
一.Spring的介绍 二.Spring的搭建 (1)导包 (2)创建一个对象 (3)书写配置注册对象到容器 (4)代码测试
- Spring Shell介绍
最近开发中在下遇到了spring-shell开发工具的项目,现在整理了相关文章,以供大家学习 本博客相关的文章均是在Spring Shell 1.2.0的基础上建立 Spring Shell介绍 ...
- Spring DevTools 介绍
Spring DevTools 介绍 Spring Boot包括一组额外的工具,可以使应用程序开发体验更加愉快. spring-boot-devtools模块可以包含在任何项目中,它可以节省大量的时间 ...
- 第1章—Spring之旅—Spring模块介绍
Spring模块介绍 Spring7大核心模块: 核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.S ...
- Spring Security 介绍与Demo
一.Spring Security 介绍 Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块的默认技术选型.我们仅需引入spring-boot-s ...
随机推荐
- 0.2:Game and Art
文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本系列原更新于作者的github博客,这里给出链接. 通过上一节的学习,我们对游戏美术和游戏开发已经有了比较基本的了解.那么,该 ...
- kubernetes endpoint一会消失一会出现的问题剖析
问题现象 发现某个service的后端endpoint一会显示有后端,一会显示没有.显示没有后端,意味着后端的address被判定为notready. endpoint不正常的时候: [root@lo ...
- 使用Java或 JavaScript获取 方括号中的内容
1.使用Java获取方括号中的内容 String str = "[你]们,[我]们,[他]们,都要[好好学习,天天敲代码]"; Pattern p = Pattern.compil ...
- Windows Server 2008 R2 下载地址
以下资源均来自微软 MSDN,是原汁原味的原版系统资源,值得系统爱好者收藏.以下多数链接是 ed2k 链接,推荐使用国外开源的 eMule 下载,亦可使用迅雷,但使用 eMule 更有利于共享资源. ...
- 没有显示器、网线、路由器,编辑TF卡连接树莓派
只有电脑,连接树莓派的方法 电脑新建热点 打开TF卡,在根目录新建文件wpa_supplicant.conf,内容如下 country=GB ctrl_interface=DIR=/var/run/w ...
- C# 说说lock到底锁谁?(2)
摘要 今天在园子里面有园友反馈关于[C#基础]说说lock到底锁谁?文章中lock(this)的问题.后来针对文章中的例子,仔细想了一下,确实不准确,才有了这篇文章的补充,已经对文章中的demo进行修 ...
- 文件编码检测.ZC一些资料(包含java的)
1.IMultiLanguage3 或者 IMultiLanguage2 1.1.怎么判断XML 的编码格式(UTF-8或GB2312等)-CSDN论坛.html(https://bbs.csdn.n ...
- Django 编写模板并渲染的示例
>>> from django.template import Template, Context >>> raw_template = ""& ...
- 使用Gitlab实现自动化部署与持续集成
Gitlab-Ci运行原理: 由以下两个模块组成gitlab-ci servergitlab-ci-runner其中,gitlab-ci server负责调度.触发Runner,以及获取返回结果. 而 ...
- 两道面试题,带你解析Java类加载机制
文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...