Spring auto assembly Bean

Keywords: xml Spring encoding Java

Automatic assembly of beans can simplify our code. Here is an example. First, create three entity classes

package com.zhiying.pojo;

public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}
package com.zhiying.pojo;

public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}
package com.zhiying.pojo;

public class People {
    private String name;
    private Dog dog;
    private Cat cat;

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

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

Then the configuration file, the core

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dog" class="com.zhiying.pojo.Dog"/>
    <bean id="cat" class="com.zhiying.pojo.Cat"/>

<!--    This is the normal way to inject-->
<!--    <bean id="people" class="com.zhiying.pojo.People">-->
<!--        <property name="name" value="He Zhi Ying"/>-->
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
<!--    </bean>-->


<!--
    //Automatic assembly with autowire
    byName: Will automatically find in the container context, and their own objects set The value after the method corresponds to the beanid
    byType: It will automatically search in the container context, which is the same as its own property type bean
-->

<!--    use autowire Automatic assembly,Use byName-->
<!--    <bean id="people" class="com.zhiying.pojo.People" autowire="byName">-->
<!--        <property name="name" value="He Zhi Ying"/>-->
<!--    </bean>-->

<!--    use autowire Automatic assembly,Use byType-->
        <bean id="people" class="com.zhiying.pojo.People" autowire="byType">
            <property name="name" value="He Zhi Ying"/>
        </bean>

</beans>

Finally, the test

import com.zhiying.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = (People) context.getBean("people");
        people.getCat().shout();
        people.getDog().shout();
    }
}

Conclusion:

When byName is used, you need to ensure that all bean IDs are unique, and that the bean must be consistent with the value of the set method of the auto injected property

When byType is used, the class of all beans should be unique, and the bean should be consistent with the type of the automatically injected property

Annotation for automatic assembly

@Autowired annotation

The same first is entity class

package com.zhiying.pojo;

public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}
package com.zhiying.pojo;

public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}
package com.zhiying.pojo;

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

public class People {

    private String name;
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

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

Then the configuration file needs to be annotated

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

<!--    Open notes-->
    <context:annotation-config/>

    <bean id="cat" class="com.zhiying.pojo.Cat"/>
    <bean id="dog" class="com.zhiying.pojo.Dog"/>
    <bean id="people" class="com.zhiying.pojo.People"/>

</beans>

And then there's the test

import com.zhiying.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = (People) context.getBean("people");
        people.getCat().shout();
        people.getDog().shout();
    }
}

@Resource comments

This annotation is provided by Java, but the configuration file cannot be few. Only the javax package is imported into the People class. Here, only the People class is modified, and others remain unchanged

package com.zhiying.pojo;

import javax.annotation.Resource;

public class People {

    private String name;
    @Resource
    private Dog dog;
    @Resource
    private Cat cat;

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

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

Conclusion:

@The difference between Autowired and @ Resource:

@Autowired is implemented by byType, and the object must exist

@Resource is implemented by byName. If the name cannot be found, it is implemented by byType, and no error can be found

Published 351 original articles, won praise 236, visited 40000+
Private letter follow

Posted by fotobleu on Sat, 11 Jan 2020 06:25:42 -0800