理论学习

·PO(persistent object)是持久化对象,所谓的持久化就是和数据库对应的主要是字段上,典型的应用是在hibernate中通过实体对象直接操作数据库的增删查改。一般提供get、set方法,实现序列化接口,可以继承自其它的object。

·VO(value object)有时可以跟po一样,有时并不对应,因为其并不直接和数据库打交道,名字并不是强制的。

·POJO(Plain old java objects)简单java对象,不继承任何类,不实现任何接口,只有属性和get、set方法。

·Spring 的两个核心概念:

依赖注入 DI

面向切面编程 AOP

依赖注入让相互协作的软件保持松散耦合,而AOP编程允许你把遍布应用各处的功能分离出来形成可重用的组件。

面向切面编程往往被定义为促使应用程序分离关注点的一项技术。系统由许多不同组件组成,每一个组件各负责一块特定功能。除了实现自身核心的功能之外,这些组件还经常承担着额外的职责。诸如日志、事务管理和安全此类的系统服务经常融入到自身核心业务逻辑的组件中去,这些系统服务通常被称为横切关注点,因为他们总是跨越系统的多个组件。

·Spring下载

Csdn下载地址:http://download.csdn.net/detail/bestcxx/9623917

下载下来的jar包:spring-framework-3.0.0.RC3-with-docs.zip

解压之后的内部结构:

·bean的配置文件的基本模板beanConfigurerTests.xml地址位于解压后的位置如下:

\..\spring-framework-3.0.0.RC3\projects\org.springframework.aspects\src\test\java\org\springframework\beans\factory\aspectj\beanConfigurerTests.xml

<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"></beans>

· Spring 的核心框架自带了10个命名空间配置,刚才的出现在beans配置文件中的beans只是其中之一,全部描述如下:

aop:为声明切面以及将 @AspectJ注解的类代理为Spring切面提供了配置元素。

beans: 支持声明Bean和装配Bean,是Spring最核心也是最原始的命名空间。

context: 为配置Spring应用上下文提供了配置元素,包括自动检测和自动装配Bean,注入非Spring直接管理的对象。

jee:提供了与JavaEE API的集成,例如JNDI和EJB。

jms: 为声明消息驱动的POJO提供了配置元素。

lang: 支持配置由Groovy、JRuby或BeanShell等脚本实现的Bean。

mvc:启用Spring MVC的能力,例如面向注解的控制器、视图控制器和拦截器。

oxm: 支持Spring的对象到XML映射配置。

tx: 提供声明式事物配置。

util: 提供各种各样的工具类元素,包括把集合配置为Bean,支持属性占位符元素。

·Spring模块

当我们下载Spring 并解压后,在dist 目录下可以看到20个不同的jar文件,它们依据所属功能可以划分为6个不同的功能模块。

·创建spring项目

1、在myeclipse中创建一个新的web项目,springtest

为了便于以后使用maven管理项目,建立如下的目录结构

2、引入spring3.0 的所有jar包,下载下来后位于目录dist下

此外,由于spring 还需要其他基础jar包的支持,而我们正好要配合使用struts2,所以还要引入下面的jar包,即struts2的基础jar包

下载地址:http://download.csdn.net/detail/bestcxx/9626401

3、将bean.xml 配置文件放置到 src.main.resources的spring文件夹下,将文件名改为
applicationContext.xml

由于目前还没有具体的bean配置,所以该xml仅需基本内容,如下

<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>

4、编写接口类-表演者

接口类是实现spring 依赖注入思想的基础,spring通过配置文件或者注解完成接口调用时的不同实体的注入,从而实现了松耦合。

Performer.java 的内容

package stu.bestcxx.springtest.facade;
/*** spring通过调用接口,而接口由不同的实现类来执行具体的实现,* 通过spring的配置文件或者注解完成"依赖注入"(DI)* * @author WuJieJecket*/
public interface Performer {public void perform();
}

5、编写表演者接口的实现类-具备具体表演技能

PerformJuggler.java

package stu.bestcxx.spring.impl;
import stu.bestcxx.springtest.facade.Performer;
/*** 实现接口类 Perform* 杂技师 juggler* 其表演内容是抛豆子* 为了体现传参功能,其可以抛固定个数的豆子* 也可以通过构造方法完成注入 豆子的数量* * @author WuJieJecket*/
public class PerformJuggler implements Performer {//豆子的数量private int beanBags=3;//由于重构了无参构造方法,需要显示声明默认构造方法public PerformJuggler(){};//重构构造方法,增加入参public PerformJuggler(int beanBags){this.beanBags=beanBags;};@Overridepublic void perform() {// TODO Auto-generated method stubSystem.out.println("杂技师表演抛豆袋子,数量是 "+beanBags);}
}

