Spring复习

第一节Spring :IOC和AOP的容器框架
IOC(控制反转) : 资源的管理权转交给了Spring容器
DI: 依赖注入
AOP:面向切面编程 解决系统级别业务逻辑的解耦 如日志和事件等
第一个spring程序:
1.拷贝jar包到lib文件夹中
spring所需jar包
 
2.在src文件夹下,配置spring核心配置文件 applicationContext.xml 【内容如下:】
 
 
    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">
 
 
3.创建pojo包并新建pojo student类 (属性、方法在这省略)
 
4.注册bean节点,使资源受spring容器管理
 
 
    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="student" class="com.ambow.pojo.Student">
 
 
5.编写测试类
步骤: 1刷新上下文环境 2.读取配置文件 3.创建对象
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
 
Student student = context.getBean(“student”);
student.setSid(1001);
student.setSname("张翠山");
System.out.println(student.toString());
 
改进:
1.在第四步骤的基础上 修改applicationContext.xml 实现属性注入
0
 
 
 
 
===》
0
 
 
 
 
 
 
 
 
2.编写测试代码:
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
 
Student student = context.getBean("student");
System.out.println(student.toString());
 
 
 
 
第二节Spring配置文件的bean属性
name 属性中可以为 Bean 指定多个名称,每个名称之间用逗号或分号隔开 例如:
student,stu" class="com.ambow.pojo.Student">
0
 
注意区分 singleton和prototye
scope="singleton" name="student,stu" class="com.ambow.pojo.Student">
scope=“singleton”,容器加载完毕后,就创建好了该对象,默认的。该节点产生的对象在整个Spring容器里面只有一个;
 
 
 
scope=“prototype”,当你获取资源(getBean)的时候,才给你创建。每次获取对象(getBean)的时候,都会产生新对象。
0
 
 
基于xml的依赖注入:
1.对pojo中的属性值进行注入 (必须提供 set方法和无参的构造函数 )
 
 
 
2.构造函数注入
 
<constructor-arg name="sid" value="1002">
<constructor-arg name="sname" value="张三丰">
 
3.p名字空间注入 p是属性的简写
i.核心配置文件的beans节点引入p名字空间
ii.在bean节点上,使用c:属性名=”值”的方式进行属性注入,代码如下:
 
 
4.c名字空间 c是构造函数
i.核心配置文件的beans节点引入c名字空间
xmlns:p=“http://www.springframework.org/schema/c”
ii.在bean节点上,使用c:属性名=”值”的方式进行属性注入,代码如下:
 
 
5.引入其他bean组件 (一个对象引用另一个对象)
 
 
 
posted @ 2021-01-06 16:05  Havk_lzh  阅读(72)  评论(0编辑  收藏  举报