Spring learning_three kinds of dependency injection object attributes_various types of injection_special value injection_automatic assembly_annotation added to ioc container

1. Three kinds of dependency injection object properties

set injection , constructor injection and namespace injection

<?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. Various types of injection

New class: 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;
    }
}

List, Array, Set, Map, and Properties type injection.

    <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. Special value injection

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

Insert picture description here
Quotation of commonly used special characters:
Insert picture description here

赋空值直接写<null>标签

4. Automatic assembly (only suitable for ref reference types)

Convention is better than configuration, not suitable for simple type injection.
Automatic assembly according to id (autowire="byName").
Automatic assembly according to type. Only one of them can be used. Type (autowire="byType")
Automatic assembly according to constructor (autowire=" constructor")

<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>

Global automatic assembly

Local automatic assembly
Insert picture description here

5. Add annotations to the ioc container

Create a new dao class: StudentDaoImpl.java
@Component() is a general annotation, which can be refined into the following:
dao layer annotation: @Repository
service layer annotation: @Service
controller layer annotation: @ControllerConfiguration
Insert picture description here
Scanner
Insert picture description here

May your heart be like flowers and trees

Guess you like

Origin blog.csdn.net/nbcsdn/article/details/98960423