cgb2106-day15

一,Spring

–1,概述

功能非常丰富,核心的功能是: IOC DI AOP
IOC : 是控制反转,指 把创建对象的过程交给了Spring
DI : 是依赖注入,指把对象间的依赖关系 自动维护
AOP : 是补充了OOP的不足

–2,IOC的XML实现方式

是指把创建对象管理对象的过程交给了Spring框架

创建Maven Module

File-New-Module-Maven-next-输入ModuleName-ok

导入jar包(不需要了,被Spring Boot整合了)
创建Hello类

在这里插入图片描述

创建配置文件
<?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用来作为这个bean的唯一标识,class用来描述类的全路径
    -->
    <bean id="hello" class="cn.tedu.spring.Hello"></bean>
</beans>
测试
package cn.tedu.test;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
    @Test
    public void get(){
        //1,读取配置文件
        ClassPathXmlApplicationContext spring =
                        new ClassPathXmlApplicationContext(
                            "spring-config.xml");
        //2,直接getBean
        Object o = spring.getBean("hello");
        System.out.println(o);//cn.tedu.spring.Hello@45752059
    }
}

–3,IOC的注解实现方式

在这里插入图片描述

创建User类
package cn.tedu.ioc;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Component("a")//自动完成IOC,自己指定bean的名字a -> {"a",new User()}
//@Component  //默认的bean的名字,user
//@Component自动完成IOC, -> {"user",new User()}
//@Controller //spring提供的,用来ioc
//@Service //spring提供的,用来ioc
public class User {
    public void get(){
        System.out.println(123);
    }
}

修改配置文件
<?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: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/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--
        配置Bean的信息
        id用来作为这个bean的唯一标识,class用来描述类的全路径
        IOC->{ "hello" , new Hello() }
    -->
    <bean id="hello" class="cn.tedu.spring.Hello"></bean>


    <!--
        配置包扫描
        base-package指定一个包的路径,扫描范围可以自己定
     -->
    <context:component-scan base-package="cn.tedu"></context:component-scan>

</beans>
测试
package cn.tedu.test;
import cn.tedu.ioc.User;
import cn.tedu.spring.Hello;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
    @Test
    public void get(){
        //1,读取配置文件
        ClassPathXmlApplicationContext spring =
                        new ClassPathXmlApplicationContext(
                            "spring-config.xml");
        //2,直接getBean
        Object o = spring.getBean("hello");
        System.out.println(o);//cn.tedu.spring.Hello@45752059

        //调用子类的方法--向下转型/造型
        Hello h = (Hello) o ;
        h.get();

        //获取注解的bean
        User u = (User) spring.getBean("a");
        System.out.println(u);//cn.tedu.ioc.User@28f3b248

     
    }
}

总结

在这里插入图片描述

4,Spring的DI 依赖注入

–1,概述

是指对象间的依赖关系,可以由框架来完成

–2,简单模拟

在这里插入图片描述

创建Dept类
package cn.tedu.di;

public class Dept {
    String name="java软件开发一部";
    @Override
    public String toString() {
        return "Dept{" +
                "name='" + name + '\'' +
                '}';
    }
}

创建Emp类
package cn.tedu.di;

public class Emp {
    String name="jack";
    //绑定两个类间的关系
    private Dept d ;
    public Dept getD() {
        return d;
    }
    public void setD(Dept d) {
        this.d = d;
    }
    @Override
    public String toString() {
        return "Emp{" +
                "name='" + name + '\'' +
                ", d=" + d +
                '}';
    }
}

测试
package cn.tedu.test;

import cn.tedu.di.Dept;
import cn.tedu.di.Emp;
import org.junit.jupiter.api.Test;
public class Test1 {
    @Test
    public void di(){
        Dept d = new Dept();
        System.out.println(d);//Dept{name='java软件开发一部'}
        Emp e = new Emp();
        System.out.println(e);//Emp{name='jack', d=null}

        //di--把两个对象间的关系依赖注入
        e.setD(d);
        System.out.println(e);
        //已经实现了di的效果,在查询e把关联的d的信息也查到了
        //Emp{name='jack', d=Dept{name='java软件开发一部'}}

    }
}

–3,使用Spring实现DI

在这里插入图片描述

创建Teacher类
package cn.tedu.di2;

import org.springframework.stereotype.Component;
@Component
public class Teacher {
    String name = "tony";
    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                '}';
    }
}

创建Student类
package cn.tedu.di2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Student {
    String name = "蔡徐坤";
    @Autowired //di
    Teacher t ;
    @Override
    public String toString() {
        return "Student{" +
                "t=" + t +
                ", name='" + name + '\'' +
                '}';
    }
}

创建配置文件,包扫描
<?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: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/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--包扫描:扫描指定包路径下的所有类,谁有ioc的注解就new谁  -->
    <context:component-scan base-package="cn.tedu.di2"></context:component-scan>
</beans>
测试
 @Test
    public void di2(){
        //1,读取xml文件
        ClassPathXmlApplicationContext spring = new
                ClassPathXmlApplicationContext(
                        "spring-config.xml");
        //2,getBean
        Object o = spring.getBean("student");
//di:查学生信息的同时也查到了老师信息,两个对象之间的依赖注入
        //Student{t=Teacher{name='tony'}, name='蔡徐坤'}
        System.out.println(o);
    }
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值