spring小结1
核心功能:
1.IOC(控制反转):将对象的管理权限交给spring容器,管理生命周期
2.DI(依赖注入):管理IOC容器中对象的依赖关系
3.AOP(面向切面 ):对面向对象补充, 解决系统级别的非核心功能耦合度问题
- Spring是一个开源的免费的框架(容器)
- Spring是一个轻量级的、非入侵式的框架
- 控制反转(IOC)、面向切面的编程(AOP)
- 支持事务的处理,对框架整合的支持
Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架
配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd"
default-lazy-init="true">
<!-- 注册对象到IOC容器中,受spring管理
默认情况下,注册的对象为单例
class:对象的全限定类名
id:当前对象的名称 不能重复 外界通过id获取到该对象
scope:设置对象的作用范围 设置对象的模式
1.singleton:单例模式 默认
2.prototype:原型模式 每次通过getBean获取时,都会实例化一个新的对象
web模式:
3.request:对象的作用范围为一次请求
4.session:对象的作用范围为一次会话
init-method:指定对象的初始化方法,值就是对象中的某个方法名
该方法不用手动调用,会在创建完成后,自动调用
destroy-method:销毁的方法,对象被销毁之前自动执行的方法
lazy-init:延时(懒)加载
true:获取对象时才会创建对象
false:加载IOC容器时,就会创建对象
默认为FALSE
-->
<bean id = "student"
class="com.kang.Student"
scope="singleton"
init-method="init"
destroy-method="destroy"
lazy-init="true"></bean>
<!-- 依赖注入:在注册对象时,将对象所依赖的对象 注入到当前对象中,
为注册对象的成员变量赋值
1.属性注入(setter注入)
2.构造器注入
-->
<bean id = "stu" class = "com.kang.Student">
<!-- name:属性名 和setter方法匹配的属性名
value:要为该属性注入的值
-->
<property name="id" value="10"></property>
<property name="name" value="张三"></property>
<property name="age" value="10"></property>
<!-- 数组注入 -->
<property name="arr" >
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<!-- map注入 -->
<property name="map">
<map>
<entry key ="name" value="张三" ></entry>
<entry key ="age" value="15" ></entry>
</map>
</property>
<!-- 复杂对象的注入!! -->
<property name="student">
<bean class="com.kang.Student">
<!-- 如果依赖的对象已经在容器中
ref:容器中已经注册对象的id值
-->
<property name="student" ref="student"></property>
</bean>
</property>
</bean>
</beans>
对象
package com.kang;
import java.util.Arrays;
import java.util.Map;
public class Student {
private Integer id;
private String name;
private Integer age;
private int[] arr;
private Map<String,String> map;
private Student student;
public void init() {
System.out.println("初始化的方法");
}
public void destroy() {
System.out.println("销毁的方法");
}
public Student() {
super();
System.out.println("无参数的构造方法");
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public int[] getArr() {
return arr;
}
public void setArr(int[] arr) {
this.arr = arr;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", arr=" + Arrays.toString(arr) + ", map="
+ map + ", student=" + student + "]";
}
public Map<String,String> getMap() {
return map;
}
public void setMap(Map<String,String> map) {
this.map = map;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
}
Main方法
package com.kang;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1.获取spring上下文对象,获取到容器了
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring.xml");
//2.获取容器中的对象
//参数为注册时候的id或者name属性的值
Object stu = ctx.getBean("student");
System.out.println(stu);
//获取对象时,指定对象类型
Student stu2 = ctx.getBean("student",Student.class);
System.out.println(stu2);
System.out.println(stu==stu2);
/*
* 关闭容器时,会通知容器中所有对象,让对象去调用销毁的方法
* 做扫尾的工作,例如释放资源,保存数据等等
*/
AbstractApplicationContext absCtx =
(AbstractApplicationContext)ctx;
absCtx.close();
System.out.println("关闭容器");
/*
* spring容器中对象的 生命周期:
* 1.创建对象 此时会调用构造方法 默认调用无参数的构造方法
* 2.执行初始化方法 init-method
* 3.在对象被销毁时,调用销毁的方法 destroy-method
*/
}
}
package com.kang;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main1 {
public static void main(String[] args) {
//1.获取spring上下文对象,获取到容器了
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring.xml");
Student student = ctx.getBean("stu",Student.class);
System.out.println(student);
}
}