Spring4-bean的作用域&自动装配

Spring

自动装配

  • xml文件中

    • <bean id="address" class="com.atguigu.spring.beans.autowire.Address"
      p:city="BeiJing" p:street="HuiLongGuan">
      
      </bean>
      
      <bean id="address2" class="com.atguigu.spring.beans.autowire.Address"
      	 p:city="ShangHai" p:street="ZhongShan">
      
      </bean>
      
      <bean id="car" class="com.atguigu.spring.beans.autowire.Car"
      	 p:brand="Audi" p:price="300000">
      
      </bean>
      
      <!-- 
      	 可以使用autowire 属性指定自动装配的方式,
      	 byName根据bean的名字和当前bean的setter风格的属性名进行自动装配 ,若有匹配的则进行自动装配,若没有匹配的,则不匹配   注意:名字一定要一致才行
      	 byType根据bean的类型和当前bean的属性的类型进行匹配,若IOC容器中有1个以上的类型匹配的bean,则抛异常.   比如上面有两个address就不能使用byType
      -->
      
      <bean id="person" class="com.atguigu.spring.beans.autowire.Person"
      	 p:name="Tom" autowire="byName">
      
      </bean>
      
  • autowire中Address类

    • 在这里插入图片描述
  • autowire中Person类

    • 在这里插入图片描述
  • autowire中Car类

    • 在这里插入图片描述

抽象bean

  • xml文件中

    •  <!-- 抽象bean:bean的abstract属性为true的bean,这样的bean不能被IOC容器实例化,只能用来继承配置 
        若某一个bean的class属性没有指定,则该bean必须是一个抽象bean,子bean需要写出class属性-->
      
        <bean id="address" p:city="BeiJing" p:street="WuDaoKou" abstract="true"></bean>
      
      	<!-- bean配置的继承:使用bean的parent属性指定继承哪个bean的配置 -->
      
        <bean id="address2" class="com.atguigu.spring.beans.autowire.Address" 
        	p:street="DaZhongSi" parent="address">
      
        </bean>
      
        <bean id="address3" class="com.atguigu.spring.beans.autowire.Address"  
        	p:city="Nanyang" p:street="DaZhongSi" parent="address">
      
        </bean>
      
       <bean id="car" class="com.atguigu.spring.beans.autowire.Car"
        p:brand="Audi" p:price="300000">
      
        </bean>
      
        <!-- 要求在配置Person时,必须要有一个关联的car! 也就是说person这个bean依赖于Car这个bean -->
      
        <bean id="person" class="com.atguigu.spring.beans.autowire.Person" 
        	p:name="Tom" p:address-ref="address2" depends-on="car">
      
        </bean>
      

bean的作用域

  • xml文件中

    • <!-- 
      使用bean的scope属性来配置bean的作用域
      		singleton :默认值 容器初始时创建的bean实例。在整个容器的声明周期内只创建这一个bean。单例
      	 	proptotype:原型的。容器初始化不创建bean的实例,而在每次请求时都创建一个新的Bean实例,并返回
      -->
      
      <bean id="car" 
      		class="com.atguigu.spring.beans.scope.Car"
      		scope="singleton">
      		<property name="brand" value="Audi"></property>
      		<property name="price" value="300000"></property>
      </bean>
      
  • scope中的Car类

    • 在这里插入图片描述

使用speT为属性赋值

  • xml中
    •  <bean id="address" class="com.atguigu.spring.beans.spel.Address">
        <!-- 使用spel为属性赋一个字面值 -->
        		<property name="city" value="#{'BeiJing'}"></property>
        		<property name="street" value="WuDaoKou"></property>
        </bean>
       <bean id="car" class="com.atguigu.spring.beans.spel.Car">
        <property name="brand" value="Audi"></property>
        		<property name="price" value="500000"></property>
        		<!-- 使用SpET 引用类的静态属性-->
        		<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
        </bean>
       <bean id="person" class="com.atguigu.spring.beans.spel.Person">
        <!-- 使用SpEL来引用其他的Bean -->
        		<property name="car" value="#{car}"></property>
        		<!-- 使用SpEL来引用其他的Bean的属性 -->
        		<property name="city" value="#{address.city}"></property>
        		<!-- 在SpEL中使用运算符 -->
        		<property name="info" value="#{car.price >300000 ? '金领':'白领'}"></property>
        		<property name="name" value="Tom"></property>
        </bean>
      

自动扫描

  • xml文件中

    •  <!-- 指定spring IOC容器扫描的包 -->
        <!-- 可以通过resource-pattern 指定扫描的资源 -->
      
        <context:component-scan
        			base-package="com.atguigu.spring.bean.annotation"
        			resource-pattern="repository/*.class"
        		>
        </context:component-scan>
      
       <!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件 -->
      
        <context:exclude-filter type="annotation" 
        		expression="org.springframework.stereotype.Repository"/>
      
       <!-- context:include-filter 子节点指定包含哪些表达式的组件,该字节点需要 use-default-filters="true(或false)"配合使用 -->
      
        <context:include-filter type="annotation" 
        		expression="org.springframework.stereotype.Repository"/>
      
       <context:component-scan
        base-package="com.atguigu.spring.bean.annotation"
        		use-default-filters="false">
      
        	<!--<context:exclude-filter type="assignable"
        		expression="com.atguigu.spring.bean.annotation.repository.UserRepository"/>-->
        		
          	<context:include-filter type="assignable"
        			expression="com.atguigu.spring.bean.annotation.repository.UserRepository"/>
        </context:component-scan>`
      

导入属性文件

  • xml文件中

    • <!-- 导入属性文件 -->
      
      <context:property-placeholder location="classpath:db.properties"/>
      
      <bean id="dataSource" class="">
      		<!-- 使用外部化属性文件的属性 -->
      		<property name="user" value="${user}"></property>
      		<property name="password" value="${password}"></property>
      		<property name="driverClass" value="${driverclass}"></property>
      		<property name="jdbcUrl" value="${jdbcurl}"></property>
      </bean>
      
  • db.properties中

    • user:root
      password=1234
      driverclass=com.mysql.jdbc.Driver
      jdbcurl=jdbc:mysql:///test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值