Spring学习笔记(一)

Spring学习笔记(一)

Spring概念

  • Spring是开源的轻量级框架

  • Spring的核心主要有两部分

  1. Aop:面向切面编程,扩展功能不需要修改源代码
  2. IoC:控制反转,比如有一个类,在类里面有一个方法(不是静态方法),我们如果要调用该类里的方法,就需要创建该类的对象,使用对象来调用方法,创建类对象的过程,是使用new的方式。 使用了Spring之后,创建类对象的过程,则需要交给Spring了。
  • Spring是一站式框架

Spring在JavaEE的每一层都提供了不同的解决方案

  1. Web:SpringMVC
  2. Service:Spring IoC
  3. Dao:JdbcTemplate

IoC底层原理

IoC底层原理使用的技术

  1. xml
  2. dom4j
  3. 工厂设计模式
  4. 反射

使用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底层实现方式

优点:降低了类之间的耦合度 实现步骤:

  1. 创建xml配置文件并配置要创建的类对象(xml)
<bean id="Student" class="com.xianxian86.pojo.Student"/>
  1. 创建工厂类(工厂设计模式)
  • 使用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
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring是一个开源的Java框架,用于构建企业级应用程序。它提供了一种轻量级的、非侵入式的开发方式,通过依赖注入和面向切面编程等特性,简化了Java应用程序的开发过程。 以下是关于Spring学习的一些笔记: 1. IoC(控制反转):Spring通过IoC容器管理对象的创建和依赖关系的注入。通过配置文件或注解,将对象的创建和依赖关系的维护交给Spring容器来管理,降低了组件之间的耦合度。 2. DI(依赖注入):Spring通过依赖注入将对象之间的依赖关系解耦。通过构造函数、Setter方法或注解,将依赖的对象注入到目标对象中,使得对象之间的关系更加灵活和可维护。 3. AOP(面向切面编程):Spring提供了AOP的支持,可以将与业务逻辑无关的横切关注点(如日志、事务管理等)从业务逻辑中分离出来,提高了代码的可重用性和可维护性。 4. MVC(模型-视图-控制器):Spring提供了一个MVC框架,用于构建Web应用程序。通过DispatcherServlet、Controller、ViewResolver等组件,实现了请求的分发和处理,将业务逻辑和视图展示进行了分离。 5. JDBC和ORM支持:Spring提供了对JDBC和ORM框架(如Hibernate、MyBatis)的集成支持,简化了数据库访问的操作,提高了开发效率。 6. 事务管理:Spring提供了对事务的支持,通过声明式事务管理和编程式事务管理,实现了对数据库事务的控制和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值