Spring之Spring环境搭建

一、什么是Spring?

  Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。
    ◆目的:解决企业应用开发的复杂性
    ◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
    ◆范围:任何Java应用
  Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。

  1、Spring的核心是一个轻量级(Lightweight)的容器(Container)。
  2、Spring是实现IoC(Inversion of Control)容器和非入侵性(No intrusive)的框架。
  3、Spring提供AOP(Aspect-oriented programming)概念的实现方式。
  4、Spring提供对持久层(Persistence)、事物(Transcation)的支持。
  5、Spring供MVC Web框架的实现,并对一些常用的企业服务API(Application Interface)提供一致的模型封装。
  6、Spring提供了对现存的各种框架(Structs、JSF、Hibernate、Ibatis、Webwork等)相整合的方案。

二、Spring之JavaSE环境搭建

 1、基本的Jar 包:

  commons-logging-1.2.jar
  spring-aop-4.3.0.RELEASE.jar
  spring-aspects-4.3.0.RELEASE.jar
  spring-beans-4.3.0.RELEASE.jar
  spring-context-4.3.0.RELEASE.jar
  spring-core-4.3.0.RELEASE.jar
  spring-expression-4.3.0.RELEASE.jar

 2、项目目录结构

3、源代码

①、HelloWorld.java(POJO)

 package cn.com.zfc.helloworld;

 public class HelloWorld {

     // 私有属性
private String name; public HelloWorld() {
System.out.println("HelloWorld Constructor()...");
} public void sayHello() {
System.out.println("Hello " + this.name);
} public String getName() {
System.out.println("HelloWorld getName()...");
return name;
} public void setName(String name) {
System.out.println("HelloWorld setName()...");
this.name = name;
} }

②、bean-helloworld.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.xsd"> <!-- 配置bean:id,bean的唯一标识;class,bean实现的实现类 -->
<bean id="helloWorld" class="cn.com.zfc.helloworld.HelloWorld">
<!-- 使用属性注入 -->
<property name="name" value="Spring"></property>
</bean>
</beans>

③、TestHelloWorld.java(测试类)

 package cn.com.zfc.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.com.zfc.helloworld.HelloWorld; public class TestHelloWorld {
public static void main(String[] args) {
/* 1、获取Ioc容器 */
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-helloworld.xml"); /* 2、获取bean */ //根据id获取bean
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); /* 3、调用bean的方法 */
helloWorld.sayHello();
}
}

④、运行效果

二、Spring之JavaEE环境搭建

1、基本Jar包

  commons-logging-1.2.jar
  spring-aop-4.3.0.RELEASE.jar
  spring-beans-4.3.0.RELEASE.jar
  spring-context-4.3.0.RELEASE.jar
  spring-core-4.3.0.RELEASE.jar
  spring-expression-4.3.0.RELEASE.jar
  spring-web-4.3.0.RELEASE.jar

2、目录结构

3、源代码

①HelloWorld.java(POJO)

 package cn.com.zfc.bean;

 public class HelloWord {
private String name; public HelloWord() {
super();
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "HelloWord [name=" + name + "]";
} }

②web.xml(注册Spring的配置文件)

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Spring_02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 注册 Spring 配置文件 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- Spring 配置文件的位置 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器,加载 Spring 配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

③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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 配置 bean -->
<bean id="" class="cn.com.zfc.bean.HelloWord">
<!-- 使用属性注入为 HelloWorld 的属性赋值 -->
<property name="name" value="zfc"></property>
</bean> </beans>

④index.jsp(测试页面)

 <%@page import="cn.com.zfc.bean.HelloWord"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page
import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring</title>
</head>
<body>
<h1>Spring 在 web 项目中的应用</h1>
<%
/* 在 jsp 页面中获取 Spring 的 Ioc 容器 */
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
/* 获取 bean,使用类型 */
HelloWord helloWord = ctx.getBean(HelloWord.class);
%>
<h2>
在 jsp 中获取 bean:<%=helloWord%></h2>
</body>
</html>

4、运行效果

