@必需的註釋用法。我試着給上面這個setter方法,並試圖從上下文中找到bean而不設置這個依賴關係。我得到的對象與依賴項設置爲空,我的期望是春天會拋出一些異常。請指導我在哪裏做錯了?如何在春季使用setter注入@Required註解?
0
A
回答
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
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());
}
}
相關問題
- 1. 春季注入Maven
- 2. 瞭解春季註釋DI
- 3. 通過Java註解春季
- 4. 春天@Required註解工作不正常
- 5. 注入枚舉(春季)
- 6. 春季Bean依賴注入
- 7. 春季注入枚舉
- 8. 我可以使用AOP註釋在春季注入代碼嗎?
- 9. 春天注入由註釋值進入實施春季類
- 10. 如何在春季開機註冊ServletContextListener
- 11. 春季誤注入春天對象注射
- 12. 春:問題的setter依賴注入
- 13. 使用setter注入的ActionFilter
- 14. 春季啓動 - 什麼註解需要
- 15. 錯誤春季自動裝配註解
- 16. 春季安全註解@Preauthoritze不工作
- 17. 春季註解中的大括號
- 18. Laravel setter注入
- 19. 在@春季沒有@Autowire註釋的Bean注入
- 20. 春季引導豆注入豆注入方法
- 21. 春季自我注入交易
- 22. 春季數組依賴注入?
- 23. 春季啓動:注入模擬Runner類
- 24. 春季注入番石榴緩存
- 25. 春季注入豆過濾器
- 26. 春@Transactional註解的使用
- 27. 如何在春季使用註釋進行方法替換?
- 28. 春季注射技巧
- 29. 春季安全元註釋
- 30. 春季安全註銷
非常感謝它的工作就像一個魅力:)。以下是可編譯狀態的完整示例,以便其他人可以直接使用。 –
嗯。不用謝!! – iMBMT