2015-12-30 79 views

回答

0
@Required to make it mandatory that the dependency has to be injected 

讓我們例如: 我們需要使用RequiredAnnotationPostProcessor,檢查@Required依賴是否已經注入或不

public class BankService { 
    private CustomerService customerService; 
    private BillPaymentService billPaymentService; 
    @Required 
    public void setCustomerService(CustomerService customerService) { 
    this.customerService = customerService; 
    } 
    @Required 
    public void setBillPaymentService(BillPaymentService billPaymentService) { 
    this.billPaymentService = billPaymentService; 
} 
} 

的配置是這樣

<bean id="custService" class="xml.CustomerServiceImpl" /> 
<bean id="billingService" class="xml.BillPaymentServiceImpl" /> 
<bean id="bankService" class="xml.BankServiceImpl"> 
<property name="customerService" ref="custService" /> 
<property name="billPaymentService" ref="billingService" /> 
</bean> 
<bean class=」o.s.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /> 

如果我們不」 t設置這兩個屬性,我們將得到BankService配置的錯誤,因爲它們都是@Required。

我希望這有助於!

+0

非常感謝它的工作就像一個魅力:)。以下是可編譯狀態的完整示例,以便其他人可以直接使用。 –

+0

嗯。不用謝!! – iMBMT

0
Same anwer by bmt - just adding here what i did to verify. 

package com.requiredAnnotation; 

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

public class BankService { 
    private CustomerService customerService; 
    private BillPaymentService billPaymentService; 

    @Required 
    public void setCustomerService(CustomerService customerService) { 
     this.customerService = customerService; 
    } 

    @Required 
    public void setBillPaymentService(BillPaymentService billPaymentService) { 
     this.billPaymentService = billPaymentService; 
    } 

    public BillPaymentService getBillPaymentService() { 
     return billPaymentService; 
    } 

    public CustomerService getCustomerService() { 
     return customerService; 
    } 
} 

package com.requiredAnnotation; 

public class CustomerService { 

} 


package com.requiredAnnotation; 

public class BillPaymentService { 

} 


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    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 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 


    <bean id="customerService" class="com.requiredAnnotation.CustomerService" /> 
    <bean id="billPaymentService" class="com.requiredAnnotation.BillPaymentService" /> 
    <bean id="bankService" class="com.requiredAnnotation.BankService"> 
     <property name="customerService" ref="customerService" /> 
     <!-- <property name="billPaymentService" ref="billPaymentService" /> --> 
    </bean> 
    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /> 

</beans> 

package com.requiredAnnotation; 

import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class Test { 
    public static void main(String[] args) { 
     ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:com/requiredAnnotation/beans-required.xml"); 

     BankService bankService = applicationContext.getBean(BankService.class); 
     System.out.println("bankService got."); 
     System.out.println("getCustomerService - " + bankService.getCustomerService()); 
     System.out.println("getBillPaymentService - " + bankService.getBillPaymentService()); 
    } 
}