Spring之手动配置bean和自动装配bean讲解

传统的手动配置bean

相关的准备类

  • Cat类代码
public class Cat {
    public void action(){
        System.out.println("miao~");
    }
}
  • Dog类代码
public class Dog {
    public void action(){
        System.out.println("wang~");
    }
}
  • Person类代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

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

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

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

  • xml配置文件:创建每个类与bean之间的映射关系

<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="dog" class="com.Dog"/>
    <bean id="cat" class="com.Cat"/>

    <bean id="person" class="com.Person">
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    bean>
beans>
  • 测试类代码:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        System.out.println("使用手动配置bean");
        Person person = (Person) context.getBean("person");
        System.out.println(person.getName());
        person.getDog().action();
        person.getCat().action();
   }
}

反射机制与自动装配bean

反射机制是框架设计的灵魂,在spring和springBoot中应用了大量的注解,而注解配合反射机制的使用,使得框架在开发中更好的提高效率,降低代码的冗余,自动装配bean的本质就是利用反射机制来实现的。

自动装配bean的方法:

  • byType

  • byName

  • 注解

xml自动配置bean


<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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="person-auto-byName" class="com.Person" autowire="byName">
        <property name="name" value="zhangsan"/>
    bean>

    
    <bean id="person-auto-byType" class="com.Person" autowire="byType">
        <property name="name" value="lisi"/>
    bean>

    
    <import resource="bean-annotation.xml"/>

beans>


开启注解的xml文件配置




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

    
    <context:annotation-config/>

beans>


  • 使用注解后的Person类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class Person {
    //直接在需要配置的属性上使用@Autowired即可,可以省略set方法,因为注解是通过反射机制实现的
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;
    @Autowired
    @Value("mazi")
    private String name;

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

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

  • 使用注解实现自动配置bean的测试类代码
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    //自动装配bean讲解
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
    	//1.使用byName
        System.out.println("使用自动配置bean:byName方法");
        Person personauto1 = (Person) context.getBean("person-auto-byName");
        System.out.println(personauto1.getName());
        personauto1.getDog().action();
        personauto1.getCat().action();

        //2.使用byType
        System.out.println("使用自动配置bean:byType方法");
        Person personauto2 = (Person) context.getBean("person-auto-byType");
        System.out.println(personauto2.getName());
        personauto2.getDog().action();
        personauto2.getCat().action();

        //3.使用注解实现bean的自动配置
        System.out.println("使用注解实现bean的自动配置");
        Person personannotation = (Person) context.getBean("person");
        System.out.println(personannotation.getName());
        personannotation.getCat().action();
        personannotation.getDog().action();

    }
}

你可能感兴趣的:(Spring,Java)