Spring5 学习笔记

视频出处
练习源码
讲的比较有逻辑性 有一些底层的东西讲的不是很清晰 需要看第二遍才能看懂
总体来说还是比较不错的

随看随记

  • 可以直接生成Student类型的对象而不是Object类型
 Student student2 = context.getBean("student2", Student.class);

Maven 安装与使用

Spring

Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架
Spring 7大模块
在这里插入图片描述

  • IoC理论(Inversion of Control) (控制反转) 通过set() 等接口 将客户的需求传入框架中 而不需要通过程序员根据每一个需求去定制一个程序 降低耦合性
  • 控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。

HelloSpring Spring_01 和 Spring_02

  • Spring用起来感觉和Vue框架有一些相似之处 与不同之处
    1. Spring是使用xml来配置文件,Vue是使用index.js进行路由的配置
    2. 两者都是在最外层使用Application进行整体调用 通过对ApplicationContext.xml文件和index.js对源码文件进行管理和配置
      • Spring是使用bean标签进行文件配置 使用property进行属性配置
      • Vue是使用路由中的path进行页面配置 使用 component 进行当前模板配置和 children进行子路由配置
		<!--   将我们需要的类放置到bean 容器中 -->
   		<bean id="hello" class="com.kuang.pojo.Hello">
   		<!--   对类中的成员变量进行 进行初始化-->
   		
   			<!--   如果赋值的量是一个常量 则使用value-->
        	<property name="string" value="HelloSpring" />
        	
        	<!--   如果赋值的量是一个在前面用bean配置或的对象 则使用ref-->
        	<property name="userDao" ref="userMysql" />
    	</bean>
		 //获取配置及文件
		ApplicationContext context = new ClassPathXmlApplicationContext
                ("beans.xml");
        //获得哦欸至文件的指定类 的指定对象
        UserService userService = (UserService) context.getBean("userService");
        // 调用对象的方法
        userService.getUser();

IoC 创建对象的方式

当在beans.xml中被配置以后 Spring在开始程序运行的时候就会新建对象
即Spring创建对象的时机是在配置文件中

  • 无参构造 因为无参构造 因此不需要添加参数
  • 有参构造的三种方式
    1. 索引式
    2. 类型式
    3. 参数名式
	 <bean id="world" class="com.kuang.pojo.World">
        <constructor-arg name="string" value="hello"/>
    </bean>

    <bean id="world2" class="com.kuang.pojo.World">
        <constructor-arg index="0" value="hell2" />
    </bean>

    <bean id="world3" class="com.kuang.pojo.World">
        <constructor-arg type="java.lang.String" value="hello3"/>
World world = (World) context.getBean("world");
World world1 = (World) context.getBean("world");

World world2 = (World) context.getBean("world2");
World world3 = (World) context.getBean("world3");

System.out.println(world.hashCode());
System.out.println(world1.hashCode());

System.out.println(world2.hashCode());
System.out.println(world3.hashCode());

其输出结果如下: 表明Spring在beans.xml中已经创立好对象 world 和world1 是通过world创建的 所以有相同的哈希值 但是world和world2和world3 在xml中是三个对象 所以哈希值不同。 单例模式

1864350231
1864350231
25548982
1735934726

Spring的配置

配置文件的常用标签有三个

  • 别名
 <alias name="userDao" alias="userdao" />
  • bean 其中个也有name属性值 其意义也是别名
	<bean id="userDao" class="com.kuang.dao.Userdao"  name="userdao"/>
    <bean id="userMysql" class="com.kuang.dao.UserMysql" />
    <bean id="userOracle" class="com.kuang.dao.UserOracle" />
    
  • import 在ApplicationContext的总xml文件中引用所有的xml文件 使得在最后main文件中只需要引用
    ApplicationContext.xml即可引用所有的配置文件
<import resource="beans.xml"/>
<import resource="beans1.xml"/>

DI依赖注入 Spring_03

  • 构造方法注入 上面以及记过了
  • set方法注入 按照注入的类型不同进行分类
<bean id="address" class="com.paleatta.pojo.Address">
    <property name=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值