Spring xml tag attribute day02

Posted by p-co on Tue, 22 Feb 2022 13:45:30 +0100

4)IoC configuration (XML format)

4.1)bean

  • Name: bean

  • Type: Label

  • Attribution: beans tag

  • Function: define resources in spring. Resources defined by this tag will be controlled by spring

  • Format:

    <beans>
    	<bean />
    </beans>
    
  • Basic properties:

    <bean id="beanId" name="beanName1,beanName2" class="ClassName"></bean>
    

    id: the name of the bean. Get the bean through the id value

    class: type of bean

    Name: the name of the bean. You can get the bean through the name value. It is used to alias the bean when multiple people cooperate

4.2)bean attribute scope

  • Name: scope

  • Types: properties

  • Attribution: bean tag

  • Role: define the scope of the bean

  • Format:

    <bean scope="singleton"></bean>
    
  • Value:

    • Singleton: set that the created object is saved in the spring container and is a singleton object
    • prototype: set that the created object is saved in the spring container and is a non singleton object
    • request, session, application, websocket: set the created object to be placed in the corresponding position of the web container

4.3)bean life cycle

  • Name: init method, destroy method

  • Types: properties

  • Attribution: bean tag

  • Function: defines the work done by the bean object during initialization or destruction

  • Format:

    <bean init-method="init" destroy-method="destroy></bean>
    
  • Value: the corresponding concrete method name in the class corresponding to the bean

  • matters needing attention:

    • When scope = "singleton", there is only one object in the spring container, and the init method is executed only once when creating the container

    • When scope = "prototype", the spring container needs to create multiple objects of the same type, and the init method is executed once when each object is created

    • When scope = "singleton", closing the container will lead to the destruction of the bean instance, and the destroy method will be called once

    • When scope = "prototype", the destruction of the object is controlled by the garbage collection mechanism gc(), and the destroy method will not be executed

4.4)bean object creation method (understand)

(1)factory-bean

  • Name: Factory bean

  • Types: properties

  • Attribution: bean tag

  • Function: define the creation method of bean object, create beans in the form of static factory, and be compatible with the upgrading of early legacy systems

  • Format:

    <bean class="FactoryClassName" factory-method="factoryMethodName"></bean>
    
  • Value: the static method name used to obtain the object in the factory bean

  • matters needing attention:

    • The class attribute must be configured as the class name of the static factory

(2)factory-bean,factory-method

  • Name: Factory bean, factory method

  • Types: properties

  • Attribution: bean tag

  • Function: define the creation method of bean object, create beans in the form of instance factory, and be compatible with the upgrading of early legacy systems

  • Format:

    <bean factory-bean="factoryBeanId" factory-method="factoryMethodName"></bean>
    
  • Value: the name of the instance method used to get the object in the factory bean

  • matters needing attention:

    • To create a bean using an instance factory, you first need to configure the instance factory as a bean and hand it over to spring for management

    • Factory bean is the beanId of the instance factory

4.5)DI

  • IoC (Inversion Of Control) controls flipping, and Spring reversely controls the external resources required by the application

  • DI (Dependency Injection) Dependency Injection. The resources that the application program depends on are provided by Spring. The way resources enter the application program is called injection

4.6)set injection (mainstream)

  • Name: property

  • Type: Label

  • Attribution: bean tag

  • Function: provide resources for bean s in the form of set method

  • Format:

    <bean>
    	<property />
    </bean>
    
  • Basic properties:

    <property name="propertyName" value="propertyValue" ref="beanId"/>
    

Name: the name of the attribute in the corresponding bean. It is required that the attribute must provide an accessible set method (strictly standardized. This name is the corresponding name of the set method)

Value: set the value corresponding to the non reference type attribute. It cannot be used with ref at the same time

ref: set the id of the bean corresponding to the reference type attribute. It cannot be used together with value

  • Note: a bean can have multiple property tags

4.7) constructor injection (understand)

  • Name: constructor ARG

  • Type: Label

  • Attribution: bean tag

  • Function: provide resources for bean s in the form of construction method, which is compatible with the upgrading of early legacy systems

  • Format:

    <bean>
    	<constructor-arg />
    </bean>
    
  • Basic properties:

    <constructor-arg name="argsName" value="argsValue />
    

Name: the parameter name carried by the constructor in the corresponding bean

Value: set the value corresponding to the non reference type constructor parameter. It cannot be used with ref at the same time

Other attributes:

<constructor-arg index="arg-index" type="arg-type" ref="beanId"/>

ref: set the id of the bean corresponding to the reference type constructor parameter. It cannot be used together with value

Type: sets the type of construction method parameters, which are used to match parameters by type or perform type verification

Index: sets the position of the construction method parameter, which is used to match the parameter by position. The parameter index value counts from 0

  • Note: a bean can have multiple constructor Arg tags

