侧边栏壁纸
  • 累计撰写 2,058 篇文章
  • 累计创建 73 个标签
  • 累计收到 20 条评论

目 录CONTENT

文章目录

(二)IOC---Spring简明快速入门学习

大猿本猿
2021-10-17 / 405 阅读 / 0 字

IOC

IOC简介

优质程序的代码制作规则

内聚与偶合
耦合(Coupling):代码书写过程中所使用技术的结合紧密度,用于衡量软件中各个模块之间的互联程度
内聚(Cohesion):代码书写过程中单个模块内部各组成部分间的联系,用于衡量软件中各个功能模块内部的功能联系
程序书写目标
高内聚、低偶合

工厂模式发展史

Spring的发展史

Spring的核心就是由工厂模式逐步演化来

IOC的概念

loC (Inversion Of Control)控制反转,Spring反向控制应用程序所需要使用的外部资源
Spring控制的资源全部放置在Spring容器中,该容器称为loC容器

 IOC入门案例

loC入门案例制作步骤:

1.导入spring坐标(5.1.9.release)

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.19.RELEASE</version>
            <optional>true</optional>
</dependency>

2.编写业务层与表现层(模拟)接口与实现类

package com.devtao.service;


public interface UserService {
    public void save();
}
package com.devtao.service.impl;

import com.devtao.service.UserService;


public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("user Service impl");
    }
}

3.建立spring配置文件

<?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">



</beans>

4.配置所需资源(Service)为spring控制的资源

    <bean id="userService" class="com.devtao.service.impl.UserServiceImpl"/>

5.表现层(App)通过spring获取资源(Service实例)

这里直接创建一个类,直接调用配置文件获取spring的控制资源。
package com.devtao;
import com.devtao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class UserApp {
    public  static void main(String args[]){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        userService.save();

    }
}

IOC配置

bean的属性

作用:定义spring中的资源,受此标签定义的资源将受到spring控制。
格式:
 <beans>
      <bean />
  </beans>
bean的基本属性:
<bean id="beanId" name="beanName1,beanName2" class="ClassName"></bean>
     id:bean的名称,通过id值获取bean
      class:bean的类型
      name:bean的名称,可以通过name值获取bean,用于多人配合时给bean起别名
 <bean id="userService" name="userService2,userService3" class="com.devtao.service.impl.UserServiceImpl"/>
以下调用都不会报错:
UserService userService = (UserService) ctx.getBean("userService");
UserService userService2 = (UserService) ctx.getBean("userService2");
UserService userService3 = (UserService) ctx.getBean("userService3");
bean的scope属性
scope默认是单例属性

bean的生命周期

DI

IOC与DI是同一件事在不同角度看待问题。

依赖注入的两种方式

set注入(主流的)
引入类型的注入步骤
1、类中声明一个私有的dao对象,并设置set方法。
2、将dao对象交给spring控制,配置xml中的bean声明。
3、userDao的bean通过配置property注入到userService的bean中,完成对userDao的注入过程。
非引用类型的注入:
引用类型是ref属性,非引用类型是value
构造器注入(基本不用)
集合类型数据的注入

p命名空间简化书写(了解)

这两种写法是等价的,在命名空间中添加p的声明。

SpringEL(了解)

加载Properties文件

Spring提供了读取外部properties文件的机制,使用读取到的数据为bean的属性赋值
操作步骤:
1、准备外部properties文件
2、开启context命名空间支持
xmlns:context="http://www.springframework.org/schema/context"
xsi:
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
3、加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties">
4、使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
注意:如果需要加载所有的properties文件,可以使用*.properties表示加载所有的properties文件
注意:读取数据使用${propertiesName}格式进行,其中propertiesName指properties文件中的属性名

团队开发

团队开发需要拆分多个xml文件
导入xml子文件

ApplicationContext类

第三方资源配置