2026/4/16 22:13:32
网站建设
项目流程
郑州高端网站建设公司,建设外包网站,示范校建设网站,网络营销是营销的网络化吗目录 1.Spring框架介绍
1.1 Spring 框架的概述
1.2 Spring框架的优点
2.SpringIoC
2.1 什么是IoC#xff1f;
2.2 入门程序
3.IOC技术总结 1.Spring框架介绍
1.1 Spring 框架的概述 Spring 是一个开放源代码的设计层面框架#xff0c;它解决的是业务逻辑层和其他各层的…目录1.Spring框架介绍1.1 Spring 框架的概述1.2 Spring框架的优点2.SpringIoC2.1 什么是IoC2.2 入门程序3.IOC技术总结1.Spring框架介绍1.1 Spring 框架的概述Spring 是一个开放源代码的设计层面框架它解决的是业务逻辑层和其他各层的 松耦合问题因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架由RodJohnson创建。简单来说Spring 是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。 Spring 是于2003 年兴起的一个轻量级的Java开发框架由RodJohnson在其 著作Expert One-On-One J2EEDevelopment and Design中阐述的部分理念和原 型衍生而来。 它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层 架构分层架构允许使用者选择使用哪一个组件同时为 J2EE 应用程序开发提 供集成的框架。 Spring 的核心是控制反转IoC控制反转和面向切面AOP。简单来说Spring 是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。1.2 Spring框架的优点1.方便解耦简化开发Spring就是一个大工厂可以将所有对象创建和依赖关 系维护交给Spring管理。2.AOP 编程的支持Spring提供面向切面编程可以方便的实现对程序进行权限 拦截、运行监控等功能。可扩展性3.声明式事务的支持只需要通过配置就可以完成对事务的管理而无需手动编程。4.方便程序的测试Spring对Junit4支持可以通过注解方便的测试Spring程序。5.方便集成各种优秀框架Spring不排斥各种优秀的开源框架其内部提供了对 各种优秀框架如Struts2、Hibernate、MyBatis、Quartz 等的直接支持。6.降低JavaEE API 的使用难度Spring 对JavaEE开发中非常难用的一些API JDBC、JavaMail、远程调用等都提供了封装使这些API应用难度大大降低。2.SpringIoC2.1 什么是IoCIOC-- Inverse of Control控制反转将对象的创建权力反转给Spring框架 控制反转Inversion of Control缩写为IoC是面向对象编程中的一种设计 原则可以用来减低计算机代码之间的耦合度。 解决问题使用IOC可以解决的程序耦合性高的问题Spring的工厂读取配 置文件。2.2 入门程序导入依赖dependencies dependency groupIdorg.springframework/groupId artifactIdspring-context/artifactId version5.0.2.RELEASE/version /dependency dependency groupIdcommons-logging/groupId artifactIdcommons-logging/artifactId version1.2/version /dependency dependency groupIdlog4j/groupId artifactIdlog4j/artifactId version1.2.12/version /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version4.13.2/version scopetest/scope /dependency /dependencies创建UserService接口及其实现类package com.qcby.service; public interface UserService { void hello(); }package com.qcby.service.Impl; import com.qcby.service.UserService; public class UserServiceImpl implements UserService { Override public void hello() { System.out.println(Hello IOC!); } }编写配置文件applicationContext.xml?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd !--IOC管理bean -- bean iduserService classcom.qcby.service.Impl.UserServiceImpl/bean /beans测试方法import com.qcby.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo1 { /** * 入门程序 */ Test public void run1() { //使用Spring的工厂 ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml); //通过工厂获得类 UserService userService (UserService) context.getBean(userService); userService.hello(); } }运行流程导入Spring依赖如spring-context编写接口与实现类创建Spring配置文件通过ApplicationContext获取Bean并调用方法3.IOC技术总结ApplicationContext接口工厂的接口使用该接口可以获取到具体的Bean对象。该接口下有两个具体的实现类。ClassPathXmlApplicationContext加载类路径下的Spring配置文件。 FileSystemXmlApplicationContext加载本地磁盘下的Spring配置文件。