spring资料整理

来源:互联网 发布:百分百营销软件流量 编辑:程序博客网 时间:2024/05/02 05:52

合理利用Spring各部分功能:
Spring 越来越大,衍生出其他的分支项目,虽然臃肿,但如果你知道如何筛选自己想要的功能,就不会臃肿,反而会很简洁,例如:
1. 你想实现 Rest 服务,那么 Spring MVC 就预置了这个功能,你没必要再去使用 CXF / Restlet 之类的 Rest 框架,还得忙着想怎么和 Spring 整合,这样,Struts 就出局了,Rest 框架也省了。
2. 你想实现权限功能,那么 Spring Security 可以替你搞定,你没必要使用 Apache Shiro 来做,也没必要苦逼地自己设计,而且人家还原生支持 OAuth 和 SAML
3. 你不想苦逼地写 DAO(data access object,数据访问对象,是第一个面向数据库的接口), 那么 Spring Data 可以替你做,这些 boilerplate code 全部替你实现了,而且对 JPA 或 NO-SQL 的支持也是相当方便。
4. 你无法忍受也无法记住 spring 的配置方式和配置逻辑,你只想专心写自己的业务代码而不愿关心工具到底如何配置,那么 SpringBoot 可以搞定一切。

1.Spring Roo 和 Spring Boot 都是 Spring 组件,没有演变关系
2.基于 SpringBoot 的 micro-service 架构目前在国外很流行,学习 SpringBoot 尤为必要
3.推荐使用 Spring Boot,选择 Spring Roo 还是 Spring Boot,引用一段话如下:

First, whether you use Spring Boot or Spring Roo, the reality is you’re using Spring. So to answer your question of whether you’d be able to use other Spring Framework components…yes, absolutely. Again, Spring Boot is Spring. Spring Roo is Spring. They’re just different approaches to simplifying Spring development. Now, choosing between Spring Boot and Spring Roo. I was once a fan of Spring Roo, but Spring Boot outshines it in so many ways. Spring Roo was big on code generation and used aspects to perform some of its magic. It also forced you into a design model that you may or may not feel comfortable with. Spring Boot, on the other hand, uses no code generation and does not force any particular design choice. It is primarily autoconfiguration saving you from having to write a lot of common configuration yourself In short…Go with Spring Boot. You won’t regret it.

spring只要分为两块,一个是依赖注入,一个是面向切面编程,但是实际工作中用的最多的是依赖注入
你要学的话,就学会搭spring的环境,知道依赖注入和面向切面是怎么一回事,能写个简单demo出来就够了,其他的地方泛泛的看一下,知道他能做什么就够了,然后具体的就在项目中碰到了再深入的去看某一块。
其实所有的web框架都可以这样学,不用一本书从头到尾这样学,因为很多知识就是你当时学会了,长时间不用一样会忘,并且这个看书学习的过程也很耗时间,一般效率很低。

为什么我们要使用依赖注入的spring呢?

假如我们要实现一个存款的应用,那么,我们的思维是这么走的:

1.写一个“存款类”,里面有“存款方法”,“存款方法”直接操作数据库实现存款逻辑。

2.你突然发现,操作数据库的方法都是增删查改,为什么不写成一个类,让“存款方法”去调用这个操作数据库的类,减少了不少重复代码。
(分层思想来了,业务逻辑和数据操作分离了)

3.你的朋友知道了你在写这个应用,就跟你提意见,不如我帮你写操作数据库的类,你写存款业务的类,这样不就快了吗?我们约定一个接口,你调用这个接口,我实现这个接口,这样开发就很快了。
(接口思想来了,调用方不必理会具体实现,专注于自己的逻辑)(工厂模式)

4.你在写你的业务逻辑的时候,发现虽然你不用管数据操作逻辑的实现,但是在你的代码了,创建一个对象的语法:
接口 变量名 = new 实现类();
在做了那么多分离工作,还是要写new 实现类();,还是依赖于实现类。你就想,能不能做一个容器,自动帮我将实现类赋给接口呢?(依赖注入思想)
于是,你写出了spring,一开始使用xml配置,在类里面只需要getBean(),接口和实现类和调用类基本分离了(松耦合形成了),后来xml你也懒得写,弄出一批注解,实现类注解一下是组件,调用类声明一个接口,写个@AutoWired,spring容器就帮你完成了将实现类赋给接口的工作,即所谓的spring帮你new了一个对象。

最后你的应用结构如下图:这里写图片描述

比起刚开始一个类里面参杂着各式各样的代码,现在看一个个部件似的,何等舒服!

注:spring不止是DI,还有AOP,MVC等等,依赖注入只是它的一个功能。

  1. IoC (Inversion of Control,控制反转),这是spring的核心;
  2. AOP (Aspect Oriented Programming,面向切面编程);
    这两个东西是spring最重要的知识基础。了解清楚以后,看文档的同时进行练习。

关于spring的文档:
1. Spring Framework Reference Documentation(http://docs.spring.io/spring/docs/4.1.7.RELEASE/spring-framework-reference/htmlsingle/) 基础框架(中文:Spring Framework 4.x Reference Documentation 中文翻译);
2. Spring Data JPA 数据操作(Spring Data 中文版本);
3. Spring Security Reference 安全管理(Spring Security 参考手册);
4. Spring Boot Reference Guide 微框架,方便你快速实现,敏捷开发(Spring Boot参考指南);

1.Spring Framework Reference Documentation:基础框架中文翻译
2.Spring Data JPA:数据操作中文翻译
3.Spring Security Reference:安全管理中文翻译
4.Spring Boot Reference Guide:微框架,方便你快速实现,敏捷开发

0 0
原创粉丝点击