×

spring

Spring概述环境搭建及其加载过程

我的笔记 我的笔记 发表于2019-07-20 09:34:26 浏览3112 评论0

抢沙发发表评论

    相信小伙伴们都已经多多少少用过几年Spring框架了,也熟悉了框架相关一些使用方法,接下来这段时间我带着小伙伴们来深度解析一下Spring源码

一、Spring概述

image.png

image.png

image.png

    Spring框架,可以解决对象创建以及对象之间依赖关系的一种框架。

                                且可以和其他框架一起使用;SpringStruts,  Springhibernate

                                (起到整合(粘合)作用的一个框架)

    Spring提供了一站式解决方案:

             1 Spring Core  spring的核心功能: IOC容器, 解决对象创建及依赖关系

             2 Spring Web  Springweb模块的支持。

                                                            -à 可以与struts整合,strutsaction创建交给spring

                                                       -à spring mvc模式

             3 Spring DAO  Spring jdbc操作的支持  JdbcTemplate模板工具类】

             4 Spring ORM  springorm的支持:

                                                            à 既可以与hibernate整合,【session

                                                            à 也可以使用spring的对hibernate操作的封装

             5Spring AOP  切面编程

             6SpringEE   spring javaEE其他模块的支持    


二、Spring环境搭建,这点我就不累赘了,如果你们公司用的是maven多模块,相信大家每天都会做这种工作

pom.xml所需内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.fyd</groupId>
      <artifactId>fyd-spring</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <dependencies>
           <!-- 引入Spring-AOP等相关Jar -->
           <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-core</artifactId>
                 <version>3.0.6.RELEASE</version>
           </dependency>
           <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-context</artifactId>
                 <version>3.0.6.RELEASE</version>
           </dependency>
           <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-aop</artifactId>
                 <version>3.0.6.RELEASE</version>
           </dependency>
           <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-orm</artifactId>
                 <version>3.0.6.RELEASE</version>
           </dependency>
           <dependency>
                 <groupId>org.aspectj</groupId>
                 <artifactId>aspectjrt</artifactId>
                 <version>1.6.1</version>
           </dependency>
           <dependency>
                 <groupId>aspectj</groupId>
                 <artifactId>aspectjweaver</artifactId>
                 <version>1.5.3</version>
           </dependency>
<dependency>
                 <groupId>cglib</groupId>
                 <artifactId>cglib</artifactId>
                 <version>2.1_2</version>
           </dependency>
      </dependencies>
 
</project>

需要交给Spring管理注入类

public class UserEntity {
     private String name;
     private Integer age;
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public Integer getAge() {
         return age;
     }
     public void setAge(Integer age) {
         this.age = age;
     }
}

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" xmlns:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd"> 
<bean id="userEntity" class="com.fyd.entity.UserEntity" />
</beans>

测试类

public class SpringTest {
 
     public static void main(String[] args) {
         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                   "applicationContext.xml");
         UserEntity userEntity = (UserEntity) applicationContext.getBean("userEntity");
         System.out.println(userEntity);
     }
 
}

这时你运行测试类,你就能打印出来,Spring已经把userEntity给我们放到容器中了

那么接下来我来提问几个问题:

spring bean id重复会怎么办?

spring是单例还是多例?

spring作用域

当一个bean的 作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把 一个bean定义设置为singleton作用域时,Spring IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都 将返回被缓存的对象实例,这里要注意的是singleton作用域和GOF设计模式中的单例是完全不同的,单例设计模式表示一个ClassLoader中 只有一个class存在,而这里的singleton则表示一个容器对应一个bean,也就是说当一个bean被标识为singleton时 候,spring的IOC容器中只会存在一个该bean。

Prototype

prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的 getBean()方法)都会产生一个新的bean实例,相当与一个new的操作,对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。 清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被singleton作用域bean占用资源的一种可行方式是,通过使用 bean的后置处理器,该处理器持有要被清除的bean的引用。)

request

request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例:

request、session、global session使用的时候首先要在初始化web的web.xml中做如下配置:

session

session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效


我的笔记博客版权我的笔记博客版权