Spring(一)概述、IOC、在Spring中对bean进行管理、在Spring中对数据访问层进行管理、在Spring中集成MyBatis

发布于:2024-12-18 ⋅ 阅读:(50) ⋅ 点赞:(0)

文章目录

一、Spring概述

1、Spring是什么?

(1)轻量级的

(2)IOC

(3)AOP

(4)一站式框架

(5)Spring框架体系结构

2、搭建Spring中的HelloWorld

(1)创建maven项目下载Spring核心jar

(2)创建并编写Spring配置文件spring.xml

二、IOC(Inversion Of Control 控制反转)

1、定义

2、从Spring框架中获取对象

(1)方式一:通过bean的id获取

(2)方式二:通过bean的id和java类的class对象获取

4、ClassPathXmlApplicationContext类

三、Spring如何对Bean进行管理

1、方式一:基于XML(了解)

(1)bean

(2)bean中的属性

(3)依赖注入

2、方式二:使用注解方式(常用)

(1)在spring.xml文件中开启注解扫描

(2)使用注解创建对象

3、注解方式中的自动注入

(1)方式一:使用Spring框架提供的@Autowired注解

(2)方式二:使用jdk中提供的@Resource注解

四、Spring如何对数据访问层进行管理

1、配置

(1)下载Spring-jdbc数据访问层jar包和阿里数据源jar包

(2)在Spring容器中创建阿里巴巴数据库连接中的数据源对象

(3)在Spring容器中创建JdbcTemplate类对象

五、在Spring中集成MyBatis

1、配置

(1)导入mybatis的jar包

(2)导入spring结合mybatis的插件包

(3)配置 sqlSessionFactory

(4)生成接口代理

(5)在service中注入Dao代理接口,此接口由Spring生成的接口代理对象实现


一、Spring概述

1、Spring是什么?

Spring是一个轻量级的IOC和AOP一站式Java开发框架,是为了简化企业级应用开发而生的。

(1)轻量级的

  • Spring框架使用的jar包都比较小,一般在1M以下或者几百KB。
  • 并且Spring框架的核心功能所需的jar包总共在3M左右
  • 而且Spring框架运行占用的资源少,运行效率高。

(2)IOC

即Inversion of Control,缩写为IOC,其汉语意思是控制反转,想要表达的意思在Spring框架中是由Spring框架管理对象,而非传统实现中的由程序代码直接进行操纵。

(3)AOP

即Aspect Oriented Programming,缩写为AOP,其汉语意思是面向切面编程。AOP是一种编程思想,是对OOP(面向对象编程)的一种补充。面向对象编程是将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面

(4)一站式框架

Spring框架本身也提供了数据访问功能web功能,并且使用Spring框架也可以很好地管理其他框架

(5)Spring框架体系结构

2、搭建Spring中的HelloWorld

(1)创建maven项目下载Spring核心jar

<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>

(2)创建并编写Spring配置文件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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="admin" class="com.ffyc.springpro1.model.Admin"> </bean>
</beans>

二、IOC(Inversion Of Control 控制反转)

1、定义

在Spring框架中,将原本在程序中手动创建对象的控制权反转给了Spring框架,让Spring框架来负责对对象的管理,具体的管理内容包括对象的创建、对象的初始化、对象功能的增强、对象与对象之间的依赖关系和对象的销毁,这个行为就被称为IOC(控制反转),我们在哪里需要使用对象,直接从Spring框架中获取即可。

2、从Spring框架中获取对象

获取的方式有两种:

(1)方式一:通过bean的id获取

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Admin admin = (Admin) applicationContext.getBean("admin");
        System.out.println(admin);

(2)方式二:通过bean的id和java类的class对象获取

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Admin admin1 = applicationContext.getBean("admin", Admin.class);
        System.out.println(admin1);

3、Spring框架只会为每一个类对应创建一个对象

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Admin admin = (Admin) applicationContext.getBean("admin");
        Admin admin1 = applicationContext.getBean("admin", Admin.class);
        System.out.println(admin == admin1);

4、ClassPathXmlApplicationContext类

ClassPathXmlApplicationContext类是Spring框架落地实现功能的具体类,我们可以将其理解为是一个生成并管理对象的容器,其还有两个别称,IOC容器和Spring容器。

三、Spring如何对Bean进行管理

1、方式一:基于XML(了解)

(1)bean

bean是由Spring创建出来的对象,和我们程序员自己new出来的对象是有区别的

(2)bean中的属性

id:为生成对象指定一个名字

name:为生成对象指定多个名字

class:需要让Spring管理的某个类的全类名

scope:

  • singleton(单例的) 单例模式,Spring框架只为该类创建一个对象并且是在Spring容器启动时进行创建的。
<bean id="admin" name="admin1,admin2" scope="singleton" class="com.ffyc.springpro1.model.Admin">
    </bean>
  •  prototype(原型的) 多例模式,在程序中每次获取对象时,Spring都会创建一个新的对象
<bean id="admin" name="admin1,admin2" scope="prototype" class="com.ffyc.springpro1.model.Admin">
    </bean>

