• Spring学习篇(一)


    Spring学习篇(一)

    1 准备工作

    1.1 在pom.xml文件里面导入spring依赖

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>5.3.22version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.2 在java文件下创建dao包和service包

    在这里插入图片描述

    1.3 在dao包下创建ProductDao类

    package dao;
    
    public class ProductDao {
        public void select(){
            System.out.println("在数据库中 查询所有商品 ");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1.4 在service包下创建ProductService类

    package service;
    
    import dao.ProductDao;
    
    public class ProductService {
        public ProductService() {
        }
        public void select(){
            System.out.println("在业务层中 调用dao层方法 ");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.5 在resources文件夹下创建一个spring.xml文件

    1.5.1 右键new—>xml configuration---->Spring config

    在这里插入图片描述

    1.5.2 输入spring.xml然后回车即可

    在这里插入图片描述

    2 使用spring创建对象(IOC)

    2.0 bean标签属性总结

      id表示组件在容器中的唯一标识 一般写这个
      name标识在容器中的名称 唯一,可以写特殊符号.与id的作用类似
      scope 作用范围 默认是单例模式(singleton 单例),单例是每次获取的对象都是同一个实例
      还有原型模式(prototype),每次getBean就能获取新的实例
      class 类的全限定名(先要容器初始化谁,你就写谁)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.1 使用bean标签与类关联

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
           <bean id="pd" class="dao.ProductDao>
           "ps" class="service.ProductService">
           bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.2 加载spring配置文件 初始化容器

    ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
    //其中传入的参数是spring配置文件的路径,如果是直接在resoures资源文件夹下面,直接写名称即可
    //如果是自定义文件夹的下面,则路径=自定义文件夹\文件名
    //判断地址是否写对的方案:可以试着点过去,若能正常链接过去,则地址并没有错误
    
    • 1
    • 2
    • 3
    • 4

    2.3 使用getBeans()方法获取创建好的对象并进行方法的调用

     ProductDao pd = (ProductDao) ac.getBean("pd");
     pd.select();
     System.out.println("---------------------");
     ProductService ps = (ProductService) ac.getBean("ps");
     ps.select();
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.4 测试运行截图

    在这里插入图片描述

    2.5 注意事项

    2.5.1 使用getBean去获取对象,最好是使用id
    2.5.2 spring,xml中的bean标签的class值不能有重复的

    3 使用spring注入属性(DI)

    3.1 通过set方法注入

    3.1.1 需要定义属性和其对应的set方法
    private ProductDao pd;
    public void setPd(ProductDao pd) {
        this.pd = pd;
    }
    
    • 1
    • 2
    • 3
    • 4
    3.1.2 在相应的bean标签里面写上property标签
    
    
    
    
    • 1
    • 2
    • 3
    3.1.3 完整代码
    a ProductDao类
    package dao;
    
    public class ProductDao {
        public void select(){
            System.out.println("在数据库中 查询所有商品 ");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    b ProductService类
    package service;
    import dao.ProductDao;
    public class ProductService {
        private ProductDao pd;
        public void setPd(ProductDao pd) {
            this.pd = pd;
        }
    
        public ProductService() {
        }
        //业务层
        public void select(){
            System.out.println("在业务层中 调用dao层方法 ");
            pd.select();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    c test类
    import dao.ProductDao;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import service.ProductService;
    
    public class Test {
        public static void main(String[] args) 
            //加载spring配置文件 初始化容器 初始化时会把容器中的所有对象创建出来
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
            ProductService ps = (ProductService) ac.getBean("ps");
            ps.select();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    3.1.4 测试运行截图

    在这里插入图片描述

    3.2 通过构造方法注入

    3.2.1 创造一个有参构造
    //② 有参
        public ProductService(ProductDao pd) {
            this.pd = pd;
        }
    
    • 1
    • 2
    • 3
    • 4
    3.2.2 在对应的bean中创建constructor标签
    <constructor-arg name="pd" ref="pd">constructor-arg>
    
    • 1
    3.2.3 完整代码
    a productDao类
    package dao;
    public class ProductDao {
        public void select(){
            System.out.println("在数据库中 查询所有商品 ");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    b productService类
    package service;
    
    import dao.ProductDao;
    
    public class ProductService {
        private ProductDao pd;
        public ProductService() {
        }
        //② 有参
        public ProductService(ProductDao pd) {
            this.pd = pd;
        }
        //业务层
        public void select(){
            System.out.println("在业务层中 调用dao层方法 ");
            pd.select();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    c test类
    import dao.ProductDao;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import service.ProductService;
    public class Test {
        public static void main(String[] args) 
            //加载spring配置文件 初始化容器 初始化时会把容器中的所有对象创建出来
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
            ProductService ps = (ProductService) ac.getBean("ps");
            ps.select();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    3.2.4 测试运行截图

    在这里插入图片描述

  • 相关阅读:
    十、Echart图表 之 dataZoom区域缩放 基本使用与配置大全
    Django基本知识
    【JavaWeb】响应报文详解
    Day121.ElasticSearch:概述、安装、基本操作、DSL高级查询
    基于生成对抗网络的动漫人脸生成实例
    代码随想录算法训练营第49天|121. 买卖股票的最佳时机,买卖股票的最佳时机II
    Docker部署服务(实战)
    第2章丨IRIS Global 结构
    图像分类(二) 全面解读复现ZFNet
    网络安全(黑客)自学
  • 原文地址:https://blog.csdn.net/SSS4362/article/details/127798029