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

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"/>

posted @ 2022-07-26 17:51  路漫漫qixiuyuanxi  阅读(45)  评论(0编辑  收藏  举报