Using mathematical, logical, and relational operators

In some of the scenarios, the values of the dependencies need mathematical, logical, or relational operators to define and place the values of the dependencies. You can find out how to use them in the following demo:

  1. We will use the same project (Ch02_SpringEL) defined in the previous example.
  2. Define a bean for Customer in beans.xml, as shown in the following piece of code:
<bean id="cust_calculation"     
      class="com.ch02.beans.Customer"> 
<property name="cust_name" value="Bina"/> 
<property name="cust_id" value="#{2}"/> 
<property name="prod_name" value="#{'Samsung Fridge'}"/> 
<property name= 
    "prod_price" value="#{27670.50*12.5}"/> 
<property name="second_handed" 
    value="#{cust_calculation.prod_price > 25000}"/> 
</bean> 

The value of prod_price is calculated at runtime with the help of a mathematical operator, and the value for second_handed is evaluated by a relational operator.

  1. Write TestCustomer_Cal to get cust_calculation, and display its properties as follows:
public static void main(String[] args) { 
  // TODO Auto-generated method stub 
  ApplicationContext context=new 
    ClassPathXmlApplicationContext("beans.xml");   
  Customer customer=    
    (Customer)context.getBean("cust_calculation"); 
  System.out.println(customer); 
  System.out.println(customer.isSecond_handed()); 
} 

Throughout this chapter, we have seen how to do the configuration. However, we missed a very important point--loose coupling. Everywhere we use classes that lead to tight coupling, we know programming by contract gives developers the facility to write loosely coupled modules. We avoided the use of interfaces for all the preceding demos done in this chapter, because it may make them unnecessarily complex. However, in real-time applications, you will find the use of interfaces at each stage in Spring. From the next chapter onwards, we will use the standard way of writing Spring-based applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.81.94