spring笔记(一)——IOC
1、spring介绍
1.1、啥叫spring?
官方来说:
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。自我理解:
方便帮助咱们写代码的框架,还能帮助我们降低耦合度。
Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EE full-stack 轻量级开源框架。
2、spring ioc介绍
1.IOC的概念:
IOC(Inversion of Control,控制反转),所有的Java类(不管是JDK库中Java类,还是你自己定义Java类)都可以利用Spring框架来new它们的实例化对象
Spring框架完成对Java类的初始化对象工作
Spring来负责控制对象的生命周期和对象间的关系
2.IOC中的DI:
在创建对象的过程中Spring可以依据配置对象的属性进行设置,这个过程称之为依赖注入,也即DI。
3、spring ioc案例
如果实现一个spring的简单案例怎么做呢?
- 导入spring有关的jar包
我这里使用的是idea,所以创建spring项目的时候会自动下载jar。
2. 我们要写一个实体类
package com.domain;
public class Teacher {
private Integer id;
private String userName;
public Teacher() {
}
public Teacher(Integer id, String userName) {
this.id = id;
this.userName = userName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", userName='" + userName + '\'' +
'}';
}
}
- 我们创建一个applicationContext.xml文件在src目录下。
<?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:aop="http://www.springframework.org/schema/aop"
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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="TeacherBean" class="com.domain.Teacher">
<property name="id" value="1"></property>
<property name="userName" value="tom"/>
</bean>
</beans>
- 我们可以看到 <beans>标签中有很多的标签,如果我要想在<beans>中写其他的标签怎么办呢?
xmlns=“http://www.springframework.org/schema/XXX”
xsi:schemaLocation="http://www.springframework.org/schema/XXX
http://www.springframework.org/schema/XXX/spring-XXX.xsd
我们可以把XXX的地方改成所需要的东西,比如aop,context;是要添加哦。我添加了一个aop和context的标签,代码如下。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- 我们可以在一个<bean>标签中加入id和class属性值
id是起的名称(全局唯一),class是这个要bean化的全限定类名。
只要bean化了,我们才可以使用这个类,才相当于把这个类加入了spring的ioc中。
<bean id="TeacherBean" class="com.domain.Teacher">
<property name="id" value="1"></property>
<property name="userName" value="tom"/>
</bean>
- 我们接下来就是依赖注入,我们这里用了一个就是set注入。
set注入的意思是:我们给属性提供setXXX() 方法,我们在xml文件中就可以直接注入属性了。因为一般我们会把属性都private,给外界露出getXX()和setXXX()方法。
name是属性名称,value是属性值。
当然我们还有别的注入方式:构造器注入和注解注入。
<!-- 构造器注入 -->
<bean id="TeacherBean" class="com.domain.Teacher">
<constructor-arg name="id" value="2"/>
<constructor-arg index="1" value="jary"/>
</bean>
我们看到有构造器注入,里面有两个属性一个是value和name,这个我们在上上面已经说过了,但是index 是什么意思,这个就是和我们的构造器有关了,我们构造器中出现了有参构造函数,里面参数是有位置的顺序的,而index就是位置的顺序,从0开始,0代表id,1就代表userName了。
- 我么进行测试
public class AppTest1 {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//使用spring进行加载配置文件,测试spring
@Test
public void test1(){
Teacher teacherBean = (Teacher) context.getBean("TeacherBean");
System.out.println(teacherBean.toString());
}
}
- 我们可以想一想,这是基本数据类型的DI,如果是引用数据类型呢?
答案是当然可以的哦
- 我首先增了一个类 student
package com.domain;
public class Student {
private Integer id;
private String studentName;
public Student() {
}
public Student(Integer id, String studentName) {
this.id = id;
this.studentName = studentName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", studentName='" + studentName + '\'' +
'}';
}
}
- 修改原始的teacher类
package com.domain;
import java.util.List;
import java.util.Map;
public class Teacher {
private Integer id;
private String userName;
private Student student;
List<Integer> nums;
Map<String, Integer> maps;
public Teacher() {
}
public Teacher(Integer id, String userName) {
this.id = id;
this.userName = userName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setNums(List<Integer> nums) {
this.nums = nums;
}
public void setMaps(Map<String, Integer> maps) {
this.maps = maps;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", userName='" + userName + '\'' +
", student=" + student +
", nums=" + nums +
", maps=" + maps +
'}';
}
}
- 修改xml文件
<bean id="StudentBean" class="com.domain.Student">
<property name="id" value="1"/>
<property name="studentName" value="学生1"/>
</bean>
<bean id="TeacherBean" class="com.domain.Teacher">
<property name="id" value="1"></property>
<property name="userName" value="tom"/>
<property name="student" ref="StudentBean"/>
<property name="maps">
<map>
<entry key="小猫" value="1"/>
<entry key="小狗" value="2"/>
</map>
</property>
<property name="nums">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
</bean>
这里xml文件的name值一定是和属性值一致的。