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
依赖
<repositories>
<repository>
<id>com.springsource.repository.maven.release</id>
<url>http://maven.springframework.org/release/</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
7、
创建spring
的配置文件
在src/main/resources
文件夹中创建文件
applicationContext.xml
,从
spring
文档中找到模板内容添加进去,如下:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
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
,如:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<bean id="helloWorld" class="com.chenzehe.spring.service.impl.HelloWorldImpl"></bean>
</beans>
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
相关推荐
本教程从Extjs5的开发环境搭建开始,讲解了Extjs5的项目结构(包括核心文件的作用),Extjs类的一些基本概念,布局、事件、MVVM和MVC架构、路由器,数据绑定等的概念和实际使用,同时讲解了开发中常用的extjs UI...
5、Spring MVC 三、课程目标 1、深入理解Maven工作原理 2、熟练掌握Maven基础应用 3、熟练掌握Maven企业级开发 四、课程大纲 第1课 Maven 概述及安装 第2课 使用Maven构建Java项目 第3课 理解Maven核心概念(一) ...
通过学习,读者可以了解Spring框架的核心概念、搭建开发环境、掌握基本用法和进阶技巧,最终实践项目开发。 其他说明:本文以幽默、专业化的语言讲解Spring框架,让读者在轻松愉快的氛围中学习。文章涵盖了Spring...
Spring MVC 知识点总结,很全,包括概念、框架、框架具体流程、应用、组件、环境搭建、开发步骤、注解、数据校验等。
介绍:spring框架的概述以及spring中基于XML的IOC配置。 主要内容: 1、spring的概述 spring是什么 spring的两大核心 ... spring中基于XML的IOC环境搭建 4、依赖注入(Dependency Injection)
对于MVC3、Spring.net、NHibernate、...MVC的概念我个人的理解就是一种约定俗成,更深层次的理解还得各位看其他文章和多做练习来加深理解,在这个框架中,我们主要是用到Controller去后台取数,然后呈现到View上面;
微服务概念改变着软件开发领域,传统的开源框架结构开发,由于其繁琐的配置流程,复杂的设置行为,为项目的开发增加了繁重的工作量,微服务致力于解决除业务逻辑以外的开发工作量的精简与废除,集约化的改善开发环境和开发...
这次整合我分了2个配置文件,分别是spring-mybatis.xml,包含spring和mybatis的配置文件,还有个是spring-mvc的配置文件,此外有2个资源文件:jdbc.propertis和log4j.properties。完整目录结构如下(最后附上源码...
基于Spring + Spring MVC + MyBatis的图书馆管理系统,使用Maven进行包管理。主要功能包括:图书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。 项目演示 演示地址 <——点击跳转 ...
关系映射(ORM)层称为Groovy Server Pages (GSP)的表现层技术基于Spring MVC的控制器层构建于Gant 上的命令行脚本运行环境内置Jetty服务器,不用重新启动服务器就可以进行重新加载利用内置的Spring 容器实现依赖注入...
Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和Hibernate等Java...
关系映射(ORM)层称为Groovy Server Pages (GSP)的表现层技术基于Spring MVC的控制器层构建于Gant 上的命令行脚本运行环境内置Jetty服务器,不用重新启动服务器就可以进行重新加载利用内置的Spring 容器实现依赖注入...
Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和Hibernate等Java...
Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和Hibernate等Java...
Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和Hibernate等Java...
Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和Hibernate等Java...
基于Spring + Spring MVC + MyBatis的图书馆管理系统,使用Maven进行包管理。主要功能包括:图书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。 环境配置 需要提前配置好Maven环境,...
分分钟部署一个Hexo环境 各种配置详解 开始写作吧 开发者指南 git - 简明指南 Jersey-2.x用户指南 REST 实战 Java Servlet 3.1 规范 MyBatis中文指南 Apache Shiro 用户指南 Spring Boot参考指南 Netty4 用户指南
全书分为6篇共21章内容,第一篇为Java Web基础篇,介绍了执行环境和开发环境的搭建及JSP的语法基础。第二篇为JSP 2.0开发篇,介绍了JSP指令元素、JSP动作元素、JSP内置对象、JSP和JavaBean等内容。第三篇为Servlet...