microsoft做网站,杭州房价暴跌已开始,cmseasy破解版,陕西seo排名一、认识框架 实际开发中#xff0c;随着业务的发展#xff0c;软件系统变得越来越复杂#xff0c;如果所有的软件都从底层功能开始开发#xff0c;那将是一个漫长而繁琐的过程。此外#xff0c;团队协作开发时#xff0c;由于没有统一的调用规范#xff0c;系统会出现大…一、认识框架 实际开发中随着业务的发展软件系统变得越来越复杂如果所有的软件都从底层功能开始开发那将是一个漫长而繁琐的过程。此外团队协作开发时由于没有统一的调用规范系统会出现大量的重复功能的代码给系统的二次开发和维护带来不便。为解决上述问题框架应运而生。框架实现了很多基础性的功能开发人员不需要关心底层功能操作只需要专心地实现所需要的业务逻辑大大提高了开发人员的工作效率。当前市场上的Java EE开发主流框架有Spring、SpringMVC和Mybatis等。
二、为什么要学Spring框架 Spring致力于解决Java EE应用中的各种问题对于一个Java开发者来说Spring框架的熟练使用是必备的技能之一。Spring具有良好的设计和分层结构它克服了传统重量型框架臃肿、低效的劣势大大简化了项目开发中的技术复杂性。
三、什么是Spring
1、 我们经常挂在口中的Spring指的是Spring FrameworkSpring 框架它是一个开源框架有着活跃而庞大的社区。这就是它之所以能长久不衰的原因Spring支持广泛的应用场景它可以让Java企业级开发应用程序开更简单。
2、Spring是由Rod Johnson组织和开发的一个分层的Java SE/EE一站式full-stack轻量级开源框架。它最为核心的理念是IoC控制反转和AOP面向切面编程其中IoC是Spring的基础它支撑着Spring对JavaBean的管理功能AOP是Spring 的重要特性AOP是通过预编译方式和运行期间动态代理实现程序功能也就是说可以在不修改源代码的情况下给程序统一添加功能。 3、 Spring 5框架组成模块Spring 框架主要有8大模块每个大模块由多个或1个小模块组成如Spring的核心容器模块Core Container是由Beans模块、Core模块、Context模块和SpEL模块组成。
4、框架体系图 四、Spring的设计理念来源于官网介绍 在每个层面上提供选择。Spring让你尽可能晚地推迟设计决策。例如你可以通过配置来切换持久化供应商而不需要改变你的代码。对于许多其他基础设施问题和与第三方API的集成也是如此。 适应不同的观点。Spring拥抱灵活性对事情应该如何做不持意见。它支持具有不同视角的广泛的应用需求。 保持强大的后向兼容性。Spring的演进是经过精心管理的在不同的版本之间几乎不存在破坏性的变化。Spring支持一系列精心选择的JDK版本和第三方库以方便维护依赖Spring的应用程序和库。 关心API的设计。Spring团队花了很多心思和时间来制作直观的API并且在很多版本和很多年中都能保持良好的效果。 为代码质量设定高标准。Spring框架非常强调有意义的、最新的和准确的javadoc。它是为数不多的可以宣称代码结构干净、包与包之间没有循环依赖关系的项目之一。
五、第一个Spring项目---Hello Spring!!!
1、在idea中创建Maven项目
2、在pom.xml文件中加载需使用到的Spring基础包以及Spring依赖包并重新加载
具体代码如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdSpringdemo01/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetorg.springframework.version4.3.19.RELEASE/org.springframework.versioncommons-logging.version1.2/commons-logging.version/propertiesdependencies!-- Spring核心依赖 --dependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion${org.springframework.version}/version/dependency!-- Spring 容器包 --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${org.springframework.version}/version/dependency/dependencies
/project
!--可作为Spring学习的pom.xml模板注意Maven中pom.xml的配置要求--
3、创建名为HelloSpring的类在HelloSpring类中定义userName属性和show()方法。
package com.itheima;
public class HelloSpring {private String userName;public void setUserName(String userName){this.userNameuserName; }public void show() {System.out.println(userName:欢迎进入Spring学习我是Spring的开发者);}}4、在resources下新建applicationContext.xml文件作为HelloSpring类的配置文件并在该配置文件中创建id为helloSpring的Bean
注意 XML文件包含了很多约束信息如果自己动手去编写不但浪费时间还容易出错。XML的约束信息如下所示。
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
/beans
完整applicationContext.xml如下
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- 将指定类配置给Spring让Spring创建HelloSpring对象的实例 --bean idhelloSpring classcom.itheima.HelloSpring!--为userName属性赋值--property nameuserName valueRod Johnson/property/bean
/beans
!--Beans模块提供了BeanFactory类是工厂模式的经典实现Beans模块的主要作用是创建和管理Bean对象。--
!--可作为配置文件模板--
!--XML文件包含了很多约束信息--
5、创建测试类TestHelloSpring
在main()方法中初始化Spring容器并加载applicationContext.xml配置文件通过Spring容器获取HelloSpring类的helloSpring实例调用HelloSpring类中的show()方法在控制台输出信息。
package com.itheima;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class HelloSpringTest {public static void main(String[] args){// 初始化spring容器加载applicationContext.xml配置ApplicationContext applicationContextnewClassPathXmlApplicationContext(applicationContext.xml);// 通过容器获取配置中helloSpring的实例HelloSpring helloSpring(HelloSpring)applicationContext.getBean(helloSpring);helloSpring.show();// 调用方法}}
6、运行第一个Spring程序
六、总结
1、学习Spring的中文非官方翻译文档springdoc.cn
2、什么是 pom
POM 是 Project Object Model 的缩写即项目对象模型。
pom.xml 就是 maven 的配置文件用以描述项目的各种信息。 pom 配置一览表如下所示
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersion!-- The Basics --groupId.../groupIdartifactId.../artifactIdversion.../versionpackaging.../packagingdependencies.../dependenciesparent.../parentdependencyManagement.../dependencyManagementmodules.../modulesproperties.../properties!-- Build Settings --build.../buildreporting.../reporting!-- More Project Information --name.../namedescription.../descriptionurl.../urlinceptionYear.../inceptionYearlicenses.../licensesorganization.../organizationdevelopers.../developerscontributors.../contributors!-- Environment Settings --issueManagement.../issueManagementciManagement.../ciManagementmailingLists.../mailingListsscm.../scmprerequisites.../prerequisitesrepositories.../repositoriespluginRepositories.../pluginRepositoriesdistributionManagement.../distributionManagementprofiles.../profiles
/project其中基本配置为
project - project 是 pom.xml 中描述符的根。modelVersion - modelVersion 指定 pom.xml 符合哪个版本的描述符。maven 2 和 3 只能为 4.0.0。
3、Spring框架的优点
a.非侵入式设计 Spring是一种非侵入式non-invasive框架所谓非侵入式是指Spring框架的API不会在业务逻辑上出现也就是说业务逻辑应该是纯净的不能出现与业务逻辑无关的代码。由于业务逻辑中没有Spring的API所以业务逻辑代码也可以从Spring框架快速地移植到其他框架。
b.降低耦合性 Spring就是一个大工厂可以将所有对象的创建和依赖关系的维护工作都交给Spring容器管理大大降低了组件之间的耦合性。
c.支持AOP编程 Spring提供了对AOP的支持AOP可以将一些通用的任务进行集中处理如安全、事务和日志等以减少通过传统OOP方法带来的代码冗余和繁杂。
d.支持声明式事务 在Spring中可以直接通过Spring配置文件管理数据库事务省去了手动编程的繁琐提高了开发效率。
e.方便程序的测试 Spring提供了对Junit的支持开发人员可以通过Junit直接创建测试类进行单元测试。
f.方便集成框架
Spring提供了一个广阔的基础平台其内部提供了对各种框架的直接支持如Struts、Hibernate、MyBatis、Quartz等这些优秀框架可以与Spring无缝集成。
g.降低Java EE API的使用难度
对Java EE开发中的一些API如JDBC、JavaMail等都进行了封装大大降低了这些API的使用难度。
4、什么是IOC
Spring是一个容器容器就是用来容纳某种物品的装置。比如我们生活中常用的水杯水桶这些都可以看做是容器是一个IOC容器那么这里提的IOC是什么呢IoC Inversion of Control 翻译成中⽂是 “控制反转” 的意思也就是说 Spring 是⼀个“控制反转”的容器。
5、学Spring的本质
既然 Spring 是⼀个 IoC控制反转容器重点还在“容器”⼆字上那么它就具备两个最基础的功能将对象存入到容器从容器中取出对象即学习Spring 最核心的功能就是学如何将对象存入到 Spring 中再从 Spring 中获取对象的过程。
6、本文主要是Spring学习入门的笔记所以并不完善后续将会继续补充