简体   繁体   中英

How to use @Required annotation in setter injection in spring?

@required annotation usage. I tried giving this above setter method and tried to find the bean from context without setting this dependency. I got the object with the dependency is set null, my expectation was spring will throw some exception. Please guide me where I am doing wrong?

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

Let us example: We need to use RequiredAnnotationPostProcessor which checks whether @Required dependencies have been injected or not

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

The configuration is like

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

If we don't set both the properties we will get an error for the BankService configuration as both are @Required.

I hope this helps!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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