4.8) set type data injection

  • Name: array, list, set, map, props

  • Type: Label

  • Attribution: property tag or constructor Arg tag

  • Function: inject collection data type attribute

  • Format:

    <property>
    	<list></list>
    </property>
    

(1) Collection type data injection - list

<property name="al">
    <list>
        <value>itheima</value>
        <value>66666</value>
    </list>
</property>

(2) Collection type data injection -- props

<property name="properties">
    <props>
        <prop key="name">itheima666</prop>
        <prop key="value">666666</prop>
    </props>
</property>

(3) Collection type data injection - array

<property name="arr">
    <array>
        <value>123456</value>
        <value>66666</value>
    </array>
</property>

(4) Set type data injection - set

 <property name="hs">
     <set>
         <value>itheima</value>
         <value>66666</value>
     </set>
</property>

(5) Collection type data injection - map

<property name="hm">
    <map>
        <entry key="name" value="itheima66666"/>
        <entry key="value" value="6666666666"/>
    </map>
</property>

4.9) use p namespace to simplify configuration (understand)

  • Name: p:propertyName, p:propertyName ref

  • Types: properties

  • Attribution: bean tag

  • Function: inject attribute values into bean s

  • Format:

    <bean p:propertyName="propertyValue" p:propertyName-ref="beanId"/>
    
  • Note: to use the p command space, you need to turn on spring's support for the p command space and add the corresponding space support in the beans tab

    <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     https://www.springframework.org/schema/beans/spring-beans.xsd">
    

    Other namespaces will be opened in subsequent courses in the same way as above

  • Case:

     <bean
           id="userService"
           class="com.itheima.service.impl.UserServiceImpl"
           p:userDao-ref="userDao"
           p:bookDao-ref="bookDao"
           />
    

4.10)SpEL (understand)

  • Spring provides support for EL expressions and unifies the format of attribute injection

  • Type: attribute value

  • Attribution: value attribute value

  • Function: inject attribute values into bean s

  • Format:

    <property value="EL"></bean>
    
  • Note: all attribute values do not distinguish between reference type and reference type, and value assignment is used uniformly

  • All formats are used uniformly. value = "***********"

    • Constant #{10} #{3.14} #{2e5} #{'itcast'}

    • Reference bean #{beanId}

    • Reference bean property #{beanId.propertyName}

    • Reference bean method beanid methodName(). method2()

    • Reference static method t (Java. Lang. math) PI

    • Operator support #{3 lt 4 == 4 ge 3}

    • Regular expressions support #{user.name matches' [a-z]{6,} '}

    • Set support {[like3] #

  • Case:

     <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
         <property name="userDao" value="#{userDao}"/>
         <property name="bookDao" value="#{bookDao}"/>
         <property name="num" value="#{666666666}"/>
         <property name="version" value="#{'itcast'}"/>
    </bean>
    

4.11)properties file

  • Spring provides a mechanism to read the external properties file and use the read data to assign values to the properties of the bean

  • Operation steps

    1. Prepare external properties file

    2. Enable context namespace support

    xmlns:context="http://www.springframework.org/schema/context"
    

​ 3. Load the specified properties file

<context:property-placeholder location="classpath:filename.properties">

​ 4. Use loaded data

<property name="propertyName" value="${propertiesName}"/>
  • Note: if you need to load all the properties files, you can use * Properties means to load all the properties files

  • Note: the data is read in * * ${propertiesName} format, where propertiesName * * refers to the property name in the properties file

4.12) team development

  • Name: import

  • Type: Label

  • Attribution: beans tag

  • Role: import items from other configuration files in the current configuration file

  • Format:

    <beans>
        <import />
    </beans>
    
  • Basic properties:

    <import resource="config.xml"/>
    

resource: the name of the loaded configuration file

  • The Spring container loads multiple configuration files

    new ClassPathXmlApplicationContext("config1.xml","config2.xml");
    
  • bean definition conflict in Spring container

    • For bean s with the same id, the later defined ones override the earlier defined ones

    • Importing a configuration file can be understood as copying and pasting the imported configuration file to the corresponding location

    • The order and location of importing configuration files may lead to different final program running results

4.13)ApplicationContext

1.ApplicationContext is an interface that provides API s for accessing spring containers

2.ClassPathXmlApplicationContext is a class that implements the above functions

3. The top-level interface of ApplicationContext is BeanFactory

4.BeanFactory defines the most basic operations related to bean s

5.ApplicationContext adds several new functions to BeanFactory

Compare BeanFactory

1. The bean created by beanfactory adopts the form of delayed loading and is created only after use

2. The bean created by ApplicationContext is loaded immediately by default

FileSystemXmlApplicationContext

You can load configuration files anywhere in the file system, while ClassPathXmlApplicationContext can only load configuration files under the classpath

Topics: Java Spring xml