6、在配置文件applicationContext.xml中

杂技师 juggler ,杂技师并不是一个具体的人,而是一个职业,这样Performer对应多个职业,一个职业对应多个人。至于这个人是谁?就是spring要做的事情了,将这个人注入到杂技师这个bean中

<!-- 杂技师-juggler1 --><bean id="juggler1" class="stu.bestcxx.spring.impl.PerformJuggler"/>

7、编写测试方法,让杂技师juggler1进行表演

根据maven规范(尽管这个项目没有使用maven),应该建立完善的测试方法

PerformJugglerTest.java内容如下:

package stu.bestcxx.spring.impl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import stu.bestcxx.springtest.facade.Performer;
public class PerformJugglerTest {/** 杂技师 juggler1表演抛豆子 */@Testpublic void testperform(){//从类路径获取spring的上下文ApplicationContext ctx=new ClassPathXmlApplicationContext("spring/applicationContext.xml");//根据配置文件,将杂技师 juggler1 注入到表演者 PerformerPerformer juggler1=(Performer) ctx.getBean("juggler1");//juggler1进行表演juggler1.perform();}
}

运行结果:

8、从文件系统获取spring的上下文

刚才的测试方法是从类路径中获取的beans的配置信息,下面演示从文件系统获取spring的上下文。在myeclipse的Navigator视图模式下,我们看到,applicationContext.xml已经被编译到classes目录下

在PerformJugglerTest.java中添加内容

/** 杂技师 juggler1表演抛豆子 * 文件系统获取spring的上下文*/@Testpublic void testperform2(){//从文件系统获取spring的上下文,可以使用通配符表示多个文件,如aaaT.xml 和 bbT.xml 可以是*T.xml//ApplicationContext ctx=new FileSystemXmlApplicationContext("WebRoot/WEB-INF/classes/spring/applicationContext.xml");ApplicationContext ctx=new FileSystemXmlApplicationContext("classpath:spring/applicationContext.xml");//根据配置文件,将杂技师 juggler1 注入到表演者 PerformerPerformer juggler1=(Performer) ctx.getBean("juggler1");//juggler1进行表演juggler1.perform();}

9、使用XmlWebApplicationContext获取spring系统的上下文

在这种模式下,web 项目启动之后就会自动加载beans 的配置文件

我们在方法中使用的时候可以直接使用spring的依赖注入功能。

9.1优先是修改web.xml的内容,修改完后如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext.xml</param-value></context-param><!-- struts2 的filter 配置 --><filter><filter-name>springfilter</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>springfilter</filter-name><url-pattern>/*</url-pattern><dispatcher>FORWARD</dispatcher><dispatcher>REQUEST</dispatcher></filter-mapping><listener><description>Spring 刷新 Introspector 防止内存泄露</description><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><listener><description>Spring ApplicationContext 载入</description><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><session-config><session-timeout>30</session-timeout></session-config><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><login-config><auth-method>BASIC</auth-method></login-config>
</web-app>

9.2 然后编写action类

Juggler1Action.java 内容如下

package stu.bestcxx.springtest.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import stu.bestcxx.springtest.facade.Performer;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Juggler1Action extends ActionSupport {private static final long serialVersionUID = 7483844875659263452L;/*** 继承了struts2的 ActionSupport 类,直接重写 execute方法*/@Overridepublic String execute() throws Exception {// TODO Auto-generated method stub//使用 XmlWebApplicationContext 获取 Spring 的上下文ActionContext ctx = ActionContext.getContext();       HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);       ServletContext servletContext = request.getSession().getServletContext();    ApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);//根据配置文件,将杂技师 juggler1 注入到表演者 PerformerPerformer juggler1=(Performer) wac.getBean("juggler1");//juggler1进行表演juggler1.perform();return SUCCESS;}
}

9.3新增struts.xml文件

struts.xml 内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts><include file="struts/struts-juggler1.xml"></include>
</struts>

struts-juggler1.xml的内容

<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts><package name="juggler1" extends="struts-default" namespace="/juggler1"><action name="juggler1" class="stu.bestcxx.springtest.action.Juggler1Action"><result name="success" type="redirect">https://www.baidu.com</result>   </action></package>
</struts>

9.4 启动服务,访问如下地址,注意端口,我的测试环境是8085

http://localhost:8085/springtest/juggler1/juggler1.action

页面会跳转到百度首页,这不是重点,控制台打印输出

基础环境搭建和测试访问结束

Spring 3.0 学习-环境搭建和三种形式访问相关推荐

  1. Win7 命令行下C语言学习环境搭建(三)

    从上次碰到编译链接带有自定义库的C源文件后,就停止了学习,工作时间瞎忙,这一个多点星期以来,老问题又重新涌上脑际,查阅了好多与 Makefile Gcc 相关的网页资料,终于搞明白了相关的原因,记录下 ...

  2. Vue.js2.0开发环境搭建(三)

    转载自  Vue.js2.0从入门到放弃---入门实例(三) 今天就来简单说一下vue-resource,这是vue的一个与服务器端通信的HTTP插件,用来从服务器端请求数据.话不多说,直接上干货吧. ...

  3. Java之美[从菜鸟到高手演变]之Spring源码学习 - 环境搭建

    准备工作 1.下载安装STS(Spring Tool Suite),在eclipse market里直接搜索.下载.安装. 2.下载安装gradle, Spring源码使用gradle构建,下载后解压 ...

  4. 保姆级教程——Ubuntu16.04 Server下深度学习环境搭建:安装CUDA8.0,cuDNN6.0,Bazel0.5.4,源码编译安装TensorFlow1.4.0(GPU版)...

    写在前面 本文叙述了在Ubuntu16.04 Server下安装CUDA8.0,cuDNN6.0以及源码编译安装TensorFlow1.4.0(GPU版)的亲身经历,包括遇到的问题及解决办法,也有一些 ...

  5. ESXi6.5环境搭建(三:vSphere Client6.0安装)

    实验目的及要求 完成VMware workstations安装,会应用相关操作: 完成虚拟机中ESXI6.5平台的安装及网络环境配置: 完成VMware vSphere Client 6.0软件在PC ...

  6. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一) 1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee ...

