Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring的setter注入中使用@Required注释?_Java_Spring_Dependency Injection - Fatal编程技术网

Java 如何在spring的setter注入中使用@Required注释?

Java 如何在spring的setter注入中使用@Required注释?,java,spring,dependency-injection,Java,Spring,Dependency Injection,@所需的注释用法。我尝试给出上述setter方法,并尝试从上下文中查找bean,而不设置此依赖项。我得到了依赖项设置为null的对象,我期望spring会抛出一些异常。请告诉我哪里做错了 @Required to make it mandatory that the dependency has to be injected 让我们举个例子: 我们需要使用RequiredAnnotationPostProcessor来检查@Required依赖项是否已被注入 public class Bank

@所需的注释用法。我尝试给出上述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" />

与bmt的答案相同-只是在这里添加了我所做的验证。
包com.requiredAnnotation;
导入org.springframework.beans.factory.annotation.Required;
公营银行服务{
私人客户服务;
私人账单支付服务账单支付服务;
@必需的
公共无效设置CustomerService(CustomerService CustomerService){
this.customerService=customerService;
}
@必需的
公共无效setBillPaymentService(BillPaymentService BillPaymentService){
this.billPaymentService=billPaymentService;
}
公共BillPaymentService getBillPaymentService(){
返回账单支付服务;
}
公共CustomerService getCustomerService(){
返回客户服务;
}
}
包com.requiredAnnotation;
公共类客户服务{
}
包com.requiredAnnotation;
公共类付费服务{
}
包com.requiredAnnotation;
导入org.springframework.context.ConfigurableApplicationContext;
导入org.springframework.context.support.ClassPathXmlApplicationContext;
公开课考试{
公共静态void main(字符串[]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());
}
}

非常感谢,它工作起来很有魅力:)。下面是一个完整的示例,其中包含可编译状态,以便其他人可以直接使用。
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());
    }
}