Spring笔记(一)
一、Ioc容器(概念)
- 什么是IoC?
- 控制反转,把对象创建和对象之间得调用过程,交给了Spring来管理
- 使用IoC得目的:为了降低耦合度
- IoC底层使用了哪些技术?
- XML解析
- 工厂模式
- 反射
- Spring提供的IoC容器实现的两种方式(两个接口):
- BeanFactory接口:IoC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用(加载配置文件的时候不会创建对象,只有在获取对象的时候才回去创建对象,懒加载)
- ApplicationContext接口:BeanFactory接口的子接口,提供了更多更强大的功能,一般由开发人员进行使用(加载配置文件的时候就会把在配置文件对象进行创建)
- ApplicationContext接口的实现类
- FileSystemXmlApplicationContext:加载xml配置文件的时候使用的是绝对路径
- ClassPathXmlApplicationContext:加载xml配置文件的时候使用的是相对路径,src目录下为根目录
二、 IoC容器 – Bean管理(基于XML方式)
-
Bean管理由两个操作:
- Spring创建对象
- Spring注入属性
-
基于XML方式创建对象
-
<bean id="该Bean对象的id名称,唯一标识" class="Bean对象的全类名"></bean>
创建对象的时候,默认执行无参数构造的方法完成对象的创建
-
-
基于XML方式注入属性(DI:依赖注入(DI是IoC中的一种具体实现))
-
使用set方法进行注入
public class Book { //创建属性 private String bname; //创建属性对应的set方法 public void setBname(String bname) { this.bname = bname; } }
<bean id="book" class="com.atguigu.spring5.Book"> <!--使用property完成属性注入 name:类里面属性名称 value:向属性注入的值 --> <property name="bname" value="Hello"></property> <property name="bauthor" value="World"></property> </bean>
-
有参构造函数注入
public class Orders { //属性 private String oname; private String address; //有参数构造 public Orders(String oname,String address) { this.oname = oname; this.address = address; } }
<bean id="orders" class="com.atguigu.spring5.Orders"> <constructor-arg name="oname" value="Hello"></constructor-arg> <constructor-arg name="address" value="China!"></constructor-arg> </bean>
-
p名称空间注入(了解即可)
<!--1、添加p名称空间在配置文件头部--> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="//www.springframework.org/schema/beans" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:p="//www.springframework.org/schema/p" <!--在这里添加一行p--> <!--2、在bean标签进行属性注入(算是set方式注入的简化操作)--> <bean id="book" class="com.atguigu.spring5.Book" p:bname="hello" p:bauthor="world"></bean> </beans>
-
XML注入其他类型的属性(字面量:null值、包含的特殊符号)
-
注入null值
<property name="address"> <null/> </property>
-
注入特殊符号
<!-- 有两种方法: 将符号转义:<替换<, 使用CDATA --> <property name = "address"> <value><![CDATA[<<南京>>]]></value> </property>
-
-
注入属性:外部Bean
- 创建两个类Service类和dao类
- 在service调用dao里面的方法
- 在Spring配置文件中进行配置
//service类 public class UserService { //创建UserDao类型属性,生成set方法 private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { System.out.println("service add..............."); //调用dao方法 userDao.update(); } } //dao类 public class UserDaoImpl implements UserDao { @Override public void update() { System.out.println("dao update..........."); } }
- 在Spring配置文件中进行配置
<bean id="userService" class="com.atguigu.spring5.UserService"> <!-- 注入dao对象、 name属性:类里面的id名称 ref属性:创建userDao对象bean标签id值 --> <property name="userDao" ref="userDaoImpl"></property> </bean> <bean id="userDaoImpl" class="com.atguigu.spring5.UserDaoImpl"></bean>
-
注入属性:内部Bean
- 一对多关系:部门和员工,一个部门有多个员工,一个员工属于一个部门
- 在实体类之间表示一对多关系,员工表示所属部门,在使用对象类型属性进行表示
//部门类 public class Dept { private String dname; public void setDname(String dname) { this.dname = dname; } } //员工类 public class Emp { private String ename; private String gender; //员工属于某一个部门,使用对象形式表示 private Dept dept; public void setDept(Dept dept) { this.dept = dept; } public void setEname(String ename) { this.ename = ename; } public void setGender(String gender) { this.gender = gender; } }
- 在Spring配置文件中配置
<bean id="emp" class="com.atguigu.spring5.bean.Emp"> <!-- 设置普通属性的值 --> <property name="ename" value="Andy"></property> <property name="gender" value="女"></property> <!-- 设置对象类型的属性 --> <property name="dept"> <bean id="dept" name="com.atguigu.spring5.bean.Dept"> <property name="dname" value="安保部"></property> </bean> </property> </bean>
-
注入属性:级联赋值
-
方法一:级联赋值
<!--方式一:级联赋值--> <bean id="emp" class="com.atguigu.spring5.bean.Emp"> <!--设置两个普通属性--> <property name="ename" value="Andy"></property> <property name="gender" value="女"></property> <!--级联赋值--> <property name="dept" ref="dept"></property> </bean> <bean id="dept" class="com.atguigu.spring5.bean.Dept"> <property name="dname" value="公关部门"></property> </bean>
- 方法二:生成dept的get方法(必须要有get方法)
<!-- 级联赋值 --> <bean id="emp" class="com.atguigu.spring5.bean.Emp"> <!--设置两个普通属性--> <property name="ename" value="jams"></property> <property name="gender" value="男"></property> <!--级联赋值--> <property name="dept" ref="dept"></property> <property name="dept.dname" value="技术部门"></property> </bean> <bean id="dept" class="com.atguigu.spring5.bean.Dept"></bean>
-
-
XML注入集合属性
- 注入数组类型属性
- 注入List集合类型属性
- 注入Map集合类型属性
- 注入Set集合类型属性
//(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法 public class Stu { //1 数组类型属性 private String[] courses; //2 list集合类型属性 private List<String> list; //3 map集合类型属性 private Map<String,String> maps; //4 set集合类型属性 private Set<String> sets; public void setSets(Set<String> sets) { this.sets = sets; } public void setCourses(String[] courses) { this.courses = courses; } public void setList(List<String> list) { this.list = list; } public void setMaps(Map<String, String> maps) { this.maps = maps; } }
<!--(2)在 spring 配置文件进行配置 --> <bean id="stu" class="com.atguigu.spring5.collectiontype.Stu"> <!--数组类型属性注入--> <property name="courses"> <array> <value>java课程</value> <value>数据库课程</value> </array> </property> <!--list类型属性注入--> <property name="list"> <list> <value>张三</value> <value>小三</value> </list> </property> <!--map类型属性注入--> <property name="maps"> <map> <entry key="JAVA" value="java"></entry> <entry key="PHP" value="php"></entry> </map> </property> <!--set类型属性注入--> <property name="sets"> <set> <value>MySQL</value> <value>Redis</value> </set> </property> </bean>
-
在集合里面设置对象类型的值
- 如果集合里的元素类型是对象,则不能用普通的方法来注入
// 创建课程类,学生所学多门课程 public class Course { private String cname; public void setCname(String cname) { this.cname = cname; } @Override public String toString() { return "Course{" + "cname='" + cname + '\'' + '}'; } }
- 然后编辑XML配置文件
<!-- 先创建多个course对象 --> <bean id="course1" class="com.atguigu.spring5.collectiontype.Course"> <property name="cname" value="Spring5框架"></property> </bean> <bean id="course2" class="com.atguigu.spring5.collectiontype.Course"> <property name="cname" value="MyBatis框架"></property> </bean> <!-- 然后在property中注入 --> <bean> <property name="courseList"> <list> <ref bean="course1"></ref> <ref bean="course2"></ref> </list> </property> </bean>
-
将集合注入的部分提取出来
- 先在spring配置文件中映入名称空间util
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="//www.springframework.org/schema/beans" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:util="//www.springframework.org/schema/util" xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd //www.springframework.org/schema/util //www.springframework.org/schema/util/spring-util.xsd"> </beans>
- 使用util标签完成List集合注入提取
<util:list id="bookList"> <value>易筋经</value> <value>九阴真经</value> <value>葵花宝典</value> </util:list>
- 将提取出来的集合类型属性注入使用
<bean id="book" class="com.atguigu.spring5.collectiontype.Book"> <property name="list" ref="bookList"></property> </bean>
-