The way Spring injects beans

  • Annotations inject

A case in field

Annotation injection

For bean injection, in addition to using XML configuration, the configuration of annotations simplifies development and makes the program look more concise. For annotation interpretation, Spring has a special interpreter for annotation, the definition of annotation parsing, the implementation of the corresponding bean object injection, reflection technology implementation.

Add the spring-aop JAR package spring-aop-4.3.2.release.jar

2.Xml configuration: Add context namespace and XSD address

3. Add context:annotation-config/ configuration


      
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <bean id="userDao" class="com.xxx.demo.UserDao"></bean>
    <bean id="userService" class="com.xxx.demo.UserService"></bean>
</beans>
Copy the code

Common annotation types for bean injection

@autowired property field or set method

@resource property field or set method

The difference between: @autoWired defaults to bean type matching which can be modified by name matching with @qualifier using @resource by name by default. The name can be specified using the name attribute. If the name attribute is not specified, when annotations are written to the field, the default is to take the field name for matching injection. If the annotation is written on a setter method, it defaults to the property name for assembly. Assemble by type when no bean matching the name can be found. Note, however, that if the name attribute is specified, it will only be assembled by name.

The @Resource annotation is recommended for J2EE and reduces coupling to Spring.

extension

IoC collection type attribute injection

List collection injection

<bean id="listDI" class="com.xxx.demo.ListDI">
    <property name="list">
        <list>
            <value>Henan stewed noodles</value>
            <value>South saozi noodles</value>
            <value>Oil spilt surface</value>
            <value>The instant noodles</value>				
        </list>
    </property>
</bean>
Copy the code

Set collection injection

<bean id="listDI" class="com.xxx.demo.ListDI">
    <property name="set">
        <set>
            <value>Happy little steamed bread</value>
            <value>Big steamed buns in the north</value>
            <value>Tianjin twist</value>
            <value>Xinjiang pie</value>				
        </set>
    </property>
</bean>
Copy the code

Map type attribute injection

<bean id="userServiceImpl"class="com.xxx.demo.ListDI">
    <property name="map">
    <map>
      <entry>
        <key><value>henan</value></key>
        <value>Yuntai Mountain scenery</value>
      </entry>        
      <entry>
        <key><value>Shanghai</value></key>
        <value>The pagoda</value>
      </entry>
      <entry>
        <key><value>Beijing</value></key>
        <value>The Forbidden City.</value>
      </entry>
    </map>      
</property>
</bean>
Copy the code
for(Map.Entry<String,Object> entry:map.entrySet()){
    System.out.println("key:"+entry.getKey()+":value"+entry.getValue());
}
Copy the code

Properties Property injection

<bean id="userServiceImpl" class="com.xxx.demo.ListDI">
    <property name="prop">
        <props>
            <prop key="Beijing">The Great Wall</prop>
            <prop key="Shanghai">The Oriental pearl tower</prop>
            <prop key="Xi 'an">The Terra Cotta Warriors</prop>   
        </props>
	</property>
</bean>
Copy the code
public void printProperties(a){
    Set<Map.Entry<Object,Object>> set=properties.entrySet();
    Iterator<Map.Entry<Object,Object>> iterator=set.iterator;
    while(iterator.hasNext()){
        Map.Entry<Object,Object> entry=iterator.next();
        System.out.println(entry.getKey()+"..."+entry.getValue())
    }
}

Copy the code