Spring——First

maven项目

主配置文件——pom.xml

写坐标的标签

<dependencies>
    <dependency>
        .......
    </dependency>
</dependencies>

目录存放的文件

SpringIOC

控制反转:解耦

高内聚,低耦合编程思想

硬编码实现解耦
  1. 不直接new对象,通过反射创建
  2. 将需要创建的对象独立保存在资源文件中,动态加载
演示解耦过程
/*
    演示解耦过程
 */
public class Test01 {
    public static void main(String[] args) throws Exception {
//高耦合
//        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//        Connection root = DriverManager.getConnection("jdbc:mysql://39.107.121.247:3306/myschool?serverTimezone=GMT","root","123456");
//        System.out.println(root);

//中耦合
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        Connection root1 = DriverManager.getConnection("jdbc:mysql://39.107.121.247:3306/myschool?serverTimezone=GMT","root","123456");
//        System.out.println(root1);

//低耦合
        //1.创建工具类
        Properties properties = new Properties();
        //2.加载文件
        InputStream inputStream = Test01.class.getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(inputStream);
        //3.通过key获取value
        String driver = properties.getProperty("msg1");
        String url = properties.getProperty("msg2");
        String name = properties.getProperty("msg3");
        String pwd = properties.getProperty("msg4");
        //4.获取链接
        Class.forName(driver);
        Connection root = DriverManager.getConnection(url,name,pwd);
        System.out.println(root);
    }
}
资源文件(key=value)

三层串联调用

方式1:高耦合

方式1的优化

beans.properties:定义一些需要创建的对象

BeansFactory:工具类

Spring两大思想

一、IOC控制反转  DI依赖注入

二、AOP面向切面编程

IOC控制反转

IOC(控制反转)

作用是解耦,使用IOC容器管理项目组件之间的耦合关系

IOC是Spring框架的核心思想之一,主要作用是解耦,IOC是指将创建对象的控制权转移给Spring框架进行管理。由Spring框架根据配置文或者注解等方式,创建bean对象并管理各个bean对象之间的依赖关系。使对象之间形成松散耦合的关系,实现解耦

控制:指的是对象创建(实例化、管理)的权利

反转:控制权交给外部环境(Spring框架、IOC容器)

IOC容器

springIOC搭建spring容器管理程序中的各个组件(class)让程序可以实现高内聚,低耦合的编程

spring环境搭建

1.坐标;2.配置文件

springIOC的使用步骤

  1. 找到需要以解耦方式获取实例对象的类
  2. 将需要spring管理的类注入spring容器
  3. 向spring容器中索取java实例(解耦)
//beans.xml文件
<!--注入类-->
    <!--id:注入类的唯一标识;class:注入类的类型-->
    <bean id="student" class="com.apesource.pojo.student"/>
    <!--注入类-->
    <bean id="dao" class="com.apesource.dao.UserDaoImp"/>
    <!--注入日期类-->
    <bean id="date" class="java.util.Date"/>
//test01文件
public class Test01 {
    public static void main(String[] args) {
/*-----向spring容器中索取java实例-----*/

        //1.加载spring核心配置文件,获取spring容器对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");   //通过xml配置文件的相对路径加载

        //2.向spring容器中获取javabean
        student student = (student)applicationContext.getBean("student");
        System.out.println(student);

        
        IUserDao dao = (IUserDao) applicationContext.getBean("dao");
        dao.save();

        Date date = (Date)applicationContext.getBean("date");
        System.out.println(date);
    }
}

注:

  • ClassPathXmlApplicationContext【通过加载主配置文件的相对路径,获取spring容器】
  • FileSystemXmlApplicationContext【通过加载主配置文件的绝对路径,获取spring容器】
  • AnnotationConfigApplicationContext【通过加载配置类,获取spring容器】
  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值