[Spring]@Autowired,@Required,@Qualifier注解


@Required注解

@Required注解用於setter方法,表明這個屬性是必要的,不可少的,必須注入值

假設有個測試類,里面有name和password兩個屬性

我給兩個屬性的setter方法都加了@Required注解

package com.example.demo1.Implements;

import com.example.demo1.Interface.UserService;
import org.springframework.beans.factory.annotation.Required;

public class UserClientImpl implements UserService {

    private String name;
    private String password;

    public UserClientImpl(){}

    public UserClientImpl(String name,String password){
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    @Required
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public void save() {
        System.out.println("客戶端保存信息"+name+"--"+password);
    }
}

現在我只給一個屬性加注入,另一個不加

可以看到報錯

然后我補上注入之后就沒問題了

@Autowoired注解

 其實看名字就可以看出來,這個是跟自動裝填有關

使用它需要加一行代碼

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcess></bean>

1,在屬性前加此注解

先給定兩個類

package com.example.demo1.Other;

public class CustomerTest {
    public CustomerTest(){
        System.out.println("在Customer.構造方法中...");
    }
    public void show(){
        System.out.println("在Customer.show方法中...");
    }
}
package com.example.demo1.Implements;

import com.example.demo1.Interface.Customer;
import com.example.demo1.Other.CustomerTest;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomerImpl implements Customer {


    private String name;
    private String id;
    @Autowired
    private CustomerTest customerTest;

    public CustomerTest getCustomerTest() {
        return customerTest;
    }

    public void setCustomerTest(CustomerTest customerTest) {
        this.customerTest = customerTest;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public void show() {
        System.out.println(id+"..."+name);
        customerTest.show();
    }
}

第二個類在第三個成員變量前面加個此注解

然后applicationContext這樣寫

 <bean id="Customer"
          class="com.example.demo1.Implements.CustomerImpl">
    </bean>
    <bean id="CustomerTest" class="com.example.demo1.Other.CustomerTest"></bean>

在打印一下結果

ApplicationContext instance = new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerImpl customer = (CustomerImpl) instance.getBean("Customer");
        customer.show();
        ((ClassPathXmlApplicationContext) instance).registerShutdownHook();

可以看到Customer對象是自動裝填了的

2,在構造函數之前加此注解

效果和上面是一樣的,不演示了

3,@Autowired(required=false)的作用

這里跟之前的@Required的作用類似

默認情況下,@Autowired 注釋意味着依賴是必須的,它類似於 @Required 注釋,然而,你可以使用 @Autowired 的 (required=false) 選項關閉默認行為。

這里跟@Required的效果類似,不演示了

 

@Qualifier注解

在之前的學習注解的過程中,顯然,用自動裝配的時候,如果配置的bean是相同的類的生成的對象,會報錯,於是為了解決這個問題,@Qualifier就出來了

@Qualifier和@Autowired一起使用,在@Qualifier后面的括號里欲裝配的bean的名稱,就可以讓Spring自動給我們裝配合適的bean

首先applicationContext.xml ,因為要用到context標簽,需要用到aop.jar,所以在這之前記得引入Springframework-aop.jar,這個標簽就像之前的注解一樣,需要注冊那幾個Processor才能起作用

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

    <context:annotation-config/>
    <bean id="Profile" class="com.example.demo1.Testclass.Profile"></bean>

    <bean id="student1" class="com.example.demo1.Testclass.Student">
        <property name="name" value="student1"></property>
        <property name="age" value="2"></property>
    </bean>
    <bean id="student2" class="com.example.demo1.Testclass.Student">
        <property name="age" value="3"></property>
        <property name="name" value="student2"></property>
    </bean>
</beans>

注解是放在要裝配放屬性的上方

package com.example.demo1.Testclass;

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

public class Profile {
    @Autowired
    @Qualifier(value = "student1")
    private Student student;
    public Profile(){
        System.out.println("Inside Profile constructor." );
    }
    public void setStudent(Student student) {
        this.student = student;
    }

    public void printAge() {
        System.out.println("Age : " + student.getAge() );
    }
    public void printName() {
        System.out.println("Name : " + student.getName() );
    }
}

 

package com.example.demo1.Testclass;

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

之后Spring就會識別出合適的bean並注入了

測試代碼

ApplicationContext instance = new ClassPathXmlApplicationContext("applicationContext.xml");
        Profile profile = (Profile) instance.getBean("Profile");
        profile.printName();
        ((ClassPathXmlApplicationContext) instance).registerShutdownHook();

結果也正是這樣

 


免責聲明!

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



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