SpringIOC、DI依赖注入、Spring整合MyBatis

30 篇文章 0 订阅
17 篇文章 0 订阅

1.框架的概念:

不同的项目会使用相同的代码进行开发。而这部分代码如果要进行封装不是一个类或者一个方法

就能搞定的,需要封装出来很多的类文件,于是为了方便其他人使用,我们再次将这些代码封装

为jar包,所以,框架其实就是不同的项目都会使用的代码的封装,软件的半成品。

2.框架的本质:

就是别人封装好的功能代码。不同项目之间要使用的重复代码。封装的代码是用来解决功能开发

的,不是一个具体的功能,还需要结合调用代码以及功能自己的独立代码来使用,框架也称为软

件的半成品。

3.框架的具体表现形式

一堆jar包:封装好了要使用的代码。

4.框架的使用

① 将jar包导入到自己的项目中

② 查阅API文档,调用jar包中的资源完成功能开发

③ 配置框架的配置文件(框架代码运行需要的常量数据)

数据和代码的解耦,使用者可以通过配置文件来自定义数据。

配置文件不能乱写,必须按照API的说明的格式来配置,因为框架的底层

已经将配置文件的解析代码提前封装了。

5.Spring框架原理

作用:

实现了代码层与层之间的解耦

原理:
在这里插入图片描述

6.SpringIOC

作用:

IOC将耦合性非常高的对象进行解耦

基本流程:

6.1 创建项目然后配置web的相关依赖以及项目结构的配置

创建java工程, 导入jar包

spring核心jar包: 4个

日志包: commons-logging.jar, 被spring-core.jar所依赖.

6.2在src下创建并配置applicationcontext.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 
     http://www.springframework.org/schema/beans/spring-beans.xsd">    
   <!--配置要让Spring容器对象通过反射创建的对象的权限定路径:业务层对象-->
    <bean id="us" class="com.bjsxt.service.impl.UserServiceImpl2">
    </bean>
</beans>

6.3.在业务层中创建UserService接口及其实现类

UserService接口

package com.bjsxt.service;
public interface UserService {
    //声明功能方法
    String testIOCService();
}
UserServiceImpl实现类
package com.bjsxt.service.impl;
import com.bjsxt.service.UserService;
public class UserServiceImpl implements UserService {
    //测试IOC
    @Override
    public String testIOCService() {
        return "SpringIOC  Study";
    }
}
6.4.在项目中创建MVC的包结构,并创建IOCServlet,并在Service方法中获取Spring的容器对象,并从容器对象中获取业务层对象,完成业务操作。
@WebServlet("/ioc")
public class IOCServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码格式
        //设置响应编码格式
        //获取请求信息
        //处理请求
            //创建Spring的容器对象
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
            //获取容器中的业务层对象
            UserService us = (UserService) ac.getBean("us");
            //处理请求
            String s = us.testIOCService();
        //响应结果
            resp.getWriter().write(s);
    }
}

控制:创建对象的过程

反转:创建对象这个操作本身是程序完成的,现在反交给spring 进行管理创建

7.SpringIOC创建对象的三种方式

7.1 通过构造器方式

①无参数构造器(创建一个没有初始化数据的对象)

②有参数构造器(创建一个带有初始化数据的对象)

applicationcontext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--创建student的bean对象-->
    <!--构造器方式-->
    <!--
    无参构造器
    特点:Spring容器默认使用无参构造方式创建对象
    使用:在配置文件中直接使用bean标签配置即可,无需过多声明
    -->
    <bean id="stu" class="com.bjsxt.pojo.Student"></bean>
    <!--有参数的构造器
    特点:Spring容器对根据配置调用的有参构造器创建一个带有初始化数据的对象
    使用:constructor-arg:使用bean的字标签来声明调用的构造器的形参的个数
    一个字标签表示一个参数
    属性:index:参数的下标
          type:参数的类型,全限定路径
          name:参数的形参名
          value:参数要给的值
    -->
    <bean id="stu2" class="com.bjsxt.pojo.Student">
        <constructor-arg index="0" type="java.lang.Integer" name="sid" value="1"></constructor-arg>
        <constructor-arg index="1" type="java.lang.String" name="sname" value="张三"></constructor-arg>
    </bean> 
</beans>

TestObject代码示例

import com.bjsxt.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testStu {
    public static void main(String[] args) {
        //创建容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
        //获取容器中的对象
        //无参构造器方式
        Student student = (Student) ac.getBean("stu");
        System.out.println("无参构造:"+student);
        //有参构造器
        Student student1= (Student) ac.getBean("stu2");
        System.out.println("有参构造:"+student1);
    }
}

7.2 属性注入方式

示例

<!--
    2.使用属性注入方式:
    注意:必须提供属性的set方法,底层通过反射调用set方法给属性赋值。
    一个property子标签表示一个属性赋值
        name属性的值为属性名,value属性为要赋的值
   此方式相对于构造器方式使用面更广,更灵活,因为不需要声明对应的构造器
-->
    <bean id="stu3" class="com.bjsxt.pojo.Student">
        <property name="sid" value="2"></property>
        <property name="sname" value="李四"></property>
    </bean>

7.3 通过工厂模式

本质:

就是封装对象的创建过程的一种代码的编程思想

静态工厂:

生产对象的方法是静态方法

动态工厂:

生产对象的方法是非静态方法

applicationcontext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--创建student的bean对象-->
    <!--工厂设计模式-->
    <!--动态工厂-->
        <bean id="factory" class="com.bjsxt.pojo.StudentFactory"></bean>
        <!--生产Student对象-->
        <bean id="stu4" factory-bean="factory" factory-method="newIntance"></bean>
    <!--静态工厂-->
    <!--可以理解为静态方法直接用类名调用-->
        <bean id="stu5" class="com.bjsxt.pojo.StudentFactory2" factory-method="newIntance"></bean>
</beans>

8.DI依赖注入

存在依赖关系的类被实例化时,其依赖属性必须有值!

流程总结:

① 配置bean

  1. 当前要使用的对象
    
  2. 当前对象依赖的对象的bean
    
  3. 当前对象依赖的对象的依赖对象的bean........
    

② 组装

  1. 构造器方式在constructor-arg标签上使用ref属性
    
  2. 属性注入方式在property标签上使用ref属性
    

注意:ref的属性值为要赋值的bean的id

三种方式:

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--1.1无参构造器-->
    <bean id="stu" class="com.bjsxt.pojo.Student"></bean>
    <!--1.2有参构造器-->
    <bean id="stu2" class="com.bjsxt.pojo.Student">
        <constructor-arg index="0" name="sid" type="java.lang.Integer" value="1"></constructor-arg>
        <constructor-arg index="1" name="sname" type="java.lang.String" value="张三"></constructor-arg>
        <constructor-arg index="2" name="fvg" type="java.lang.String" value="玩"></constructor-arg>
    </bean>
    <!--
       2.使用属性注入方式:
       注意:必须提供属性的set方法,底层通过反射调用set方法给属性赋值。
       一个property子标签表示一个属性赋值
           name属性的值为属性名,value属性为要赋的值
      此方式相对于构造器方式使用面更广,更灵活,因为不需要声明对应的构造器
   -->
    <bean id="stu3" class="com.bjsxt.pojo.Student">
        <property name="sid" value="3"></property>
        <property name="sname" value="张三"></property>
    </bean>
    <!--3.使用工厂模式-->
    <!--3.1动态工厂-->
    <bean id="stuFactory" class="com.bjsxt.factory.StudentFactory" ></bean>
    <bean id="stu4" factory-bean="stuFactory" factory-method="newInstance"></bean>
    <!--3.2静态工厂-->
    <bean id="stu5" class="com.bjsxt.factory.StudentFactory2" factory-method="newInstance"></bean>
    <!--4.DI依赖注入:创建存在依赖关系的对象-->
    <!--4.1有参数构造器方式-->
    <bean id="stu6" class="com.bjsxt.pojo.Student">
        <constructor-arg index="0" name="sid" type="java.lang.Integer" value="1"></constructor-arg>
        <constructor-arg index="1" name="sname" type="java.lang.String" value="项王"></constructor-arg>
        <constructor-arg index="2" name="fvg" type="java.lang.String" value="喜欢打架"></constructor-arg>
        <constructor-arg index="3" name="teacher" type="com.bjsxt.pojo.Teacher" ref="tea"></constructor-arg>
    </bean>
    <bean id="tea" class="com.bjsxt.pojo.Teacher">
        <property name="tid" value="1"></property>
        <property name="tname" value="老夫子"></property>
    </bean>
    <!--属性注入方式-->
    <bean id="stu7" class="com.bjsxt.pojo.Student">
        <property name="teacher" ref="tea"></property>
    </bean>
    <!--自动注入方式-->
    <bean id="stu8" class="com.bjsxt.pojo.Student" autowire="byName">
        <property name="sname" value="赵丽颖"></property>
    </bean>
    <bean id="teacher" class="com.bjsxt.pojo.Teacher">
        <property name="tid" value="2"></property>
        <property name="tname" value="王安石"></property>
    </bean>
</beans>

9.IOC之Bean的单例和多例

​ scope 属性设置对象的域

​ singleton 表示单例(默认)

​ 1、Spring容器在创建的时候,就会创建Bean对象

​ 2、每次调用getBean都返回spring容器中的唯一一个对象

​ prototype 表示多例

​ 1、多例在Spring容器被创建的时候,不会跟着一起被创建。

​ 2、每次调用getBean都会创建一个新对象返回

10.Spring整合MyBatis(重要)

① 导入jar包

(1) MyBatis的jar

(2) SpringIOC的jar

(3) mysql的jar

(4) Spring-jdbc和Spring-tx和Spring-aop的jar

(5) spring-web的jar

(6) spring整合Mybatis的jar

② 创建web项目,并在src下创建MVC的包结构

(1) com.bjsxt.controller

(2) com.bjsxt.service

(3) com.bjsxt.mapper

(4) com.bjsxt.pojo

③ 在src下创建并配置applicationcontext.xml文件

(1) 数据源bean,并使用属性注入完成数据库参数的声明

(2) 工厂bean,使用依赖注入将数据源bean注入到工厂bean中

(3) mapper扫描bean,使用依赖注入将工厂bean注入到mapper的bean中,并使用属性注入声明mapper扫描路径

注意:

mapper接口扫描的实例化对象,会直接存储到Spring容器对象中,默认其id为接口名首字母小写。比如“UserMapper”“userMapper”

(4) 声明业务层的bean,并使用依赖注入将mapper接口对象注入到业务层bean中

注意:

必须在业务层的代码中声明Mapper接口属性,并提供get/set方法

(5) 配置其它bean

④ 在 mapper层声明数据库操作代码

⑤ 在service层声明业务逻辑代码,并声明mapper层属性以及提供get/set方法

⑥ 在controller层声明Servlet代码,并声明业务层属性,同时使用init方法完成Spring容器内资源的初始化加载,也就是获取业务层对象,并赋值给业务层属性。

⑦ 在web.xml文件中配置Spring容器对象配置文件的路径参数,并配置监听器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值