Scope of Spring 4-bean: Automatic Assembly

Spring

automatic assembly

  • xml file

    • <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>
      
      <!-- 
      	 You can use the autowire attribute to specify how to assemble automatically.
      	 byName assembles automatically according to the name of the bean and the setter-style attribute name of the current bean. If there is a match, it assembles automatically. If there is no match, it does not match.
      	 ByType matches the type of the bean and the type of the property of the current bean. If there is more than one type matching bean in the IOC container, an exception is thrown. For example, if there are two address es above, byType cannot be used.
      -->
      
      <bean id="person" class="com.atguigu.spring.beans.autowire.Person"
      	 p:name="Tom" autowire="byName">
      
      </bean>
      
  • Address class in autowire

  • Person class in autowire

  • Car class in autowire

Abstract bean

  • xml file

    •  <! - abstract beans: beans whose abstract attribute is true cannot be instantiated by IOC containers and can only be used to inherit configurations 
        If the class attribute of a bean is not specified, the bean must be an abstract bean and the subbean needs to write the class attribute - >.
      
        <bean id="address" p:city="BeiJing" p:street="WuDaoKou" abstract="true"></bean>
      
      	Inheritance of bean configuration: specify which bean configuration to inherit using the bean's parent property - >
      
        <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>
      
        <! - Requires that when configuring Person, there must be an associated car! That is to say, the person bean depends on the Car bean - >.
      
        <bean id="person" class="com.atguigu.spring.beans.autowire.Person" 
        	p:name="Tom" p:address-ref="address2" depends-on="car">
      
        </bean>
      

Scope of bean s

  • xml file

    • <!-- 
      Use the scope attribute of the bean to configure the scope of the bean
      		singleton: The bean instance created at the beginning of the default container. Only one bean is created throughout the container's declaration cycle. Single case
      	 	proptotype: Prototype. Container initialization does not create an instance of a bean, but creates a new bean instance every time a request is made and returns
      -->
      
      <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>
      
  • Car class in scope

Use speT to assign attributes

  • xml medium
    •  <bean id="address" class="com.atguigu.spring.beans.spel.Address">
        <!-- Use spel Assign a literal value to an attribute -->
        		<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>
        		<!-- Use SpET Static attributes of reference classes-->
        		<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
        </bean>
       <bean id="person" class="com.atguigu.spring.beans.spel.Person">
        <!-- Use SpEL To quote others Bean -->
        		<property name="car" value="#{car}"></property>
        		<!-- Use SpEL To quote others Bean Attribute -->
        		<property name="city" value="#{address.city}"></property>
        		<!-- stay SpEL Using Operators -->
        		<property name="info" value="#{car.price >300000 ? 'Golden collar':'white collar'}"></property>
        		<property name="name" value="Tom"></property>
        </bean>
      

Automatic scanning

  • xml file

    •  <!-- Appoint spring IOC Packages scanned by containers -->
        <!-- Can pass resource-pattern Specify scanning resources -->
      
        <context:component-scan
        			base-package="com.atguigu.spring.bean.annotation"
        			resource-pattern="repository/*.class"
        		>
        </context:component-scan>
      
       <!-- context:exclude-filter Subnodes specify which components to exclude from the specified expression -->
      
        <context:exclude-filter type="annotation" 
        		expression="org.springframework.stereotype.Repository"/>
      
       <!-- context:include-filter The child node specifies the component that contains which expressions, and the byte point needs to use-default-filters="true(or false)"Cooperative use -->
      
        <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>`
      

Import Property Files

  • xml file

    • <!-- Import Property Files -->
      
      <context:property-placeholder location="classpath:db.properties"/>
      
      <bean id="dataSource" class="">
      		<!-- Attributes using externalized property files -->
      		<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>
      
  • In db.properties

    • user:root
      password=1234
      driverclass=com.mysql.jdbc.Driver
      jdbcurl=jdbc:mysql:///test

Keywords: Spring Attribute xml MySQL

Added by jonker on Sat, 14 Sep 2019 14:56:25 +0300