繁体   English   中英

春天如何在setter注入中使用@Required注释?

[英]How to use @Required annotation in setter injection in spring?

@必需的注释用法。 我尝试使用上面的setter方法,并尝试从上下文中查找Bean,而不设置此依赖项。 我得到的对象的依赖项设置为null,我期望spring会抛出一些异常。 请指导我我做错了什么?

@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" />

如果我们没有同时设置这两个属性,我们将在BankService配置中得到一个错误,因为两者都是@Required。

我希望这有帮助!!

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());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM