第一章:Spring5概念
1.Spring概述
1.Spring框架:是一个轻量级的开源javaEE框架
2.Spring开源解决企业中的应用开发的复杂性
3.Spring有两个核心部分 :IOC和AOP
--1.IOC:控制反转 把创建对象过程交给Spring进行管理
--2.AOP:面向切面 不修改源代码进行功能增强
4.Spring特点:
--1.方便解耦 简化开发
--2.Aop编程支持
--3.方便程序的测试
--4.方便和其他框架进行整合
--5.方便进行事物的操作
--6.降低api开发难度
5.现在的教程采用Spring5
2.入门案例
1.下载Spring5 解压完成
2.打开idea工具 创建普通java工程
3.导入Spring5相关的jar包
在lib文件夹里面找到 Beans Core Context Expression四个jar包
4.创建普通类
5,创建Spring配置文件 在配置文件创建对象
1.Spring配置文件是xml文件
6.测试代码的编写
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置User类对象创建-->
<bean id="user" class="SpringDemo.User"> </bean>
</beans>
java代码:
package SpringDemo.TestDemo;
import SpringDemo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5 {
public void testAdd(){
ApplicationContext context =new ClassPathXmlApplicationContext("bean1.xml");
User user= context.getBean("user",User.class);
System.out.println(user);
user.add();
}
}
package SpringDemo;
public class User {
public void add(){
System.out.println("add......");
}
}
3.IOC容器
控制反转(IOC):
控制反转:把对象创建和对象之间的调用过程 交给Spring进行管理
使用IOC目的 为了减低耦合度
2.IOC底层原理
---1.xml解析 工厂模式 反射
Ioc过程:
第一步:xml配置文件 配置创建的对象
<bean id="" class=""></bean>
第二步:有service和dao类 创建工厂类
class UsePactory{
public static UserDao getDao(){
String classValue=class属性值;
Class clazz=Class.forName(classValue);
return (UserDao)clazz.newintance();
}
}
进一步降低耦合度
IOC接口
1.IOC思想教育IOC容器完成 IOC容器底层就是对象工厂
2Spring提供IOC容器实现两种方式(两个接口)
BeanFactory :IOC容器基本实现 是Spring内部的使用接口 不提供开发人员使用·
加载配置文件的时候不会创建对象 在获取对象(使用)才会去创建对象
ApplicationContext :BeanFactory 接口的子接口 提供更多更强大的功能 一般由开发人员使用
加载配置文件时就会把在配置文件对象进行创建
4.IOC具体操作
1.什么是Bean管理:
----Bean指的是两个操作
--Spring创建对象
---Spring注入对象
2.Bean管理操作有两种方式:
1.基于xml配置文件方式实现
2.基于注解方式实现
基于XML配置文件
1.xml方式创建对象
在Spring的配置文件中 使用bean标签 标签里面添加对应的属性 就可以实现对象的创建
在Bean标签里面有很多常见的属性 介绍一下常用的属性:
id属性:唯一标识
class属性:要创建类的全路径
创建对象的时候 默认也是执行无参构造方法 完成对象的创建
2.基于xml方式注入属性
--1.DI:依赖注入 就是注入属性
方式一:set方法注入
--1.创建类 定义属性和对应的set方法
package Pro01;
public class Spring {
private String bname;
// 首先要创建属性和属性对应的方法
private String bauthor;
// 生成对应的set方法
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void setBname(String bname) {
this.bname = bname;
}
public static void main(String[] args) {
// 方式一 :直接通过set方法去注入
Spring spring = new Spring();
spring.setBname("abc");
System.out.println();
}
}
在Spring配置文件中配置对象的创建 配置属性注入
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Spring" class="Pro01.Spring"> </bean>
<bean id="book" class="Pro01.Spring">
<property name="bname" value="盗墓笔记"></property>
<property name="bauthor" value="南派三叔"></property>
</bean>
</beans>
方式二:使用有参数构造
方式三:通过
package Pro01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public void testAdd(){
ApplicationContext context =new ClassPathXmlApplicationContext("bean1.xml");
User user= context.getBean("user",User.class);
System.out.println(user);
user.add();
}
public void testBook(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
User user=context.getBean("book",Book.class);
System.out.println(book);
book.testDemo
}
}
5.管理xml文件
1.创建类,定义属性 创建属性对应有参数构造方法
package Pro02;
public class Orders {
private String oname;
private String address;
/*有参数的构造注入
*
* */
public Orders(String oname, String address) {
this.oname = oname;
this.address = address;
}
}
在spring的配置文件中进行配置:
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="orders" name="Pro02.Orders">
<constructor-arg name="oname" value="盗墓笔记"> </constructor-arg>
<constructor-arg name="address" value="中国"> </constructor-arg>
</bean>
</beans>
6.p名称空间注入
1.使用p名称空间注入 可以简化基于xml配置方式
1.添加一个p
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
第二步:进行属性注入:在bean标签里面
7.Bean管理xml-注入空值和特殊符号
1.字面量
--1.null值
<!-- 1.为null值-->
<property name="address"> </null> </property>
--2.属性值包含特殊符号
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="Orders" class="Pro02.Orders">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="2"/>
<property name="address" >
<value>
<![CDATA[<<南京>>]]>
</value>
</property>
</bean>
</beans>
8.Bean管理xml方式-注入外部Bean
注入属性 内部bean和级联赋值
---1.创建两个类 service类dao类
----2.在service调用dao里面的方法
9.Bean管理XML方式-注入内部bean和联级赋值
1.一对多关系 :部门和员工
--在实体类之间表示一对多关系
//1.部门类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
package bean;
//2.员工类
public class employee {
private String ename;
private String gender;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
package bean;
//2.员工类
public class employee {
private String ename;
private String gender;
public void setDept(Dept dept) {
this.dept = dept;
}
// y员工属于某个部门 使用对象形式表示
private Dept dept;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
第三部分:在spring配置文件中进行配置
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="bean.employee">
<property name="ename" value="吴邪"></property>
<property name="gender" value="男"></property>
<property name="dept" >
<bean id="dept" class="bean.Dept">
<property name="dname" value="前端"></property>
</bean>
</property>
</bean>
</beans>
package bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class MainTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
Emp emp=context.getBean("emp",Emp.class);
emp.add();
}
}
10.Bean管理xml方式-注入集合类型属性
1.注入数组类型的属性
package BeanSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
private String [] couress;
public void setCouress(String[] couress) {
this.couress = couress;
}
private List<String > list;
private Map<String,String> maps;
private Set<String> set;
public void setSet(Set<String> set) {
this.set = set;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
}
书写配置文件:
创建类 定义数组 list map set类型属性 生成对应set方法
在spring的配置文件中注入:
2.注入List集合类型属性
package BeanSet;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
private String [] couress;
public void setCouress(String[] couress) {
this.couress = couress;
}
private List<String > list;
private Map<String,String> maps;
private Set<String> set;
public void setSet(Set<String> set) {
this.set = set;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void test(){
System.out.println(Arrays.toString(couress));
System.out.println(list);
System.out.println(maps);
System.out.println(set);
}
}
package BeanSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5 {
public void testCollection(){
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
Stu stu = context.getBean("stu", Stu.class);
stu.test();
}
public static void main(String[] args) {
TestSpring5 t=new TestSpring5();
t.testCollection();
}
}
3.注入Map集合
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="stu" class="BeanSet.Stu">
<!-- 1.数组类型属性注入-->
<property name="couress" >
<array>
<value>java</value>
<value>数据库</value>
</array>
</property>
<!-- 2.完成list类型属性输入-->
<property name="list">
<list>
<value>吴邪</value>
<value>小三</value>
</list>
</property>
<!-- 3.map类型注入-->
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!-- 4.set集合注入-->
<property name="set">
<set>
<value >MySQl</value>
<value>ORACLE</value>
</set>
</property>
</bean>
</beans>
第十四节课