Spring learning_trois types d'attributs d'objet d'injection de dépendance_various types de valeur d'injection_special injection_automatic assembly_annotation ajouté au conteneur ioc

1. Trois types de propriétés d'objet d'injection de dépendance

set injection , injection de constructeur et injection d'espace de noms

<?xml version="1.0" encoding="UTF-8"?>
<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="UserDao" class="com.itheima.ioc.UserDaoImpl"/>
    <bean id="teacher" class="com.itheima.entity.Teacher">
        <!--<property name="name" value="张老师"></property>-->
        <!--<property name="age" value="13"></property>-->
        <constructor-arg  value="张老师"></constructor-arg>
        <constructor-arg  value="13"></constructor-arg>
    </bean>
    <bean id="course" class="com.itheima.entity.Course" p:courseHour="2" p:courseName="数学" p:teacher-ref="teacher">
        <!--&lt;!&ndash;相当于person.setId(1);&ndash;&gt;-->
        <!--<property name="courseHour" value="2"></property>-->
        <!--<property name="courseName" value="数学"></property>-->
        <!--&lt;!&ndash;非简单类型用ref&ndash;&gt;-->
        <!--<property name="teacher" ref="teacher"></property>-->
    </bean>
</beans>

2. Différents types d'injection

Nouvelle classe: AllCollectionType.java

package com.itheima.entity;
import java.util.*;

public class AllCollectionType {
    
    
    private List<String> list;
    private String[] array;
    private Set<String> set;
    private Map<String, String> map;
    private Properties props;

    @Override
    public String toString() {
    
    
        return "AllCollectionType{" +
                "list=" + list +
                ", array=" + Arrays.toString(array) +
                ", set=" + set +
                ", map=" + map +
                ", props=" + props +
                '}';
    }

    public AllCollectionType(List<String> list, String[] array, Set<String> set, Map<String, String> map, Properties props) {
    
    
        this.list = list;
        this.array = array;
        this.set = set;
        this.map = map;
        this.props = props;
    }
    public AllCollectionType() {
    
    
    }

    public List<String> getList() {
    
    
        return list;
    }

    public void setList(List<String> list) {
    
    
        this.list = list;
    }

    public String[] getArray() {
    
    
        return array;
    }

    public void setArray(String[] array) {
    
    
        this.array = array;
    }

    public Set<String> getSet() {
    
    
        return set;
    }

    public void setSet(Set<String> set) {
    
    
        this.set = set;
    }

    public Map<String, String> getMap() {
    
    
        return map;
    }

    public void setMap(Map<String, String> map) {
    
    
        this.map = map;
    }

    public Properties getProps() {
    
    
        return props;
    }

    public void setProps(Properties props) {
    
    
        this.props = props;
    }
}

Injection de type List, Array, Set, Map et Properties .

    <bean id="collectionDemo" class="com.itheima.entity.AllCollectionType">
        <property name="list">
            <list>
                <value>list足球</value>
                <value>list篮球</value>
                <value>list乒乓球</value>
            </list>
        </property>
        <property name="array">
            <array>
                <value>数组足球</value>
                <value>数组篮球</value>
                <value>数组乒乓球</value>
            </array>
        </property>
        <property name="set">
            <set>
                <value>map足球</value>
                <value>map篮球</value>
                <value>map乒乓球</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry>
                    <key><value>foot</value></key>
                    <value>map足球</value>
                </entry>
                <entry>
                    <key><value>bask</value></key>
                    <value>mao篮球</value>
                </entry>
                <entry>
                    <key><value>pingpang</value></key>
                    <value>map乒乓球</value>
                </entry>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="foot">prop足球</prop>
                <prop key="bask">prop篮球</prop>
                <prop key="pingpang">prop乒乓球</prop>
            </props>
        </property>
    </bean>

3. Injection de valeur spéciale

<value/>和<value></value>的区别

Insérez la description de l'image ici
Citation de caractères spéciaux couramment utilisés:
Insérez la description de l'image ici

赋空值直接写<null>标签

4. Assemblage automatique (convient uniquement aux types de référence ref)

La convention vaut mieux que la configuration, ne convient pas pour une injection de type simple.
Assemblage automatique selon id (autowire = "byName").
Assemblage automatique selon le type. Un seul d'entre eux peut être utilisé. Type (autowire = "byType")
Assemblage automatique selon le constructeur (autowire = " constructeur")

<bean id="teacher" class="com.itheima.entity.Teacher">
  	<!--<property name="name" value="张老师"></property>-->
    <!--<property name="age" value="13"></property>-->
    <constructor-arg  value="张老师"></constructor-arg>
    <constructor-arg  value="13"></constructor-arg>
</bean>
<bean id="course" class="com.itheima.entity.Course" autowire="byName">
    <!--<bean id="course" class="com.itheima.entity.Course" p:courseHour="2" p:courseName="数学" p:teacher-ref="teacher">-->
    <!--相当于person.setId(1);-->
    <property name="courseHour" value="2"></property>
    <property name="courseName" value="数学"></property>
    <!--非简单类型用ref-->
    <!--<property name="teacher" ref="teacher"></property>-->
</bean>

Assemblage automatique global Assemblage

automatique local
Insérez la description de l'image ici

5. Ajouter des annotations au conteneur ioc

Créez une nouvelle classe dao:
StudentDaoImpl.java @Component () est une annotation générale, qui peut être affinée comme suit:
annotation de couche dao: @Repository
annotation de couche de service: @
Annotation de couche de contrôleur de service: @ControllerConfiguration
Insérez la description de l'image ici
Scanner
Insérez la description de l'image ici

Que ton cœur soit comme des fleurs et des arbres

Je suppose que tu aimes

Origine blog.csdn.net/nbcsdn/article/details/98960423
conseillé
Classement