@value("#{'100' matches '\\d+' }") private boolean isdigit;
package com.h3.core; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.component; @component("customerbean") public class customer { // email regular expression string emailregex = "^[_a-za-z0-9-]+(\\.[_a-za-z0-9-]+)" + "*@[a-za-z0-9]+(\\.[a-za-z0-9]+)*(\\.[a-za-z]{2,})$"; // if this is a digit? @value("#{'100' matches '\\d+' }") private boolean validdigit; // if this is a digit + ternary operator @value("#{ ('100' matches '\\d+') == true ? " + "'yes this is digit' : 'no this is not a digit' }") private string msg; // if this emailbean.emailaddress contains a valid email address? @value("#{emailbean.emailaddress matches customerbean.emailregex}") private boolean validemail; //getter and setter methods, and constructor }
package com.h3.core; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.component; @component("emailbean") public class email { @value("admin@h3.com") string emailaddress; //... }
输出
customer [isdigit=true, msg=yes this is digit, isvalidemail=true]
<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="validdigit" value="#{'100' matches '\d+' }" /> <property name="msg" value="#{ ('100' matches '\d+') == true ? 'yes this is digit' : 'no this is not a digit' }" /> <property name="validemail" value="#{emailbean.emailaddress matches '^[_a-za-z0-9-]+(\.[_a-za-z0-9-]+)*@[a-za-z0-9]+(\.[a-za-z0-9]+)*(\.[a-za-z]{2,})$' }" /> </bean> <bean id="emailbean" class="com.h3.core.email"> <property name="emailaddress" value="admin@h3.com" /> </bean> </beans>