Spring uses annotations to inject objects

Inject attributes by annotation

jdk1.5 supports annotations, and spring 2.5 supports annotations.

1. Introduce the context file header into the spring configuration file

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

http://www.springframework.org/schema/context    https://www.springframework.org/schema/context/spring-context.xsd

2. Start attribute annotation

<context:annotation-config/>

1. @Autowired

  • @Autowired is automatically assembled by type and does not support id matching.
  • You need to import the package of spring AOP!

Test:
1. Remove the set method in the People class (you can remove it or not) and use @ Autowired annotation

package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Autowired;

public class People {
    @Autowired(required = false)
    private Cat cat;
    @Autowired(required = false)
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

2.applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>
    <bean id="cat" class="com.kuang.pojo.Cat"/>
    <bean id="dog" class="com.kuang.pojo.Dog"/>
    <bean id="people" class="com.kuang.pojo.People"/>

</beans>

3. Test result: successful

    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        People bean = context.getBean("people", People.class);

        bean.getCat().shout();
        bean.getDog().shout();

    }

[little crazy God science popularization time]
@Autowired(required=false) Description: false, the object can be null;true, the object must be saved and cannot be null.

//If the allowed object is null, set required = false, and the default is true @Autowired(required = false)
private Cat cat;

2. @Qualifier

  • @Autowired is automatically assembled according to the type. With @ Qualifier, it can be automatically assembled according to byName
  • @Qualifier cannot be used alone.

Test verification:

  1. Entity class
package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class People {
    @Autowired(required = false)
    @Qualifier(value = "cat1")
    private Cat cat;
    @Autowired(required = false)
    @Qualifier(value = "dog1")
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

  1. configuration file
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>
    <bean id="cat1" class="com.kuang.pojo.Cat"/>
    <bean id="cat2" class="com.kuang.pojo.Cat"/>
    <bean id="dog1" class="com.kuang.pojo.Dog"/>
    <bean id="dog2" class="com.kuang.pojo.Dog"/>
    <bean id="people" class="com.kuang.pojo.People"/>

</beans>
  1. Test class: the running result is successful.
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        People bean = context.getBean("people", People.class);

        bean.getCat().shout();
        bean.getDog().shout();

    }

3.@Resource

  • @If the Resource has a specified name attribute, first search the assembly by name according to the attribute;
  • Secondly, assemble in the default byName mode;
  • If none of the above is successful, it will be assembled automatically by byType.
  • If they are not successful, an exception is reported.

Coding:

  1. Entity class
public class People {
    @Resource(name = "cat2")
    private Cat cat;
    @Resource
    private Dog dog;
    private String name;
}
  1. spring configuration file
    <bean id="cat1" class="com.kuang.pojo.Cat"/>
    <bean id="cat2" class="com.kuang.pojo.Cat"/>
    <bean id="dog1" class="com.kuang.pojo.Dog"/>
    <bean id="people" class="com.kuang.pojo.People"/>

  1. Test class
 @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        People bean = context.getBean("people", People.class);

        bean.getCat().shout();
        bean.getDog().shout();

    }

4. Summary

@Similarities and differences between Autowired and @ Resource:

  1. @Both Autowired and @ Resource can be used to assemble bean s. Can be written on a field or on a setter method.
  2. @Autowired is assembled by type by default (belonging to the spring specification). By default, dependent objects must exist. If you want to allow null values, you can set its required property to false, such as @ Autowired(required=false). If I
    If you want to use the name assembly, you can use it in combination with the @ Qualifier annotation
  3. @Resource (belonging to J2EE complex), which is assembled by name by default, and the name can be specified through the name attribute. If
    If the name attribute is not specified, when the annotation is written on a field, the field name is used by default to search by name. If the annotation is written on a setter method, the attribute name is used by default for assembly. Assemble by type when no bean matching the name is found. However, it should be noted that once the name attribute is specified, it will only be assembled by name.
    They have the same function. They inject objects by annotation, but the execution order is different@ Autowired byType first, @ Resource byName first.

Tags: Java Spring bean

Posted on Sat, 11 Sep 2021 23:38:02 -0400 by seenu_vas80