Spring的一个最大的目的就是使J2EE开发更加容易。同时,Spring之所以与Struts、Hibernate等单层框架不同,是因为Spring致力于提供一个以统一的、高效的方式构造整个应用,并且可以将单层框架以最佳的组合揉和在一起建立一个连贯的体系。可以说Spring是一个提供了更完善开发环境的一个框架,可以为POJO(Plain Old Java Object)对象提供企业级的服务。
Spring特性与注解
1. Spring特性IOC和AOP不明白的,可以看下这里
2. Spring注解理解不太明白,可以看下这里Spring注解 说的通俗易懂,很好理解。
Spring简介
Spring为JavaEE开发提供了一个轻量级的解决方案,主要表现为,
- IOC(或者叫做DI)的核心机制,提供了bean工厂(Spring容器),降低了业务对象替换的复杂性,提高了组件之间的解耦。
- AOP的将一些通用任务,如安全、事务、日志等集中进行管理,提高了复用性和管理的便捷性
- ORM和DAO提供了与第三方持久层框架的良好整合,简化了底层数据访问。
- 提供了优秀的Web MVC框架。
可以说Spring是贯穿表现层、业务层、持久层,为javaEE提供一站式解决方案的框架。此外,使用Spring还有如下好处,
- 低侵入设计,代码污染极低。
- 基于Spring框架的应用,可以独立于各种应用服务器,实现 write once, run anywhere,
- Spring可以与第三方框架良好整合(如ORM,DAO等模块与其他框架整合),但同时Spring提供了高度开放性,应用不会被强制依赖Spring,开发者可以自由选择Spring的部分或者全部。
Spring并不局限于中间层,而是为系统各层都提供了企业级解决方案(例如IOC可以使用Spring框架可以带来诸多好处,例如进行数据库事务处理,远程调用,JMS消息处理,JMX操作处理,而这些处理都不需要开发人员直接使用相关API(JDBC, JMX, JMS 等)
创建Web项目
第一种: 创建maven web工程,详细可见这里,不用自己再去下载jar包,只要在pom.xml中设置要依赖就好了。(推荐)
<properties>
<spring.version>4.3.7.RELEASE </spring.version>
</properties>
<dependencies>
<dependency>
<groupId> org.springframework</groupId>
<artifactId> spring-context</artifactId>
<version> ${spring.version}</version>
</dependency>
<dependency>
<groupId> org.springframework</groupId>
<artifactId> spring-core</artifactId>
<version> ${spring.version}</version>
</dependency>
<dependency>
<groupId> org.springframework</groupId>
<artifactId> spring-beans</artifactId>
<version> ${spring.version}</version>
</dependency>
<dependency>
<groupId> org.springframework</groupId>
<artifactId> spring-web</artifactId>
<version> ${spring.version}</version>
</dependency>
<dependency>
<groupId> org.springframework</groupId>
<artifactId> spring-webmvc </artifactId>
<version> ${spring.version}</version>
</dependency>
</dependencies>
第二种:如果你觉着上面创建 maven web工程麻烦, 也可以直接创建一个Dynamic Web Project工程,只是这里需要去下载Spring需要的jar包。
1. eclipse中点击File–>New–>other 如下图:
2. 点击next
3. 按照上图设置,点击 Finish 完成
4. 通过观察项目结构可以看出,新建的动态web项目缺少web.xml文件,这里我们新建一个
5. 至此,项目创建完毕,接下来,开始搭建框架
Spring框架搭建
1.首先配置web.xml文件
<web-app xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns="http://java.sun/xml/ns/javaee"
xsi:schemaLocation="http://java.sun/xml/ns/javaee http://java.sun/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!--设置转发-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--加载配置文件-->
<param-value> classpath:applicationContext.xml</param-value>
</init-param>
<!--标记容器是否在启动的时候就加载这个servlet。
当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;
当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
正数的值越小,启动该servlet的优先级越高。-->
<load-on-startup> 1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!--接受所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.配置applicationContext.xml文件
<beans xmlns= "http://www.springframework/schema/beans"
xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns:context="http://www.springframework/schema/context"
xmlns:mvc="http://www.springframework/schema/mvc"
xsi:schemaLocation="http://www.springframework/schema/beans
http://www.springframework/schema/beans/spring-beans.xsd
http://www.springframework/schema/context http://www.springframework/schema/context/spring-context.xsd
http://www.springframework/schema/mvc http://www.springframework/schema/mvc/spring-mvc.xsd ">
<!--这里可以去掉,因为下面自动扫描包的代码,就包含了该行的功能-->
<context:annotation-config/>
<!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
<context:component-scan base-package="com.zds"></context:component-scan>
</beans>
- 因为我们使用注解,所以没有Bean配置,只有一个扫描包的设置
- 关于
<context:annotation-config/>
的介绍不明白的,可以点击这里 - 这里的applicationContext.xml文件存放到 WEB-INF/classes/文件夹下,如果没有该文件夹,则新建
3.新建Controller文件
package com.zds;
/**
* @author zds
* @date 2018年3月6日
*/
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "hello", method = RequestMethod.GET)
@ResponseBody
public String helloWorld(@RequestParam("user") String userName) {
String string = "";
string.split(",");
return "Hello " + userName + " !";
}
}
4.把所需要的jar包放入 WEB-INF/lib文件夹中,这些jar包,和这个搭建的项目我都放到这里,大家如果有兴趣可以去下载。(特简单的东西,没想要下载积分,但是CSDN没有免费下载。。没办法,大家可以看看这篇文档,自己配置也行)
5.到这里配置完成, eclipse 中吧项目添加到tomcat中,启动,浏览器中输入:
http://localhost:8080/SpringWebProject/test/hello?user=world
显示如下:
总结: 这里只是一个很简单的框架, 也没有页面交互,以及各种日志配置,乱码配置等。但是这里已经是一个简单的spring框架了, 其他需要的东西都可以往里配置,因为篇幅有限其他功能性的东西,这里不再提供配置。。
更多推荐
spring 框架简介与搭建
发布评论