Spring_01_入门与环境搭建

github源码地址: 

https://github.com/ixixii/spring_demos



Spring框架_4天课程_第1天_入门与环境搭建

本课程Spring的版本4.2

之前老的版本Spring3.x 是为了整合 Hibernate3.x

现在新的版本Spring4.x 是为了整合 Hibernate5.x


Spring框架_4天课程_内容大纲:

Spring第1天: Spring的简介, Spring IOC 入门(XML方式), Spring的Bean管理, Spring属性注入

Spring第2天: Spring的 IOC 提高(注解方式), Spring的AOP开发(XML方式)

Spring第3天: Spring的AOP 提高(注解方式), Spring的声明式事务, JdbcTemplate

Spring第4天: SSH的整合, HibernateTemplate的使用,OpenSessionInViewFilter的使用



1. Struts2的内容回顾

2. Spring简介

3. Spring下载和解压

4. 新建Dynamic Web Project, 并导包

5.

6.

7.

8.

9.



Struts2的拦截器及其概念

1. 拦截对Action的访问, 拦截到Action的具体方法

2. Struts2的执行流程

请求->核心过滤器->创建ActionProxy, 调用proxy.execute方法

在该方法内部->ActionInvocation.invoke()方法

在该方法内部, 递归执行一组拦截器->Action->Result->拦截器后面的代码

3. CRM登录案例: 权限拦截器

4. Struts2的标签库

        通用标签: if else else iterator property date debug ...

        UI标签: 作用: 数据回显, 主要为表单标签



Spring (由Rod Johnson创建的一个开源框架) 

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。

啥叫一站式? 

之前学的Struts是Web层的MVC框架

之前学的Hibernate是持久层的ORM框架

现在, Spring框架有JAVASE或JAVAEE每一层的解决方案

1. Web层: SpringMVC

2. Service层: Spring的Bean管理, Spring的声明式事务

3. DAO层: Spring的JDBC模板, Spring的ORM模块





插入一张, Spring的作者Rod Johnson简介



Spring框架特征

轻量——从大小与开销两方面而言Spring都是轻量的。

完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。

并且Spring所需的处理开销也是微不足道的。

此外,Spring是非侵入式的:

典型地,Spring应用中的对象不依赖于Spring的特定类。


控制反转——Spring通过一种称作控制反转(IoC)的技术促进了低耦合。

当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,

而不是这个对象自己创建或者查找依赖对象。


你可以认为IoC与JNDI相反——

不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。


面向切面——Spring提供了面向切面编程的丰富支持,

允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。

应用对象只实现它们应该做的——完成业务逻辑——仅此而已。

它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。


容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,

你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),

你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例——以及它们是如何相互关联的。

然而,Spring不应该被混同于传统的重量级的EJB容器,它们经常是庞大与笨重的,难以使用。


框架——Spring可以将简单的组件配置、组合成为复杂的应用。

在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。

Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。


MVC——Spring的作用是整合,但不仅仅限于整合,Spring 框架可以被看做是一个企业解决方案级别的框架。

客户端发送请求,服务器控制器(由DispatcherServlet实现的)完成请求的转发,控制器调用一个用于映射的类HandlerMapping,该类用于将请求映射到对应的处理器来处理请求。

HandlerMapping 将请求映射到对应的处理器Controller(相当于Action)

在Spring 当中如果写一些处理器组件,一般实现Controller 接口,

在Controller 中就可以调用一些Service 或DAO 来进行数据操作 ModelAndView 用于存放从DAO 中取出的数据,

还可以存放响应视图的一些数据。 

如果想将处理结果返回给用户,那么在Spring 框架中还提供一个视图组件ViewResolver,

该组件根据Controller 返回的标示,找到对应的视图,将响应response 返回给用户。



Spring框架特点
1.方便解耦,简化开发

通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合。

有了Spring,用户不必再为单实例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。


2.AOP编程的支持

通过Spring提供的AOP功能,方便进行面向切面的编程,许多不容易用传统OOP实现的功能可以通过AOP轻松应付。


3.声明式事务的支持

在Spring中,我们可以从单调烦闷的事务管理代码中解脱出来,通过 声明式方式灵活地进行事务的管理,提高开发效率和质量。


4.方便程序的测试

可以用非容器依赖的编程方式进行几乎所有的测试工作,

在Spring里,测试不再是昂贵的操作,而是随手可做的事情。

例如:Spring对Junit4支持,可以通过注解方便的测试Spring程序。


5.方便集成各种优秀框架

Spring不排斥各种优秀的开源框架,

相反,Spring可以降低各种框架的使用难度,

Spring提供了对各种优秀框架(如StrutsHibernate、Hessian、Quartz)等的直接支持。


6.降低Java EE API的使用难度

Spring对很多难用的Java EE API(如JDBC,JavaMail,远程调用等)提供了一个薄薄的封装层,

通过Spring的简易封装,这些Java EE API的使用难度大为降低。


7.Java 源码是经典学习范例

Spring的源码设计精妙、结构清晰、匠心独运,处处体现着大师对Java设计模式灵活运用以及对Java技术的高深造诣。

Spring框架源码无疑是Java技术的最佳实践范例。

