1. 程式人生 > 實用技巧 >Spring基礎:自動配置bean,註解實現自動裝配(重要)

Spring基礎:自動配置bean,註解實現自動裝配(重要)

1,bean的自動裝配

  •   自動裝配是Spring滿足bean依賴的一種方式
  •   Spring會在上下文中自動尋找,並自動給裝配屬性

  在Spring中由3種裝配方式

    在xml中顯示配置

    在java中顯示配置

    隱式的自動裝配bean(重要 )

1.1 環境搭建測試

實體類  

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


public class Cat {
    public void shout(){
        System.out.println(
"miao~"); } } public class People { private Cat cat; private Dog dog; private String name; }

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"
       xsi:schemaLocation
="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="com.king.pojo.Cat"/> <bean id="dog" class="com.king.pojo.Dog"/> <bean id="people" class="com.king.pojo.People"> <property name
="name" value="king"/> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> </bean> </beans>

單元測

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people",People.class);
        people.getCat().shout();
        people.getDog().shout();
        System.out.println(people.getName());
    }

1.2 自動裝配(autowire)

    <bean id="cat" class="com.king.pojo.Cat"/>
    <bean id="dog11" class="com.king.pojo.Dog"/>


    <!--
      byName:會自動在容器上下文中查詢,和自己物件set方法中形參對應的beanid!
         弊端:名字必須是能對應上的
      byType:會自動在容器上下文中查詢,和自己物件屬性型別相同的bean!
         弊端:型別必須唯一不能重複
    -->
    <bean id="people" class="com.king.pojo.People" autowire="byType">
        <property name="name" value="king"/>

    </bean>

小結:

  byname:需要保證所有的bean的id唯一,並且這個bean需要和自動注入的屬性的set方法值一樣

  byType:要保證所有bean的id的型別是唯一的,並且這個bean需要和自動注入的屬性一致

2,註解實現自動裝配

  jdk1.5支援的註解,spring2.5開始支援註解

  The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML

  使用註解須知:

    1,匯入約束,context約束(xmlns:context="http://www.springframework.org/schema/context"

    2,配置註解的支援

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

    <context:annotation-config/>

</beans>

    

@Autowired,直接在屬性上使用即可!也可以在ser方法使用

使用@Autowired可以省略掉set方法,(智慧的,可用byType也可用byName)

科普:

@Nullable 欄位標記這個註解,說明這個欄位可以為null

也可以

    //兩種不同註解實現自動配置
    //此註解常用
    @Autowired
    @Qualifier(value = "cat2")
    private Cat cat;

    //byName的,也可以通過自定義名去找
    @Resource(name = "dog3")
    private Dog dog;
    private String name;

小結:

  @Resource和@Autowired區別

    都是自動裝配的註解,都可以放在屬性欄位上

    @Autowired 預設byType方式實現,如果沒有在走byName,而且必須要求這個物件存在 【常用】

    @Resource預設通過byname的方式實現的,如果找不到名字,則按byType的方式實現,如果兩個都找不到的情況下,就報錯【常用】

    執行順序不同