Spring基於注解的配置——@Required、@Autowired、@Qualifier示例及與傳統注入方法的對比


@Required注釋

作用:用於屬性的set方法,那么這個屬性必須在xml文件的bean標簽里面進行配置,否則就會拋出一個BeanInitializationException異常。

首先准備一個類:

public class Person{
   private int age;
   private String name;
   @Required
   public void setAge(int age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   @Required
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
}

再准備一個測試類:

public class MainApp{
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("person.xml");
      Person person = (Person) context.getBean("p");
      System.out.println("Name : " + student.getName() );
      System.out.println("Age : " + student.getAge() );
   }
}

配置文件內容如下:

<?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 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="p" class="Annotations.injection.Person">
         <property name="name"  value="張三 />
         <property name="age"  value="18"/>
    </bean>
</beans>

大家可以自行把<property name="age" value="11"/>或者 <property name="name" value="Zara" />注釋掉,看看是否會報錯。上面的代碼才是完整的。

輸出結果:

Name : 張三
Age : 11

 

@Autowired

作用:這個標簽在不同的部位前面,作用也不一樣。但是總的來說,作用基本和其名字一樣,自動連線,只是連線對象不一樣了。

當在一個set方法前,即使我們沒在bean里面配置他的值或引用,它也會在beans里面尋找相同類型的bean去匹配,就如byType一樣。

當在一個屬性前,這個屬性可以不需要set方法。

當在一個構造函數前,尤其是有參構造函數,即使我們不給這個構造函數傳參,它也會在beans里尋找相同類型的bean,並傳遞給這個構造函數。

下面分別演示和對比其作用。

 

在set方法前

首先准備一個類,在其set方法前加上Autowired注釋:

public class Hello {
    private Hello_Son hs;
    public Hello_Son getHs() {
        return hs;
    }
    @Autowired
    public void setHs(Hello_Son hs) {
        this.hs = hs;
    }
}

為了方便演示,再准備一個類當自定義類型:

public class Hello_Son {
    public Hello_Son(){
        System.out.println("這是hello_son的無參構造函數");
    }
}

測試類:

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("ann_bean.xml");
        Hello h=(Hello)context.getBean("hello");
        h.getHs();
    }
}

區別對比:

使用了Autowired注釋的配置文件內容如下:

<?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 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="hello" class="Annotations.injection.Hello">
    </bean>
    <bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

不使用Autowired注釋的配置文件內容如下:

<?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 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="hello" class="Annotations.injection.Hello">
<!--        需要手動配置-->
        <property name="hs" ref="hs"></property>
    </bean>
    <bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

運行結果:

這是hello_son的無參構造函數

 

在屬性前(在set方法前和在屬性前結果一樣,所以一般用這個)

首先准備一個類,無需set方法:

public class Hello {
    @Autowired
    private Hello_Son hs;

    public Hello_Son getHs() {
        return hs;
    }

}

自定義類型類和上面一樣。

配置文件和上面一樣。

區別對比:

使用了Autowired注釋的屬性,不需要set方法。

不使用Autowired注釋的屬性,需要set方法。

運行結果:

這是hello_son的無參構造函數

 

在構造函數前

首先准備一個類:

public class Hello {
    private Hello_Son hs;
    @Autowired
    public Hello(Hello_Son hs) {
        System.out.println("這是hello的有參構造函數");
        this.hs=hs;
    }
    public Hello_Son getHs() {
        return hs;
    }
}

自定義類型類和配置文件不變。

區別對比:

使用了Autowired注釋的構造函數的配置文件:

<?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 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="hello" class="Annotations.injection.Hello">
    </bean>
    <bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

不使用Autowired注釋的構造函數的配置文件:

<?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 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="hello" class="Annotations.injection.Hello">
        <constructor-arg ref="hs"/>
    </bean>
    <bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

運行結果:

這是hello_son的無參構造函數
這是hello的有參構造函數

 

@Qualifier

作用:當創建多個相同類型的bean時,在使用時,只需要配置其中一個,那么這時候就可以使用@Qualifier注釋。

首先創建一個類:

public class students {
    private int age;
    private String name;

    public String getName() {
        return name;
    }

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

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}

再為學生類創建一個配置類,方便使用:

public class stu_profile {
    @Autowired
    @Qualifier("stu1")
    private students stu;

    public stu_profile() {
        System.out.println("配置類的構造函數");
    }

    public void getStu() {
        System.out.println("名字叫:" + stu.getName() + ";" + "年齡:" + stu.getAge());
    }
}

可以看到,配置類中指定了stu1,這個stu1就是一個bean的id。並和@Autowired一起使用,這樣就不用再寫個set方法了。

配置文件內容如下:

<?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 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="profile" class="Annotations.injection.stu_profile"></bean>
    <bean id="stu1" class="Annotations.injection.students">
        <property name="age" value="18"/>
        <property name="name" value="張三"/>
    </bean>
    <bean id="stu2" class="Annotations.injection.students">
        <property name="name" value="李四"/>
        <property name="age" value="19"/>
    </bean>

</beans>

測試類:

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("students.xml");
        stu_profile stu=(stu_profile)context.getBean("profile");
        stu.getStu();
    }
}

運行結果:

配置類的構造函數
名字叫:張三;年齡:18

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM