package com.h3.core;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
@component("customerbean")
public class customer {
//relational operators
@value("#{1 == 1}") //true
private boolean testequal;
@value("#{1 != 1}") //false
private boolean testnotequal;
@value("#{1 < 1}") //false
private boolean testlessthan;
@value("#{1 <= 1}") //true
private boolean testlessthanorequal;
@value("#{1 > 1}") //false
private boolean testgreaterthan;
@value("#{1 >= 1}") //true
private boolean testgreaterthanorequal;
//logical operators , numberbean.no == 999
@value("#{numberbean.no == 999 and numberbean.no < 900}") //false
private boolean testand;
@value("#{numberbean.no == 999 or numberbean.no < 900}") //true
private boolean testor;
@value("#{!(numberbean.no == 999)}") //false
private boolean testnot;
//mathematical operators
@value("#{1 + 1}") //2.0
private double testadd;
@value("#{'1' + '@' + '1'}") //1@1
private string testaddstring;
@value("#{1 - 1}") //0.0
private double testsubtraction;
@value("#{1 * 1}") //1.0
private double testmultiplication;
@value("#{10 / 2}") //5.0
private double testdivision;
@value("#{10 % 10}") //0.0
private double testmodulus ;
@value("#{2 ^ 2}") //4.0
private double testexponentialpower;
@override
public string tostring() {
return "customer [testequal=" + testequal + ", testnotequal="
+ testnotequal + ", testlessthan=" + testlessthan
+ ", testlessthanorequal=" + testlessthanorequal
+ ", testgreaterthan=" + testgreaterthan
+ ", testgreaterthanorequal=" + testgreaterthanorequal
+ ", testand=" + testand + ", testor=" + testor + ", testnot="
+ testnot + ", testadd=" + testadd + ", testaddstring="
+ testaddstring + ", testsubtraction=" + testsubtraction
+ ", testmultiplication=" + testmultiplication
+ ", testdivision=" + testdivision + ", testmodulus="
+ testmodulus + ", testexponentialpower="
+ testexponentialpower + "]";
}
}
package com.h3.core;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
@component("numberbean")
public class number {
@value("999")
private int no;
public int getno() {
return no;
}
public void setno(int no) {
this.no = no;
}
}
执行
customer obj = (customer) context.getbean("customerbean");
system.out.println(obj);
输出结果
customer [testequal=true, testnotequal=false, testlessthan=false, testlessthanorequal=true, testgreaterthan=false, testgreaterthanorequal=true, testand=false, testor=true, testnot=false, testadd=2.0, testaddstring=1@1, testsubtraction=0.0, testmultiplication=1.0, testdivision=5.0, testmodulus=0.0, testexponentialpower=4.0]
请参阅在xml文件定义bean的等效版本。在xml中,类似 < 小于符号不支持,应该使用下面所示文本形式,例如文本等值, ('<' = 'lt') 和 ('<=' = 'le').
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">
<bean id="customerbean" class="com.h3.core.customer">
<property name="testequal" value="#{1 == 1}" />
<property name="testnotequal" value="#{1 != 1}" />
<property name="testlessthan" value="#{1 lt 1}" />
<property name="testlessthanorequal" value="#{1 le 1}" />
<property name="testgreaterthan" value="#{1 > 1}" />
<property name="testgreaterthanorequal" value="#{1 >= 1}" />
<property name="testand" value="#{numberbean.no == 999 and numberbean.no lt 900}" />
<property name="testor" value="#{numberbean.no == 999 or numberbean.no lt 900}" />
<property name="testnot" value="#{!(numberbean.no == 999)}" />
<property name="testadd" value="#{1 + 1}" />
<property name="testaddstring" value="#{'1' + '@' + '1'}" />
<property name="testsubtraction" value="#{1 - 1}" />
<property name="testmultiplication" value="#{1 * 1}" />
<property name="testdivision" value="#{10 / 2}" />
<property name="testmodulus" value="#{10 % 10}" />
<property name="testexponentialpower" value="#{2 ^ 2}" />
</bean>
<bean id="numberbean" class="com.h3.core.number">
<property name="no" value="999" />
</bean>
</beans>