Spring配置和依赖注入

3、配置(Setting)

3,1、别名

alias 设置别名 , 为bean设置别名 , 可以设置多个别名

<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="userT" alias="userNew"/>

3.2、Bean的配置

<!--bean就是java对象,由Spring创建和管理-->

<!--
   id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
   如果配置id,又配置了name,那么name是别名
   name可以设置多个别名,可以用逗号,分号,空格隔开
   如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
   class是bean的全限定名=包名+类名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
   <property name="name" value="Spring"/>
</bean>

3.3、import

团队的合作通过import来实现 .

<import resource="{path}/beans.xml"/>

4、依赖注入(DI)

Dependency Injection

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

4.1、构造器注入

例子参考IOC,2.3IOC创建对象的方式

4.2、Setter注入

  • pojo

    • Address.java

      public class Address {
      
          private String address;
      
          public String getAddress() {
              return address;
          }
      
          public void setAddress(String address) {
              this.address = address;
          }
      
          @Override
          public String toString() {
              return "Address{" +
                      "address='" + address + '\'' +
                      '}';
          }
      }
      
    • Student.java

      public class Student {
          private String name;
          private Address address;
          private String[] books;
          private List<String> hobbys;
          private Map<String,String> card;
          private Set<String> games;
          private String wife;
          private Properties info;
          
          ... Getter\Setter\toString
      }
      
  • bean.xml

    <?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="address" class="com.qiang.pojo.Address">
            <property name="address" value="四川成都"/>
        </bean>
    
        <bean id="student" class="com.qiang.pojo.Student">
            <!--普通值注入:value-->
            <property name="name" value="一个木木的笨蛋"/>
            <!--bean注入:ref-->
            <property name="address" ref="address"/>
            <!--数组注入:<array> -->
            <property name="books">
                <array>
                    <value>西游记</value>
                    <value>红楼梦</value>
                    <value>水浒传</value>
                </array>
            </property>
            <!--List注入:list-->
            <property name="hobbys">
                <list>
                    <value>打球</value>
                    <value>刷剧</value>
                    <value>学Java</value>
                </list>
            </property>
            <!--Map注入:map-->
            <property name="card">
                <map>
                    <entry key="中国邮政储蓄银行" value="6217..."/>
                    <entry key="中国银行" value="..."/>
                </map>
            </property>
            <!--Set注入:set-->
            <property name="games">
                <set>
                    <value>PUBG</value>
                    <value>LOL</value>
                </set>
            </property>
            <!--null注入:null-->
            <property name="wife">
                <null/>
            </property>
            <!--Properties注入:props-->
            <property name="info">
                <props>
                    <prop key="学号">12345</prop>
                    <prop key="性别"></prop>
                </props>
            </property>
        </bean>
    </beans>
    
  • 测试

    @Test
    public void Ditest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
    

4.3、其他方式注入(p&c命名空间)

  • 导入约束
    在这里插入图片描述

  • beans.xml

    <!--p(属性: properties)命名空间 , 属性依然要设置set方法-->
    <bean id="user-p" class="com.qiang.pojo.User" p:name="一个Java小白" p:age="22"/>
    
    <!--c(构造: Constructor)命名空间 , 属性依然要设置set方法-->
    <bean id="user-c" class="com.qiang.pojo.User" c:name="甘帅帅" c:age="22"/>
    
  • 测试(略)

4.4、bean的作用域

在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 .

在这里插入图片描述

singleton(单例作用域)

当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。

在这里插入图片描述

注意,Singleton作用域是Spring中的默认作用域。要在XML中将bean定义成singleton,可以这样配置:

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

prototype(原型作用域)

当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。

在这里插入图片描述

根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

Request

当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

 <bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>

针对每次HTTP请求,Spring容器会根据loginAction bean的定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。

Session

当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

 <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值