Spring概述及IOC实现原理

13 篇文章 0 订阅
12 篇文章 0 订阅

Spring介绍
Spring它是一个一站式的分层轻量级框架。
Spring体系结构
在这里插入图片描述

  1. core container
    a) beans与core 它们提供spring框架最基本功能,包含ioc与di
    b) context 上下文对象,基于beans与cores
    c) spel它是sprng提供的一个表达式语言
  2. Data access/integration
    a) 数据访问
    b) 集成
  3. Web
    a) Spring本身提供spring mvc
    b) 也可以与其它的web层进行集成
  4. AOP
    AOP大部分情况下是使用动态代理来实现的。
  5. Test
    使用spring可以方便的进行测试

Spring框架优点
 方便解耦,简化开发
Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
 AOP编程的支持
Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
 声明式事务的支持
只需要通过配置就可以完成对事务的管理,而无需手动编程
 方便程序的测试
Spring对Junit4支持,可以通过注解方便的测试Spring程序
 方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
 降低JavaEE API的使用难度
Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

IOC与DI
Spring的jar包下载
Spring的官网:spring.io
我们课程中讲解使用的是spring4.2.4
在这里插入图片描述
在spring3.0.2版本后,不在提供依赖jar包
在这里插入图片描述
docs 存在API和规范文档
libs 开发jar包
schema 开发过程中需要的xml的schema约束

spring开发环境搭建
在这里插入图片描述
在spring开发中,我们要根据不同的情况来导入不同的jar包,当前我们要讲解的是关于ioc与di
对于ioc与di讲解我们只需要使用spring的核心功能。

  1. beans相关
  2. core相关
  3. context相关
  4. spel相关

在这里插入图片描述

如果没有commons-loggin的jar包会报错NoClassDefFoundError
在这里插入图片描述
我们使用spring框架也会使用到配置文件,我们需要在src下创建一个关于spring的配置文件,一般情况名称叫applicationContext.xml
问题:applicationContext.xml约束?
在这里插入图片描述
它的路径:
spring-framework-4.2.4.RELEASE-dist\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html
在这里插入图片描述
IOC快速入门
Ioc它是什么,解决什么问题,它的原理是如何实现。
IOC  inversion of Controller 控制反转。
在程序中所说的IOC其实简单说,就是原来由我们自己实例化的对象交给spring容器来实始化。这时对象的实始化的权利就会反转。

IUserService接口:

package com.itcast;

public interface IUserService {
    public void sayHello();
}

IUserServiceImpl实现类:

package com.itcast.impl;
import com.itcast.IUserService;

public class IUserServiceImpl implements IUserService {
    @Override
    public void sayHello() {
        System.out.println("hello IOC");
    }
}

测试:

package com.itcast;

import com.itcast.impl.IUserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IocTest {
    @Test
    public void test1(){
        //以前使用service
        //IUserService userService = new IUserServiceImpl();
        //userService.sayHello();

        //使用spring容器提供的IOC
        //IOC的本质就是XML配置文件+反射+工厂来实现
        //ClassPathXmlApplicationContext可以帮我们在classpath查找applicationContext.xml配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserService userService1 = (IUserService)applicationContext.getBean("userService");
        userService1.sayHello();
    }
}

总结spring使用步骤:

  1. 在applicationContext.xml文件中配置bean
<beans>
    <bean id="userService" class="com.itcast.impl.IUserServiceImpl"></bean>
</beans>
  1. 创建一个AppliCationContext对象
    ApplicationContext它是BeanFactory的一个子接口,我们在使用时使用的是AppliCationContext的实现类ClassPathXmlApplicationContext
	//使用spring容器提供的IOC
        //IOC的本质就是XML配置文件+反射+工厂来实现
        //ClassPathXmlApplicationContext可以帮我们在classpath查找applicationContext.xml配置文件
        //可以通过getBean(配置文件中id名称)来获取指定的对象。
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserService userService1 = (IUserService)applicationContext.getBean("userService");
        userService1.sayHello();

测试:成功
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值