EmmmuaCode EmmmuaCode
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)

wufan

海内存知己,天涯若比邻。
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)
  • Spring5-基础

    • Spring5 入门案例
    • Spring核心之控制反转IOC
      • IOC 概念和原理
        • 什么是IOC
        • IOC 底层原理
        • 图解IOC底层原理
      • IOC 接口(BeanFactory)
      • 概念
      • IOC操作Bean管理-基于xml方式
        • 第一种注入方式:使用set方法进行注入
        • 第二种注入方式:使用有参数构造方法进行注入
        • p名称空间注入(了解)
      • xml注入其他类型属性
        • 字面量
        • 注入属性-外部bean
        • 注入属性-内部bean
        • 注入属性-级联赋值
        • xml方式注入集合属性
      • FactoryBean
      • bean 作用域
      • bean 生命周期
      • xml 自动装配
      • 引入外部属性文件
      • IOC操作Bean管理-基于注解方式
        • 基于注解方式实现对象创建
        • 基于注解方式实现属性注入
        • 完全注解开发
    • Spring核心之面向切面编程AOP
    • Jdbc Template
    • Spring5 事务
    • Spring5 新功能
    • Spring5 Webflux
  • SpringMVC-基础

    • SpringMVC 入门案例
    • RequestMapping注解
    • SpringMVC 获取请求参数
    • 域对象共享数据
    • SpringMVC的视图
    • RESTFul
    • RESTful案例
    • HttpMessageConverter
    • 文件上传和下载
    • 拦截器
    • 异常处理器
    • 注解配置SpringMVC
    • SpringMVC执行流程
  • SpringBoot2-基础

    • Spring与SpringBoot
    • SpringBoot2 快速入门
    • SpringBoot2 常用注解
    • 了解自动装配原理
  • SpringBoot2-核心功能

    • 配置文件
    • Web开发
    • 数据访问
    • 单元测试
  • SpringCloud-微服务架构

    • 微服务架构理论入门
    • SpringCloud 初步构建
    • Eureka 服务注册与发现
    • ZooKeeper 服务注册与发现
    • Consul 服务注册与发现
    • Ribbon 负载均衡服务调用
    • OpenFeign 简化服务调用
    • Hystrix 服务降级|熔断
    • GateWay 服务网关
    • Config 服务配置中心 与 BUS 消息总线
    • Stream 消息驱动
    • Sleuth 分布式请求链路追踪
  • SpringCloud Alibaba

    • SpringCloud Alibaba 简介
    • Nacos 服务发现、配置管理和服务管理平台
    • Sentinel 实现熔断与限流
    • Seata 分布式事务
  • Spring Security

    • SpringSecurity 概述
    • SpringSecurity 入门案例
    • SpringSecurity 登录认证详解
    • SpringSecurity 授权
    • SpringSecurity 跨域
    • SpringSecurity 遗留小问题
  • studynotes
  • Spring
  • Spring5
wufan
2021-12-20
目录

Spring核心之控制反转IOC

# Spring核心之控制反转IOC

# IOC 概念和原理

# 什么是IOC

  1. 控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理
  2. 使用IOC目的:为了降低耦合度
  3. 做入门案例就是IOC实现

# IOC 底层原理

  1. xml解析、工厂模式、反射
  • 原始方式与工厂模式对比

01

# 图解IOC底层原理

02

# IOC 接口(BeanFactory)

  1. IOC思想基于IOC容器完成,IOC容器底层就是对象工厂

  2. Spring提供IOC容器实现两种方式:(两个接口)

    • BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用

      特点:加载配置文件时候不会创建对象,在获取对象(或者使用对象)才去创建对象

    • ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人人员进行调用

      特点:加载配置文件时候就会把在配置文件对象进行创建

  3. ApplicationContext接口有实现类

02

  • FileSystemXmlApplicationContext

    configLocation:要写上配置文件在系统盘(某个盘)里的路径

  • ClassPathXmlApplicationContext

    configLocation:要写上类路径

# IOC操作Bean管理

# 概念

  1. 什么是Bean管理
    • Bean管理指的是两个操作
      1. Spring 创建对象
      2. Spring 注入属性
  2. Bean管理操作有两种方式
    1. 基于xml配置文件方式实现
    2. 基于注解方式实现

