史上最全讲解:Spring

什么是框架

框架是半成品的软件,反射是框架的灵魂.

什么是Spring框架

Spring是一个开源的JavaEE应用程序,核心包括控制反转IOC(Inversion of Control),依赖注入(Injection Dependency)和Aop(Aspect Oriented Programming)面向切面编程

Spring作用

降低耦合度

Spring环境搭建

1.新建maven项目

在这里插入图片描述

2.设置项目的坐标

在这里插入图片描述

3.设置maven环境

在这里插入图片描述

4.设置项目名称和存储位置

在这里插入图片描述

5.修改pom.xml配置文件

5.1修改jdk版本为1.8

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

5.2 修改单元测试的版本

   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

5.3 删除build标签里的pluginManagement标签对

  <build>
  </build>

5.4添加spring的依赖坐标

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.3.RELEASE</version>
</dependency>

6.添加spring的配置文件

6.1新建resources文件夹并且设置文资源文件夹,在里面新建spring.xml文件
在这里插入图片描述
6.2将官方文档拷贝到spring.xml文件中

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

7.创建bean对象

在这里插入图片描述

8.通过spring.xml配置bean对象

    <bean id="userService" class="com.shsxt.service.UserService">

9.加载配置文件,获取实例化对象

package com.shsxt.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        //获取spring上下文环境
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        //通过getBean方法得到Spring容器中实例化好的对象
        UserService userService = (UserService) ac.getBean("userService");
        //调用userService中的方法
        userService.test();
    }
}

Spring IOC技术

1.工厂设计模式(简单工厂 工厂方法 抽象工场)
2.xml解析方式(Dom4J)
3.反射(实例化对象 获取构造方法 获取属性 获取方法)
4.策略模式(加载资源)
5.单例(IOC创建的实例化对象都是单例)

Spring IOC配置文件加载方式

主要根据相对路径加载,也可以根据绝对路径加载
同时加载多个资源文件时有两种方法
1. 可变参数,传入多个文件名
2. 通过总的配置文件import其他配置文件

Spring IOC 对象实例化的三种方式

1.构造器实例化
注:通过默认构造器创建 空构造方法必须存在 否则创建失败
2.静态工厂实例化(了解)
3.实例化工厂实例化(了解)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值