Spring学习笔记(一)
Spring概念
-
Spring是开源的轻量级框架
-
Spring的核心主要有两部分
- Aop:面向切面编程,扩展功能不需要修改源代码
- IoC:控制反转,比如有一个类,在类里面有一个方法(不是静态方法),我们如果要调用该类里的方法,就需要创建该类的对象,使用对象来调用方法,创建类对象的过程,是使用new的方式。 使用了Spring之后,创建类对象的过程,则需要交给Spring了。
- Spring是一站式框架
Spring在JavaEE的每一层都提供了不同的解决方案
- Web:SpringMVC
- Service:Spring IoC
- Dao:JdbcTemplate
IoC底层原理
IoC底层原理使用的技术
- xml
- dom4j
- 工厂设计模式
- 反射
使用new的方式
缺点:耦合度太高,Test类和Student类产生了耦合
public class Student{
public List<Student> getStudentList(){
......
}
}
class Test{
public void test1(){
Student student = new Student();
student.getStudentList();
}
}
使用工厂设计模式的方式
缺点:Test类和Factory类产生了新的耦合
public class Student{
public List<Student> getStudentList(){
......
}
}
class Test{
public void test1(){
<!--Student student = new Student();
student.getStudentList();-->
Student student = Factory.getStudentClass();
}
}
class Factory{
public static Student getStudentClass(){
return new Student();
}
}
IoC底层实现方式
优点:降低了类之间的耦合度 实现步骤:
- 创建xml配置文件并配置要创建的类对象(xml)
<bean id="Student" class="com.xianxian86.pojo.Student"/>
- 创建工厂类(工厂设计模式)
- 使用dom4j解析xml配置文件
- 使用反射创建对象
class Factory{
public static Object getClass(){
<!--1、使用dom4j解析xml配置文件
根据id值(Student)
获取对应的
class属性值(com.xianxian86.pojo.Student)-->
String classValue = "com.xianxian86.pojo.Student"
2、使用反射,根据获取到的class属性值,创建类对象
Class clazz = Class.forName(classValue);
//Student student = clazz.newInstance();
return clazz.newInstance();
}
}
Spring中Bean实例化的三种方式
使用类的无参构造
<?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">
<!--使用类的无参构造来创建对象-->
<bean id="student" class="com.xianxian86.pojo.Student"></bean>
</beans>
使用静态工厂
/**
* 静态工厂类
*/
public class Factory {
/*返回对象的方法*/
public static Student getStudent(){
return new Student();
}
}
<?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">
<!--使用静态工厂来创建对象-->
<!--factory-method 静态工厂类中的方法名-->
<!--class 静态工厂类的类路径-->
<bean id="student" class="com.xianxian86.test.Factory" factory-method="getStudent"></bean>
</beans>
使用实例工厂
/**
* 实例工厂类
*/
public class Factory {
/*返回对象的方法*/
public Student getStudent(){
return new Student();
}
}
<?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">
<!--使用实例工厂创建对象-->
<!--使用无参构造创建实例工厂-->
<!--class 实例工厂的类路径-->
<bean id="factory" class="com.xianxian86.test.Factory"></bean>
<!--factory-bean 实例工厂的id-->
<!--factory-method 实例工厂类中的方法名-->
<bean id="student" factory-bean="factory" factory-method="getStudent"></bean>
</beans>
注意:在实际开发当中通常使用第一种方式来创建对象,其他两种方式可以作为了解
id属性
可以看作为一个名称,id的属性值可以任意命名但不能包含特殊符号
name属性
和id属性的功能大致相同,只是name的属性值可以包含特殊符号
class属性
创建对象所在类的全路径
scope属性
scope的默认属性为singleton
<bean id="" class="" scope="singleton"/>
scope属性详解
在spring2.0之前bean只有2种作用域即:
baisingleton(单例)、non-singleton(也称duprototype)。
Spring2.0以后,增加了session、request、global session三种专用zhi于daoWeb应用程序上下文的Bean。因此,默认情况下Spring2.0现在有五种类型的Bean。
当一个bean的作用域设置为singleton,那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。
换言之,当把一个bean定义设置为singleton作用域时,Spring IOC容器只会创建该bean定义的唯一实例。
prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当于一个new的操作。
request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效。
global session为整个HTTP请求中,在作用域方面就是application
scope属性总结
singleton:单例
prototype:多例
request:把创建的对象放进request域中
session:把创建的对象放进session域中
globalSession:全局的HttpSession中,容器会返回该bean的同一个实例,典型为在是使用portlet context的时候有效。在作用域方面就是application