如果想在短时间内迅速提高自己的Java技术水平和应用开发水平,

学习和研究Spring源码将会使你收到意想不到的效果。


什么是IoC  ?  Inversion of Control 控制反转

控制反转(Inversion of Control,缩写为IoC),

是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。

其中最常见的方式叫做依赖注入(Dependency Injection,简称DI)

还有一种方式叫“依赖查找”(Dependency Lookup)。

通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用传递给它。

也可以说,依赖被注入到对象中。



第1步: 下载Spring的开发包




第1步: 下载spring-framework-4.2.4.Release-dist.zip

完整路径是: 

https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/4.2.4.RELEASE/spring-framework-4.2.4.RELEASE-dist.zip

怎么找到的? 具体步骤是: 

a. 打开 repo.spring.io

b. 点击左侧的图标(Artifacts)



c. 找到并展开 libs-release-local --> org --> springfragmentwork --> spring


d. 继续展开4.2.4Release

并download下载 spring-framework-4.2.4.Release-dist.zip



下载完成后, 解压截图如下:


其中: docs是开发规范 与 api

        libs是Spring的开发jar包和源码(注意: 源码 也是jar包)

        schema是配置文件和约束


上几张截图:







Spring框架是支持JavaSE和JavaEE的

现在来创建一个 Dynamic Web Project 项目,名称叫:spring_01

并导入Spring的jar包,配置好开发环境



新建项目时请注意: Dynamic web module version 选择 2.5


新建项目完成后, 目录结构如下:


接着引入Spring的jar包

先看下docs文档中的这张图, 然后就知道要引入哪些jar包了


Test是整合JUnit单元测试

Core Container是核心容器: 即IoC部分

Aspects是也是AOP的一部分,但是是第3方的AOP开发

今天我们学的是Core Container核心容器IoC这一块的开发,

因此, 我们只需要引入Beans, Core , Context, SpEL这4块的包就可以了

我们到libs目录下, 找到4个包 就可以进行IoC的开发了!

我们把这4个包,复制到项目的WebContent --> WEB-INF --> lib目录下, 

如图所示: 




注意: 由于 Spring 也依赖日志打印, 所以还需要安装日志输出的jar包

理论上, 所有的Spring依赖的jar包都在这样一下压缩文件夹里:

spring-framework-3.0.2.release-dependencies.zip

但是: 在网上找了一圈后,没有找到这个压缩包

而且在spring 3.0.2的版本之后就已经不再发布spring-framework-3.0.2.RELEASE-dependencies.zip包了


所以, 直接从本教程的源码中把日志相关的两个jar包直接拷贝过来了

一个是: com.springsource.org.apache.commons.logging-1.1.1.jar

里面定义的全是日志记录的接口


另一个是: com.springsource.org.apache.log4j-1.2.15.jar

日志记录具体实现的jar包

至此, Spring的开发环境总算搭建好了



Spring框架I o C  Inversion of Control 的底层实现原理

那就是: 工厂模式 + 反射 + xml配置文件



正式使用Spring

第1步: 在src 下面, 新建一个xml, 名称按约定就叫 applicationContext.xml

第2步: 写bean的约束

具体参照官方的模板(框架解压目录下的docs下的reference下的html目录下的xsd-configuration.html,打开页面滑到最下方)

/Users/beyond/sg_java/spring_download/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html



40.2.12 the beans schema

Last but not least 

we have the tags in the beans schema. 

These are the same tags that have been in Spring since the very dawn of the framework. 


Examples of the various tags in the beans schema are not shown here 

because they are quite comprehensively covered in Section 6.4.2, “Dependencies and configuration in detail”(and indeed in that entire chapter).


One thing that is new to the beans tags themselves in Spring 2.0 is the idea of arbitrary bean metadata. 

In Spring 2.0 it is now possible to add zero or more key / value pairs to <bean/> XML definitions. 


What, if anything, is done with this extra metadata is totally up to your own custom logic

 (and so is typically only of use if you are writing your own custom tags as described in the appendix entitled Chapter 41, Extensible XML authoring).


Find below an example of the <meta/> tag in the context of a surrounding <bean/> 

(please note that without any logic to interpret it the metadata is effectively useless as-is).

<?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="id_userDao" class="net.vwhm.dao.UserDaoJdbcImpl">
        <meta key="cacheName" value="foo"/>
        <property name="name" value="Beyond"/>
    </bean>

</beans>

In the case of the above example, 

you would assume that there is some logic that will consume the bean definition 

and set up some caching infrastructure using the supplied metadata.


整个如下: 



第3步: 现在就可以使用Spring的方式编写测试类了

        @Test
	public void test_3() {
		// Spring方式,如果要换成另一个dao的实现类,只需要改xml配置文件
		ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao dao = (UserDao) appContext.getBean("id_userDao");
		dao.save();
	}

第4步: 把log4j.properties文件 复制到src目录下就大功告成了

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout最

最终效果如下:


github源码地址如下: 

https://github.com/ixixii/spring_demos




































未完待续,下一章节,つづく

猜你喜欢

转载自blog.csdn.net/u012576807/article/details/81041285
今日推荐