Spring之Spring环境搭建的更多相关文章

  1. SpringData系列一 Spring Data的环境搭建

    本节作为主要讲解Spring Data的环境搭建 JPA Spring Data :致力于减少数据访问层(DAO)的开发量.开发者唯一要做的就是声明持久层的接口,其他都交给Spring Data JP ...

  2. spring boot 开发环境搭建(Eclipse)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. Spring1:Spring简介、环境搭建、源码下载及导入MyEclipse

    框架学习前言 这个模块是面向Spring的,Spring的学习我是这么想的: 1.简单介绍Spring,主要是从网上借鉴一些重点 2.尽量说明清楚Spring的使用方法以及细节点 3.尽量以自己的理解 ...

  4. 【一步一步】Spring 源码环境搭建

    平时项目中基本上都会用到spring,但是源码还没有深入的了解过.趁这段时间稍微空闲点,开始研究下spring 源码.下面是spring 源码的环境搭建. 主要分为如下步骤: ①安装jdk,gradl ...

  5. Spring ——简介及环境搭建跑通Hello

    Spring Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.是为了解决企业应用程序开发复杂性而创建的.框架的主要优 ...

  6. Spring Data 开发环境搭建(二)

    首先咱们先创建一个maven工程 在pom.xml加入以下 依赖 <!--Mysql 驱动包--> <dependency> <groupId>mysql</ ...

  7. spring web mvc环境搭建

    这两天在学习spring,哎,没办法呀,成都基本都是java,不喜欢学这个也没有用.多学点多条路.还得生活不是. 好了,言归正传,我这里记录下我搭建spring mvc的过程,其实过程不算太难,主要是 ...

  8. Spring4- 01 - Spring框架简介及官方压缩包目录介绍- Spring IoC 的概念 - Spring hello world环境搭建

    一. Spring 框架简介及官方压缩包目录介绍 主要发明者:Rod Johnson 轮子理论推崇者: 2.1 轮子理论:不用重复发明轮子. 2.2 IT 行业:直接使用写好的代码. Spring 框 ...

  9. 项目SpringMVC+Spring+Mybatis 整合环境搭建(2)-> 测试Spring+Mybatis 环境

    测试前期准备 第一步:创建easybuy数据库,设置utf-8格式 第二步:创建表test_tb CREATE TABLE `test_tb` ( `id` int(11) NOT NULL AUTO ...

  10. ①spring简介以及环境搭建(一)

    注*(IOC:控制反转.AOP:面向切面编程) spring官网:http://spring.io/ spring简介: spring是一个开源框架 spring为简化企业级应用开发而生,使用Spri ...

随机推荐

  1. iphone清除数字链接

    <meta name="format-detection" content="telephone=no">

  2. Kissy && Require

    KISSY add(name?,factory?,deps)  函数挂载在全局对象KISSY上,用来定义模块.   一个 JS 文件包含一个add()(这时路径+文件名可以用作模块名),如果一个文件包 ...

  3. Dream------scala--类的属性和对象私有字段实战详解

    Scala类的属性和对象私有字段实战详解 一.类的属性 scala类的属性跟java有比较大的不同,需要注意的是对象的私有(private)字段 1.私有字段:字段必须初始化(当然即使不是私有字段也要 ...

  4. Java与JS生成二维码

    1.二维码概念 二维码/二维条码是用某种特定的集合图形按一定规律在平面上(二维方向上)分布的黑白相间的图形记录数据符号信息的图片. 黑线是二进制的1,空白的地方是二进制的0,通过1.0这种数据组合用于 ...

  5. 利用正则表达式去除所有html标签,只保留文字

    后台将富文本编辑器中的内容返回到前端时如果带上了标签,这时就可以利用这种方法只保留文字. 标签的格式有以下几种 1.<div class="test"></div ...

  6. flask基础之蓝图的使用(七)

    前言 关于蓝图是什么?或为什么使用蓝图的详细介绍,官方文档讲的很详细,不再赘述.简单来说,在大型的应用中,我们不想视图函数显得杂乱无章,难以维护,将众多的视图函数按照Api的设计规则进行切割是一个好方 ...

  7. 我看到的最棒的Twisted入门教程!

    http://www.douban.com/note/232204441/ http://www.cnblogs.com/sevenyuan/archive/2010/11/18/1880681.ht ...

  8. linux查看内存、CPU占用资源最多的进程

    [内存占用] #利用ps命令,默认使用ps参数会显示的结果 ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 ...

  9. SQLAlchemy-介绍安装

    一:概述 SQLAlchemy的SQL工具包和对象关系映射是一个全面的工具集,用来处理数据库和Python. 它有几个不同的功能领域,可以单独使用或组合使用. 所示的主要组件,组件依赖关系组织成层: ...

  10. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...