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)

  1. package cn.com.zfc.helloworld;
  2.  
  3. public class HelloWorld {
  4.  
  5. // 私有属性
  6. private String name;
  7.  
  8. public HelloWorld() {
  9. System.out.println("HelloWorld Constructor()...");
  10. }
  11.  
  12. public void sayHello() {
  13. System.out.println("Hello " + this.name);
  14. }
  15.  
  16. public String getName() {
  17. System.out.println("HelloWorld getName()...");
  18. return name;
  19. }
  20.  
  21. public void setName(String name) {
  22. System.out.println("HelloWorld setName()...");
  23. this.name = name;
  24. }
  25.  
  26. }

②、bean-helloworld.xml(Spring配置文件)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <!-- 配置bean:id,bean的唯一标识;class,bean实现的实现类 -->
  7. <bean id="helloWorld" class="cn.com.zfc.helloworld.HelloWorld">
  8. <!-- 使用属性注入 -->
  9. <property name="name" value="Spring"></property>
  10. </bean>
  11. </beans>

③、TestHelloWorld.java(测试类)

  1. package cn.com.zfc.test;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. import cn.com.zfc.helloworld.HelloWorld;
  7.  
  8. public class TestHelloWorld {
  9. public static void main(String[] args) {
  10. /* 1、获取Ioc容器 */
  11. ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-helloworld.xml");
  12.  
  13. /* 2、获取bean */
  14.  
  15. //根据id获取bean
  16. HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
  17.  
  18. /* 3、调用bean的方法 */
  19. helloWorld.sayHello();
  20. }
  21. }

④、运行效果

二、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)

  1. package cn.com.zfc.bean;
  2.  
  3. public class HelloWord {
  4. private String name;
  5.  
  6. public HelloWord() {
  7. super();
  8. }
  9.  
  10. public String getName() {
  11. return name;
  12. }
  13.  
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17.  
  18. @Override
  19. public String toString() {
  20. return "HelloWord [name=" + name + "]";
  21. }
  22.  
  23. }

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">
  6. <display-name>Spring_02</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- 注册 Spring 配置文件 -->
  11. <!-- needed for ContextLoaderListener -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <!-- Spring 配置文件的位置 -->
  15. <param-value>classpath:applicationContext.xml</param-value>
  16. </context-param>
  17. <!-- 配置监听器,加载 Spring 配置文件 -->
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. </web-app>

③applicationContext.xml(Spring的配置文件)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  6. <!-- 配置 bean -->
  7. <bean id="" class="cn.com.zfc.bean.HelloWord">
  8. <!-- 使用属性注入为 HelloWorld 的属性赋值 -->
  9. <property name="name" value="zfc"></property>
  10. </bean>
  11.  
  12. </beans>

④index.jsp(测试页面)

  1. <%@page import="cn.com.zfc.bean.HelloWord"%>
  2. <%@page import="org.springframework.context.ApplicationContext"%>
  3. <%@page
  4. import="org.springframework.web.context.support.WebApplicationContextUtils"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"%>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>Spring</title>
  12. </head>
  13. <body>
  14. <h1>Spring 在 web 项目中的应用</h1>
  15. <%
  16. /* 在 jsp 页面中获取 Spring 的 Ioc 容器 */
  17. ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
  18. /* 获取 bean,使用类型 */
  19. HelloWord helloWord = ctx.getBean(HelloWord.class);
  20. %>
  21. <h2>
  22. 在 jsp 中获取 bean:<%=helloWord%></h2>
  23. </body>
  24. </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. 如何编写 Typescript 声明文件

    使用TypeScript已经有了一段时间,这的确是一个好东西,虽说在使用的过程中也发现了一些bug,不过都是些小问题,所以整体体验还是很不错的. TypeScript之所以叫Type,和它的强类型是分 ...

  2. UNIX网络编程 第4章 基本TCP套接字编程

    本章的几个函数在很大程度上展示了面向对象与面向过程的不同之处.

  3. 【codeforces】【比赛题解】#960 CF Round #474 (Div. 1 + Div. 2, combined)

    终于打了一场CF,不知道为什么我会去打00:05的CF比赛…… 不管怎么样,这次打的很好!拿到了Div. 2选手中的第一名,成功上紫! 以后还要再接再厉! [A]Check the string 题意 ...

  4. LINUX vim 修改文件 退出

    vim 保存退出, 先按ESC ,然后:wq(保存退出)W:write,写入 Q:quit,退出, 也可以直接输入X,代表WQ,也是保存退出 或者 先按ESC,再按shift+ZZ 也是保存退出 正常 ...

  5. MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题 收藏 MySQL中涉及的几个字符集

    MySQL中涉及的几个字符集 character-set-server/default-character-set:服务器字符集,默认情况下所采用的.character-set-database:数据 ...

  6. 内核定时器的使用(好几个例子add_timer)【转】

    转自:http://blog.csdn.net/jidonghui/article/details/7449546 LINUX内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个 ...

  7. git clone直接提交用户名和密码

    git使用用户名密码clone的方式: git clone http://username:password@remote 例如:我的用户名是abc@qq.com,密码是abc123456,git地址 ...

  8. MongoDB官方文档结构

    本文展示MongoDB 3.6.4.0的官方Server文档的结构图——一眼可见完整的知识脉络图.不过,MongoDB除了Server的文档外,还有DRIVERS.CLOUD.TOOLS.DUIDES ...

  9. SQLAlchemy-方言(Dialects)

    一: Dialects 文档是分为三个部分: SQLAlchemy ORM, SQLAlchemy Core, and Dialects. SQLAlchemy ORM:在SQLAlchemy ORM ...

  10. python面向对象(五)之多态

    继承 ​ 在讲多态之前我们再复习下继承,下面是一个例子. ​ Circle 和 Rectangle 继承自 Shape,不同的图形,面积(area)计算方式不同. # shape.py class S ...