  7. Miniconda3+PyTorch1.7.1(GPU版)+Win10_x64+GTX1060深度学习环境搭建

    写在这里的初衷,一是备忘,二是希望得到高人指点,三是希望能遇到志同道合的朋友. 硬件信息: 系统:win10家庭中文版 CPU:i7-7700HQ 内存:16GB 显卡:GTX1060 目录 一.确定 ...

  8. 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建

    从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...

  9. Ubuntu16.04深度学习环境搭建

    Ubuntu16.04深度学习环境搭建(anaconda3+cuda10.0+cudnn7.6+pytorch1.2) 文章目录 Ubuntu16.04深度学习环境搭建(anaconda3+cuda1 ...

最新文章

  1. 网站优化有三个重点不能忽视
  2. 168. Leetcode 134. 加油站 (贪心算法-模拟题目)
  3. mysql视图的简介_mysql视图简介
  4. LTE各场景下的密钥处理
  5. c语言怎么让字母倒序排列尼,如何倒序单词顺序输出 ? 我是没辙了
  6. 详细分析如何利用python批量爬取百度图片
  7. 二维粗糙海面matlab,三维随机粗糙海面的Monte-Carlo仿真
  8. 国家游泳中心诚聘CV算法开发人员~待遇优福利厚
  9. Rock8247 bsp-Tornado-VXWorks Build up
  10. iOS 操作系统被曝无线网络命名bug 导致 iPhone无法连接无线网络
  11. JS实现点击复制目标内容
  12. chrome离线安装包的稳定下载方法
  13. Sodinokibi勒索病毒利用Flash漏洞强势来袭
  14. 利用东方财富网获取股票代码
  15. c1侧方停车技巧图解解析停车要点
  16. 最新iPhone X设计规范,详细完整的了解IOS设计规范。
  17. Windows 11 将“扼杀”第三方浏览器?Firefox 绝地反击!
  18. C#使用iTextSharp打印PDF
  19. 微信小程序:高德地图搜索周边poi接口实践
  20. POJ1061 青蛙的约会

热门文章

  1. linux如何安装声卡驱动
  2. 金仓数据库 KingbaseES 客户端编程接口指南 - ODBC 驱动使用
  3. SPM12入门案例1
  4. 携手亚马逊云科技帆软,淄博热力开启传统企业数字化转型新征程。
  5. 解决小米note5 安装了google play store 打不开的问题
  6. 甘肃阿克塞百余只“岩壁精灵”雪中觅食
  7. 国外搜索引擎+视频网站
  8. 2020年物联网发展现状与趋势预测
  9. CSS布局之“弹性盒子布局”
  10. McMASTER CARR外六角螺丝的类型与区别