• [spring]spring注入属性值的两种方式以及作用域


    4.IOC创建对象的过程

    • 使用无参构造创造

    image-20220723220940611

    image-20220723221011139

    设定为有参后,就会报错!

    对象在被注册进去的时候,就被实例化了,直接使用就好。

    5.IO注入

    (1)前面的构造器注入

    (2)set注入

    
    <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="user" class="cn.itnanls.User">
           
           
           
    
           
           
           
    
           
           
           
    
           
           
    
       bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    构造注入对象之间的关系为组合

    set注入的对象之间的关系为聚合

    (3)p命名空间注入

    • 使用set方式注入
    <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 id="helloSpring" class="com.pojo.HelloSpring">
    <property name="name"  value="spring">property>
    bean>
    
         <bean id="p-name" class="com.pojo.HelloSpring" p:name="ss">
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (4)c命名空间注入

    • 使用构造器方式注入,开启构造器才能用
    HelloSpring(String name){
        this.name=name;
    }
    HelloSpring(){
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    <bean id="c-name" class="com.pojo.HelloSpring" c:name="cName"/>
    
    • 1

    注意导入头文件

    xmlns:p = "http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    
    • 1
    • 2

    6.作用域

    ScopeDescription

    singleton

    (Default) Scopes a single bean definition to a single object instance for each Spring IoC container.

    prototype

    Scopes a single bean definition to any number of object instances.

    request

    Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

    session

    Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

    application

    Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

    websocket

    Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

    • 单例
    <bean id="accountService" class="com.DefaultAccountService"/>
    ** 
    <bean id="accountService" class="com.DefaultAccountService" scope="singleton"/>
    
    • 1
    • 2
    • 3
    • 原型
    <bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
    
    • 1

  • 相关阅读:
    进程间的通信
    AIOps指标异常检测之无监督算法
    【个人博客公网访问】使用Cpolar+Emlog在Ubuntu上轻松搭建个人博客公网访问
    cJson 学习笔记
    微软AI量化平台Qlib:你需要知道的核心知识点
    Matlab直接求贝塞尔函数的导函数
    【实用 Python 库】Python glob库:轻松应对文件和目录管理
    javaEE——线程的等待和结束
    视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
    英伟达禁售?FlashAttention助力LLM推理速度提8倍
  • 原文地址:https://blog.csdn.net/weixin_48595687/article/details/126000174