Learn from the beginning Spring-1.15 the operation of the value of Spel (3)-logical operation and conditional expression

Source: Internet
Author: User
Tags logical operators

In this chapter we discuss the logical operation of Spel and the conditional expression.

1.domain

Oven class: (The ToString method is modified because the expression of the configuration file needs to return only the property value of name that is the same as the Bean's ID)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_18;public class Oven {private String name = "";p rivate Double size = 0;private Boolean bigornot = False;public Boolean isbigornot () {return bigornot;} public void Setbigornot (Boolean bigornot) {this.bigornot = Bigornot;} Public double GetSize () {return size;} public void SetSize (double size) {this.size = size;} @Overridepublic String toString () {return name;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}


Chef class: (unchanged)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_18;public class Chief {private String name = "";p rivate Oven oven;public void Checkoven () {if (Getoven (). Isbigornot ()) {System.out.println (Getoven ()); SYSTEM.OUT.PRINTLN ("oven is big");} else {System.out.println (Getoven ()); SYSTEM.OUT.PRINTLN ("Oven is not big");}} Public String GetName () {return name;} Public Oven Getoven () {return Oven;} public void SetName (String name) {this.name = name;} public void Setoven (Oven Oven) {this.oven = Oven;}}



2. Test class: (For convenience, we just use Jack)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_18;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations = {"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_18/ Applicationcontext-test.xml "}) public class Chieftest {@Autowiredprivate ApplicationContext applicationcontext;@ testpublic void Testchief () {Chief Jack = (Chief) Applicationcontext.getbean ("Jack"); Jack.checkoven ();}}



3. Configuration file: (Focus)

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= " Http://www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http ://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/UTIL/SPRING-UTIL-3.1.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA/HTTP Www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://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 "><bean id=" Smalloven "class=" com.raylee.my_new_spring.my _new_spring.ch01.topic_1_19.oven "P:name=" Smalloven "p:size=" #{2*5} "p:bigornot=" #{10 >= 8 and 2 > 3} "/>< Bean id= "Midoven" class= "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_19.Oven" p:name= "Smalloven" p:size= "# {3*5} "p:bigornot=" #{1 = = 2} "/><bean id=" Jack "class=" com.raylee.my_new_spring.my_new_spring.ch01.topic_1_19. Chief "P:name=" Jack "><property name=" oven "ref=" #{jack.name== ' Jack '? Smalloven:midoven} "/></bean> </beans>



The main point of the configuration file is to look at the P:bigornot property inside the oven, where we compare two values not only by contrasting the symbols, but also by introducing the logical operators and (with OR and not), and one is the oven attribute of the Bean in jack, we use the conditional expression, Decide which oven to use by name.

Test output:

Smalloven
Oven is not big


Summary: In this chapter we discuss the logical operation of Spel and the conditional expression.


Catalog: http://blog.csdn.net/raylee2007/article/details/50611627

My github:https://github.com/raylee2015/my_new_spring.


Learn from the beginning Spring-1.15 the operation of the value of Spel (3)-logical operation and conditional expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.