ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-07-26 18:03:41  阅读:170  来源: 互联网

标签:definition Scopes 作用域 spring bean -- single 注入 属性


4.IOC创建对象的过程

  • 使用无参构造创造

image-20220723220940611

image-20220723221011139

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

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

5.IO注入

(1)前面的构造器注入

(2)set注入

<?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="user" class="cn.itnanls.User">
       <!--构造注入——参数类型注入-->
       <!--<constructor-arg type="java.lang.Integer" value="12"/>-->
       <!--<constructor-arg type="java.lang.String" value="Tom"/>-->

       <!--构造注入——下标注入-->
       <!--<constructor-arg index="0" value="tom"/>-->
       <!--<constructor-arg index="1" value="12"/>-->

       <!--构造注入——名字注入,最常用-->
       <!--<constructor-arg name="name" value="lucy"/>-->
       <!--<constructor-arg name="age" value="12"/>-->

       <!--setter注入-->
       <!--<property name="name" value="tom"/>-->

   </bean>
</beans>

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

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>

(4)c命名空间注入

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

注意导入头文件

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

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"/>
*<!-- the following is equivalent, though redundant (singleton scope is the default) -->* 
<bean id="accountService" class="com.DefaultAccountService" scope="singleton"/>
  • 原型
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

标签:definition,Scopes,作用域,spring,bean,--,single,注入,属性
来源: https://www.cnblogs.com/lumanmanqixiuyuanxi/p/16521971.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有