(3)依赖注入

定义:在Spring创建对象时,为对象中的属性进行赋值的过程就被称为是依赖注入。

  • 依赖注入的方式一:通过属性的set方法注入
<bean id="admin" name="admin1,admin2" scope="prototype" class="com.ffyc.springpro1.model.Admin">
           <property name="id" value="1"></property>
           <property name="account" value="tom"></property>
           <property name="password" value="abc"></property>
    </bean>
public static void main(String[] args) {
        ApplicationContext applicationContext = new 
        ClassPathXmlApplicationContext("spring.xml");
        Admin admin1 = applicationContext.getBean("admin1", Admin.class);
        System.out.println(admin1);
        Admin admin2 = applicationContext.getBean("admin2", Admin.class);
        System.out.println(admin2);
        System.out.println(admin1 == admin2);
    }

  • 依赖注入的方式二:通过有参构造方法注入
<bean id="admin" name="admin1,admin2" scope="prototype" class="com.ffyc.springpro1.model.Admin">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="account" value="tom"></constructor-arg>
        <constructor-arg name="password" value="abc"></constructor-arg>
    </bean>
public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Admin admin1 = applicationContext.getBean("admin1", Admin.class);
        System.out.println(admin1);
        Admin admin2 = applicationContext.getBean("admin2", Admin.class);
        System.out.println(admin2);
        System.out.println(admin1 == admin2);
    }

2、方式二:使用注解方式(常用)

(1)在spring.xml文件中开启注解扫描

<context:component-scan base-package="包名"> </context:component-scan>

(2)使用注解创建对象

  • 所有类都可以使用@component注解创建对象
@Component (value = "admin") // 等效于 <bean id="admin" class="com.ffyc.springpro1.model.Admin"></bean>
  • Dao层的类,使用@Repository注解创建对象可以更好地扩展后序功能(在不同层使用不同的注解进行标记)
@Repository (value = "adminDao")
  • Web层的类,使用@Service注解创建对象可以更好地扩展后序功能(在不同层使用不同的注解进行标记)
@Service (value = "adminServlet")

3、注解方式中的自动注入

(1)方式一:使用Spring框架提供的@Autowired注解

特点:

  • 必须将@Autowired注解添加到要注入的属性的上面或者属性的set方法的上面
  • 如果将@Autowired注解直接添加到属性的上面,则可以不用编写set方法

注意:

默认情况下,要注入的属性对象不能为空,且@Autowired注解的required属性的默认值是true,则会去Spring容器中寻找对应的属性对象,如果将required属性的值改为false,则不会去Spring容器中寻找对应的属性对象而是直接将null关键字赋给属性。

在自动注入时,查找bean的方式有两种:

  • 通过对象名查找
@Autowired 
@Qualifier (value = "adminDao")
  •  通过属性的类型查找
@Autowired 

(2)方式二:使用jdk中提供的@Resource注解

在自动注入时,查询bean的方式有两种:

  • 通过对象名查找
@Resource (name = "adminDao")
  • 通过属性的类型查找
@Resource

4、注解与XML的对比

(1)注解

优点:高效(与配置文件相比代码更少、书写更简洁)

缺点:使用硬编码的方式将注解写入Java代码之后,修改后的Java代码需要被重新编译

(2)XML

优点:配置和代码分离,如果在XML中进行修改,不需要编译代码,只需要重启服务器即可将新的配置进行加载 

缺点:效率低、在大型项目使用会非常复杂

四、Spring如何对数据访问层进行管理

1、配置

(1)下载Spring-jdbc数据访问层jar包和阿里数据源jar包

<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>

<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>

(2)在Spring容器中创建阿里巴巴数据库连接中的数据源对象

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${uname}"></property>
        <property name="password" value="${pwd}"></property>
        <property name="initialSize" value="${initialSize}"></property>
        <property name="maxActive" value="${maxActive}"></property>
</bean>

注:必须要导入属性文件

<context:property-placeholder location="config.properties"/>

(3)在Spring容器中创建JdbcTemplate类对象

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>

五、在Spring中集成MyBatis

在Spring中集成MyBatis的核心是将SqlSessionFactory交给Spring进行管理,并让Spring管理Dao接口的代理实现。

1、配置

(1)导入mybatis的jar包

<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>

(2)导入spring结合mybatis的插件包

<!--spring结合mybatis的插件包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

(3)配置 sqlSessionFactory

<!--使用spring生成并管理SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property><!--注入数据库连接对象-->
        <property name="configLocation" value="classpath:mybatis.xml"></property><!--指定配置文件-->
        <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property><!--指定映射文件-->
    </bean>

(4)生成接口代理

<!--使用spring生成并管理接口的代理对象-->
    <bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ffyc.ssm.dao"></property><!--对指定包下的接口进行扫描,并生成接口的代理对象-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
        </property>
    </bean>

(5)在service中注入Dao代理接口,此接口由Spring生成的接口代理对象实现

@Autowired
    LoginDao loginDao;


网站公告

今日签到

点亮在社区的每一天
去签到