zoukankan      html  css  js  c++  java
  • Spring学习(4)---Bean基础

    • Bean配置项
    • Bean的作用域
    • Bean的生命周期
    • Bean的自动装配
    • Resources & ResourceLoader

    (一) Bean配置项

    常用的配置项

    1. Id   (IOC容器中Bean的唯一标识)                                      
    2. Class    (具体要实例化的类) (必须要有的)
    3. Scope    (Bean的作用域) 
    4. Constructor arguments  (构造器的参数) 
    5. Properties  (属性) 
    6. Autowiring mode  (自动装配模式) 
    7. lazy-initialzation mode  (懒加载模式) 
    8. Initialzation/destrustion method  (初始化/销毁方法) 

    (二) Bean的作用域

    1. singleton:单例,指一个Bean容器中只存在一份
    2. prototype:每次请求(每次使用)创建新的实例,destroy方式不生效
    3. request:每次http请求创建一个实例且仅在当前request内生效
    4. session:同上,每次请求创建,当前session内有效
    5. global session :基于portlet的web中有效(portlet定义了global session),如果是在web中,同session

     (三) Bean的生命周期

    • 定义          -- Spring bean 配置文件中配置的bean(id,及对应的class)
    • 初始化       
    • 使用          
    • 销毁          

    初始化:(两种方式)

    - 实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法

    public class ExampleBean implements InitializingBean{
    
        @Override
        public void afterPropertiesSet() throws Exception {
            // TODO Auto-generated method stub
            
        }
    
    }

    - 配置init-method

    <bean id="exampleInitBean" class="com.ioc.ExampleBean" init-method="init"></bean>
    
    ----------------------------------------------------------------------------------------- public class ExampleBean { public void init(){ //your work code } }

    销毁:(两种方式)

    - 实现org.springframework.beans.factory.DisposableBean接口,覆盖destroy方法

    public class ExampleBean implements DisposableBean{
    
        @Override
        public void destroy() throws Exception {
            // TODO Auto-generated method stub
        }
        
    }

    - 配置destroy-method

    <bean id="exampleDestoryBean" class="com.ioc.ExampleBean" destroy-method="destory"></bean>
    ---------------------------------------------------------------------------------------------
    public class ExampleBean{
        public void destory(){
            //your work code
        }    
    }

    配置全局默认初始化、销毁方法

    <?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-4.1.xsd"
               default-init-method="init"      default-destroy-method="destroy">  
    
    </beans>

    (四) Bean的自动装配(Autowiring)

    • No :不做任何操作
    • byname : 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配
    • byType : 如果容器存在一个与指定属性类型相同的bean,那么将与该属性自动装配;如果存在多个该类型的bean,那么抛出异常,并指出不能使用byType方式进行自动装配;如果没有找到相匹配的bean,则什么事都不发生
    • Construtor : 与byType方式相似,不同之处在于它应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么会抛出异常

     (五) Resources & ResourceLoader

    • 针对资源文件的统一接口
    • Resources

    - UrlResource : URL对应的资源,根据一个URL地址即可构建

    - ClassPathResource :获取类路径下的资源文件

    - FileSystemResource :获取文件系统里面的资源

    - ServletContextResource :ServletContext封装的资源,用于访问ServletContext环境下的资源

    - InputStreamResource :针对于输入流封装的资源

    - ByteArrayResource :针对于字节数组封装的资源

    •  ResourceLoader

    所有的ApplicationContext都实现了ResourceLoader接口,实现对Resources加载

    PS:

    Resource接口

    Spring将所有形式的资源表现概括成一个Resource接口(简化),Resource接口向应用程序屏蔽了资源表现形式的多样性

    public interface Resource {
    
        InputStream getInputStream();
    
        URL getURL();
    
        File getFile();
    
        boolean exists();
    
    }

    ResourceLoader接口

    public interface ResourceLoader {
    
        Resource getResource(String location);
    
    }
  • 相关阅读:
    在SpringBoot中使用JWT
    异常解决:non-compatible bean definition of same name and class【com.xxx.xxx.XXX】
    mysql 字段名和关键字冲突
    约束(主键、非空、唯一性、外键、检查约束)
    唯一索引与非唯一索引区别(UNIQUE INDEX, NON-UNIQUE INDEX)
    数据库设计mysql字段不默认为NULL原因搜集
    mysql索引总结----mysql 索引类型以及创建
    互联网数据库架构设计思路
    干货满满!10分钟看懂Docker和K8S
    PowerDesigner 使用教程(很具体,很实用)
  • 原文地址:https://www.cnblogs.com/JsonShare/p/4602404.html
Copyright © 2011-2022 走看看