# IOC操作Bean管理-基于xml方式

  1. 基于xml方式创建对象

    <!--配置User对象创建-->
    <bean id="user" class="com.frx01.spring5.User"></bean>
    
    1
    2
    1. 在Spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
    2. 在bean标签有很多属性,介绍常用的属性
      • id属性:唯一标识
      • class属性:类全路径(包和类路径)
    3. 创建对象时候,默认也是执行无参构造方法
  2. 基于xml方式注入属性

    1. DI:依赖注入,就是注入属性

      DI是IOC一种具体实现,表示依赖注入,注入属性是在创建对象的基础之上进行完成

# 第一种注入方式:使用set方法进行注入

  1. 创建类,定义属性和对应的set方法
 /**
   * 演示set方法注入属性
   */
  public class Book {
  
      //创建属性
      private String bname;
      private String bauthor;
  
      //创建属性对应set方法
      public void setBauthor(String bauthor) {
          this.bauthor = bauthor;
      }
      public void setBname(String bname) {
          this.bname = bname;
      }
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  1. 在Spring配置文件对象创建,配置属性注入
 <!--set方法注入属性-->
      <bean id="book" class="com.frx01.spring5.testdemo.Book">
          <!--使用property完成属性注入
              name:类里面属性名称
              value:向属性注入的值-->
          <property name="bname" value="易筋经"></property>
          <property name="bauthor" value="达摩老祖"></property>
  
      </bean>
1
2
3
4
5
6
7
8
9

# 第二种注入方式:使用有参数构造方法进行注入

  1. 创建类,定义属性,创建属性对应有参构造方法
/**
 *使用有参数构造注入
 */
public class Orders {

    private String oname;
    private String address;

    public Orders(String oname, String address) {
        this.oname = oname;
        this.address = address;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
  1. 在Spring配置文件中进行配置
<!--用有参构造注入属性-->
    <bean id="orders" class="com.frx01.spring5.testdemo.Orders">
        <constructor-arg name="oname" value="computer"></constructor-arg>
        <constructor-arg name="address"value="China"></constructor-arg>
    </bean>
1
2
3
4
5

# p名称空间注入(了解)

  1. 使用p名称空间注入,可以简化基于xml配置方式
    1. 添加p名称空间在配置文件中
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>
1
2
3
4
  1. 进行属性注入,在bean标签里面进行操作
 <!--set方法注入属性-->
        <bean id="book" class="com.frx01.spring5.testdemo.Book" p:bname="九阳神功" p:bauthor="无名氏">
        </bean>
1
2
3

# xml注入其他类型属性

# 字面量

  1. null值
     <!--null值-->
        <property name="address">
            <null/>
        </property>
1
2
3
4
  1. 属性值包含特殊符号
    <!--属性值包含特殊符号
         1. 把<>进行转义 &lt;&gt;
         2. 把带特殊符号内容写到CDATA
		-->
        <property name="address">
            <value><![CDATA[<<南京>>]]></value>
        </property>
1
2
3
4
5
6
7

# 注入属性-外部bean

  1. 创建两个类service类和dao类
  2. 在service调用dao里面的方法
package com.frx01.service;

import com.frx01.dao.UserDao;
import com.frx01.dao.UserDaoImpl;

/**
 * @author frx
 * @version 1.0
 * @date 2021/12/2  21:31
 */
public class UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("service add.................");
        userDao.update();


    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  1. 在spring配置文件中进行配置

    	<!--1 配置service和dao对象相关的创建-->
    <bean id="userService" class="com.frx01.spring5.service.UserService">
	<!--注入userDao对象
        name属性值:类里面属性名称
        ref属性:创建userDao对象bean标签id值-->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="UserDao" class="com.frx01.spring5.dao.UserDaoImpl"></bean>
	<bean id="userDaoImpl" class="com.frx01.spring5.dao.UserDaoImpl"/></bean>

1
2
3
4
5
6
7
8
9
10
11

# 注入属性-内部bean

  1. 一对多关系,部门和员工

一个部门有多个员工,一个员工属于一个部门,部门是一,员工是多。

  1. 在实体类之间表示一对多的关系,员工所属的部门,使用对象类型属性进行表示
public class Dept {

    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }
}

1
2
3
4
5
6
7
8
9
//员工类
public class Emp {
    private String name;
    private String gender;

    //员工属于某一个部门,使用对象表示
    private Dept dept;

    public void setName(String name) {
        this.name = name;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  1. 在Spring配置文件
    <!--内部bean-->
    <bean id="Emp" class="com.frx01.spring5.bean.Emp">
    <!--设置两个普通属性-->
        <property name="name" value="lucy"></property>
        <property name="gender" value="女"></property>
    <!--设置对象类型的属性-->
        <property name="dept">
            <bean id="dept" class="com.frx01.spring5.bean.Dept">
                <property name="dname" value="保安部"></property>
            </bean>

        </property>
    </bean>
1
2
3
4
5
6
7
8
9
10
11
12
13

# 注入属性-级联赋值

  1. 第一种写法
   <!--内部bean-->
    <bean id="Emp" class="com.frx01.spring5.bean.Emp">
    <!--设置两个普通属性-->
        <property name="name" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.frx01.spring5.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
1
2
3
4
5
6
7
8
9
10
11
  1. 第二种写法

    在Emp类中添加属性dept的getDept方法

   <!--内部bean-->
    <bean id="Emp" class="com.frx01.spring5.bean.Emp">
    <!--设置两个普通属性-->
        <property name="name" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
        <property name="dept.dname" value="技术部"></property>
    </bean>
    <bean id="dept" class="com.frx01.spring5.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
1
2
3
4
5
6
7
8
9
10
11
12

# xml方式注入集合属性

  1. 注入数组类型属性
  2. 注入List集合类型属性
  3. 注入Map集合类型属性

第一步,创建类,定义数组,list,map,set类型属性,生成对应set方法

public class Stu {
//    1.数组类型的属性
    private String[] course;

//    2.List集合类型
    private List<String> list;

//    3.Map集合类型
    private Map<String,String> map;

//    4.set集合类型
    private Set<String> set;
    public void setCourse(String[] course) {
        this.course = course;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

第二步,在Spring配置文件进行配置

<!--完成集合类型属性的注入-->
    <bean id="stu" class="com.frx01.spring5.collectiontype.Stu">
        <!--数组类型的属性注入-->
        <property name="courses">
            <array>
                <value>java课程</value>
                <value>数据库课程</value>
            </array>
        </property>
        <!--list属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>李四</value>
            </list>
        </property>
        <!--Map类型注入-->
        <property name="map">
            <map>
                <entry key="JAVA" value="java" ></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>
        <!--set集合注入-->
        <property name="set">
            <set>
                <value>MySQL</value>
                <value>redis</value>
            </set>
        </property>
    </bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  • 在集合里面设置对象类型的值
     <!--注入list集合类型,值是对象-->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>
<!--    创建多个course对象-->
    <bean id="course1" class="com.frx01.spring5.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.frx01.spring5.collectiontype.Course">
        <property name="cname" value="Mybatis框架"></property>
    </bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 把集合注入部分提取出来
  1. 在Spring配置文件中引入名称空间util
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/util/spring-beans.xsd
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
        <!--把集合注入部分提取出来-->
</beans>
1
2
3
4
5
6
7
8
  1. 使用util标签完成list集合注入提取
       <!--1.把提取list集合类型属性注入-->
    <util:list id="bookList">
        <value>易筋经</value>
        <value>九阳神功</value>
        <value>九阴真经</value>
    </util:list>

        <!--2.提取list集合类型属性注入使用-->
    <bean id="book" class="com.frx01.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>
1
2
3
4
5
6
7
8
9
10
11

# FactoryBean

  1. Spring有两种类型bean,一种普通bean,另一种工厂bean(FactoryBean)
  2. 普通bean:在配置文件中定义bean类型就是返回类型
  3. 工厂bean:在配置文件中定义bean类型可以和返回类型不一样

第一步,创建类,让这个类作为工厂bean,实现接口FactoryBean

/**
 * @author frx
 * @version 1.0
 * @date 2021/12/4  17:42
 */
public class MyBean implements FactoryBean<Course> {

    //定义返回bean
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        String cou[]={"math,english"};
        course.setCname(cou);
        return course;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

第二步,配置Spring配置文件

<bean id="myBean" class="com.frx01.spring5.factorybean.MyBean"></bean>
1

# bean 作用域

  1. 在Spring里面,可以设置bean实例是单实例还是多实例
  2. 在Spring里面,默认情况下,bean是单实例对象
    @Test
    public void testCollection2(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
        Book book =context.getBean("book", Book.class);
        Book book1 =context.getBean("book", Book.class);
        System.out.println(book);
        System.out.println(book1);

    }
1
2
3
4
5
6
7
8
9

02

  1. 如何设置单实例还是多实例

    1. 在Spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例
    2. scope属性值

    第一个值默认值,singleton,表示单实例对象

    第二个值prototype,表示是多实例对象

    <!--2 提取list集合类型属性注入使用-->
    <bean id="myBean" class="com.frx01.spring5.factorybean.MyBean" scope="prototype"></bean>
</beans>
1
2
3
    @Test
    public void testCollection2(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
        Book book =context.getBean("book", Book.class);
        Book book1 =context.getBean("book", Book.class);
        System.out.println(book);
        System.out.println(book1);

    }
1
2
3
4
5
6
7
8
9

02

  • singleton和prototype区别
  1. singleton表示单实例,prototype表示多实例
  2. 设置scope值singleton时候,加载spring配置文件就会创建一个单实例对象
    • 设置scope值是prototype时候,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建多实例对象

# bean 生命周期

  1. 生命周期

    • 从对象创建到对象销毁的过程
  2. bean生命周期

    1. 通过构造器创建bean实例(无参数构造)

    2. 为bean的属性设置值和对其他bean引用(调用set方法)

    3. 调用bean初始化的方法(需要进行配置初始化方法)

    4. bean可以使用了(对象获取到了)

    5. 当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)

  3. 演示bean生命周期

public class Orders {

//  无参数构造
    public Orders() {
        System.out.println("第一步 执行无参构造创建bean实例");
    }

    private String oname;

    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("第二步 调用set方法设置属性的值");
    }

//    创建执行的初始化方法
    public void initMethod(){
        System.out.println("第三步 执行初始化方法");
    }

    //    创建执行的销毁方法
    public void destroyMethod(){
        System.out.println("第五步 执行销毁方法");
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    @Test
    public void testCollection4() {
//        ApplicationContext context =
//                new ClassPathXmlApplicationContext("bean4.xml");
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);

//        手动让bean实例销毁
        context.close();

    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    <bean id="orders" class="com.frx01.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>
1
2
3

02

  1. bean的后置处理器,bean生命周期有七步

    1. 通过构造器创建bean实例(无参数构造)
    2. 为bean的属性设置值和对其他bean引用(调用set方法)
    3. 把bean实例传递bean前置处理器的方法postProcessBeforeInitialization
    4. 调用bean初始化的方法(需要进行配置初始化方法)
    5. 把bean实例传递bean后置处理器的方法postProcessAfterInitialization
    6. bean可以使用了(对象获取到了)
    7. 当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)
  2. 演示添加后置处理器效果

    1. 创建一个类,实现接口BeanPostProcessor,创建后处理器
    public class MyBeanPost implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("在初始化之前执行的方法");
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("在初始化之后执行的方法");
            return bean;
        }
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        @Test
        public void testCollection4() {
    //        ApplicationContext context =
    //                new ClassPathXmlApplicationContext("bean4.xml");
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("bean4.xml");
            Orders orders = context.getBean("orders", Orders.class);
            System.out.println("第四步 获取创建bean实例对象");
            System.out.println(orders);
    
    //        手动让bean实例销毁
            context.close();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!--    配置后置处理器-->
        <bean id="MyBeanPost" class="com.frx01.spring5.bean.MyBeanPost"></bean>
    </beans>
    
    1
    2
    3

    02

# xml 自动装配

  1. 什么是自动装配

    • 根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入
  2. 演示自动装配过程

  • 根据属性名称自动注入
<!--    实现自动装配
        bean标签属性autowire,配置自动装配
        autowire属性常用两个值:
            byName根据属性名称注入,注入值bean的id值和类属性名称一样
            byType根据属性类型注入
       -->
    <bean id="emp" class="com.frx01.spring5.autowire.Emp" autowire="byName">
<!--        <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.frx01.spring5.autowire.Dept"></bean>

1
2
3
4
5
6
7
8
9
10
11
  • 根据属性类型自动注入
    <bean id="emp" class="com.frx01.spring5.autowire.Emp" autowire="byType">
<!--        <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.frx01.spring5.autowire.Dept"></bean>
1
2
3
4

# 引入外部属性文件

  1. 直接配置数据库信息

    1. 配置德鲁伊连接池
    2. 引入德鲁伊连接池依赖jar包
<!--直接配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
    </bean>
1
2
3
4
5
6
7
  1. 引入外部属性文件配置数据库连接池
  • 创建外部属性文件,properteis格式文件,写数据库信息

02

  • 把外部properties属性文件引入到spring配置文件中
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                            http://www.springframework.org/schema/context  http://www.springframework.org/schema/util/spring-util.context.xsd">
1
2
3
4
5
6
7
8
  • 在Spring配置文件使用标签引入外部属性文件
<!--    引入外部属性文件-->
    <context:property-placeholder location="classpath*:jdbc.properties"/>
    <!--配置连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.userName}"></property>
        <property name="password" value="${prop.passwd}"></property>
        </bean>
1
2
3
4
5
6
7
8
9

# IOC操作Bean管理-基于注解方式

  1. 什么是注解

    1. 注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)
    2. 使用注解,注解作用在类上面,方法上面,属性上面
    3. 使用注解目的:简化xml配置
  2. Spring针对Bean管理中创建对象提供注解

    1. @Component
    2. @Service
    3. @Controller
    4. @repository
  • 上面四个注解功能是一样的,都可以用来创建bean实例

# 基于注解方式实现对象创建

第一步 引入依赖

02

第二步 开启组件扫描

    <!--开启组件扫描
        1 如果扫描多个包 使用逗号隔开
        2 扫描包上层目录-->
    <context:component-scan base-package="com.frx01.spring5"></context:component-scan>
1
2
3
4

第三步创建类,在类上面添加创建对象注解

//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//  UserService -- userService
@Component(value = "userService")  //<bean id="userService" class=".."/>
public class UserService {

    public void add(){
        System.out.println("service add......");
    }
}

1
2
3
4
5
6
7
8
9
10
11
  • 开启组件扫描细节配置

    <!--示例1
        use-default-filters="false 表示现在不使用默认filter,不扫描全部,自己配置filter
        context:include-filler,设置扫描那些内容-->
    <context:component-scan base-package="com.frx01" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--示例2
        下面配置扫描包所有内容
        context:exclude-filter:设置那些内容不进行扫描-->
    <context:component-scan base-package="com.frx01">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 基于注解方式实现属性注入

  1. @AutoWired:根据属性类型自动装配

第一步把service和dao对象创建,在service和dao类添加创建对象注解

第二步在service注入dao对象,在service类添加dao类型属性,在属性上面使用注解

@Service
public class UserService {

//    定义dao类型的属性
//    不需要添加set方法
//    添加注入属性注解
    @Autowired
    private UserDao userDao;


    public void add(){
        System.out.println("service add......");
        userDao.add();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  1. @Qualifier:根据属性名称注入

这个@Qualifier注解的使用,和上面@Autowired一起使用

@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao{

    @Override
    public void add() {
        System.out.println("dao add......");

    }
}
1
2
3
4
5
6
7
8
9
public class UserService {

//    定义dao类型的属性
//    不需要添加set方法
//    添加注入属性注解
    @Autowired  //根据类型进行注入
    @Qualifier(value = "userDaoImpl1")
    private UserDao userDao;


    public void add(){
        System.out.println("service add......");
        userDao.add();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  1. @Resource:可以根据类型注入,可以根据名称注入
//    @Resource  //根据类型进行注入
    @Resource(name="userDaoImpl1") //根据名称进行注入
    private UserDao userDao;

1
2
3
4

Resource是Javax包里面的

  1. @Value:注入普通类型属性
    @Value(value = "abc")
    private String name;
1
2

# 完全注解开发

  1. 创建配置类,替代xml配置文件
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = "com.frx01")
public class SpringConfig {
}

1
2
3
4
5
  1. 编写测试类
   	@Test
    public void testService2(){
//        加载配置类
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }
1
2
3
4
5
6
7
8
9

02

#Spring5
上次更新: 2024/04/21, 09:42:22
Spring5 入门案例
Spring核心之面向切面编程AOP

← Spring5 入门案例 Spring核心之面向切面编程AOP→

最近更新
01
微信支付功能的实现与流程
11-21
02
购物车与结算区域的深入优化与功能完善
11-21
03
购物车与结算区域的功能实现与优化
11-21
更多文章>
Theme by Vdoing | Copyright © 2023-2024 EmmmuaCode | 黔ICP备2022009864号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式