Spring框架

Spring框架两大核心:

                IOC(控制反转)/ DI(依赖注入)

                AOP(面向切面编程)

springIOC容器:管理项目,管理Bean,(bean容器)
         bean=========》计算机世界中可以重复利用的组件
         javaBean=====》Java世界中可以重复利用的组件
        实体类========》需要实例化使用

       bean >  javaBean  >  实体类


springIOC容器管理Bean的优势:符合高内聚,低耦合的编程思想

-------------------------初步实现解耦合---------------------------


 
  解耦合实现步骤:
     1.通过反射创建对象
     2.将需要创建对象的完全限定类名 单独保存在资源文件中

//先导坐标
public class Test01 {
    public static void main(String[] args) throws Exception{
        /**********************建立数据库链接对象方式1(耦合性强)************************/
//        //1.注册驱动
//        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//        //2.获取连接
//        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/2204db?serverTimezone=GMT","root","123456");
//        System.out.println(connection);

        /**********************建立数据库链接对象方式2(反射解耦)************************/
//        //1.注册驱动(反射的方式)
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        //2.获取连接
//        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/2204db?serverTimezone=GMT","root","123456");
//        System.out.println(connection);

        /**********************建立数据库链接对象方式3(深层次解耦)************************/
        //1.创建资源对象
        Properties properties = new Properties();
        //2.创建流对象(前面创建了jdbc.properties)
        InputStream is = Test01.class.getClassLoader().getResourceAsStream("jdbc.properties");
        //3.加载流
        properties.load(is);
        //4.通过key获取value
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String name = properties.getProperty("name");
        String pwd = properties.getProperty("pwd");
        //5.注册驱动
        Class.forName(driver);
        //6.获取连接
        Connection connection = DriverManager.getConnection(url,name,pwd);
        System.out.println(connection);

    }
}

---------------------------IOC(控制反转)--------------------

创建好spring主配置文件后,加入类

<!-- spring主配置文件===SpringIOC容器 -->
<?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 class="com.apesource.dao.DemoDaoImp" id="dao"></bean>
    <!--一个类配置一个<bean></bean>标签 class是类的路径 id是唯一标识 -->

    <bean class="com.apesource.service.DemoServiceImp" id="service"></bean>
    <bean class="com.apesource.controller.DemoControllerImp" id="controller"></bean>

</beans>

获取容器中对象的方式:

spring容器的两大核心对象

                ApplicationContext:子接口,默认管理bean的方式是单例(性能好,不安全)

                BeanFactory:父接口,默认管理bean的方式是多例(性能差,安全)

public class Test01 {
    public static void main(String[] args) {
        //加载spring的主配置文件
        ApplicationContext/* BeanFactory*/ applicationContext =
 new ClassPathXmlApplicationContext("beans.xml");
        //获取对象
        IDemoController controller = 
(IDemoController) applicationContext.getBean("controller");
        controller.save();
    }
}

获取容器的方式有三种:

(1)ClassPathXmlApplicationContext("beans.xml")-------->通过xml的“相对路径”获取spring容器

(2)FileSystemXmlApplicationContext("D:\Mavena\Maven_Spring\src\main\resources\beans.xml")------>通过xml的“绝对路径”获取spring容器

(3)AnnotationConfigApplicationContext()------------------->通过加载配置类获取spring容器

=========================DI注入================================

注入方式:

支持类型

自建的类(串联调用)

String和基本数据类型

复杂类型(构造注入不支持)

                1.set注入

步骤:

①类中写set方法

        1》基本数据和String

package com.apesource.pojo;

/**
 * @Author Maven
 * @VERSION 1.0
 * @since 2022/7/14
 */
public class Teacher {
    private String tname;
    private int tage;
    private double tmoney;
    private String thobby;
//set注入
    public void setTname(String tname) {
        this.tname = tname;
    }

    public void setTage(int tage) {
        this.tage = tage;
    }

    public void setTmoney(double tmoney) {
        this.tmoney = tmoney;
    }

    public void setThobby(String thobby) {
        this.thobby = thobby;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tname='" + tname + '\'' +
                ", tage=&
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

l1050188952

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值