java测试el表达式运算符_Spring4-EL表达式的运算符使用

1.创建Maven项目,项目名称springdemo46,如图所示

fd79328a68ef047ddc11a14bdbb54816.png

2.配置Maven,修改项目中的pom.xml文件,修改内容如下

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

1.0.0

shequ

springdemo13

0.0.1-SNAPSHOT

1.7

UTF-8

UTF-8

codelds

https://code.lds.org/nexus/content/groups/main-repo

javax.annotation

jsr250-api

1.0

org.springframework

spring-test

4.1.4.RELEASE

junit

junit

4.10

org.springframework

spring-core

4.1.4.RELEASE

org.springframework

spring-context

4.1.4.RELEASE

org.springframework

spring-jdbc

4.1.4.RELEASE

mysql

mysql-connector-java

5.1.34

3.在src/main/java下创建实体Bean Forum,包名(com.mycompany.shequ.bean)如图所示

b100a0c51ad6422bfb614180b1b45e01.png

4.实体Bean Forum的内容如下package com.mycompany.shequ.bean;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component("forumBean")

public class Forum {

private boolean equal;

private boolean notEqual;

private boolean lessThan;

private boolean lessThanOrEqual;

private boolean greaterThan;

private boolean greaterThanOrEqual;

private boolean and;

private boolean or;

private boolean not;

private double add;

private String addString;

private double subtraction;

private double multiplication;

private double division;

private double modulus ;

private double exponentialPower;

public boolean isEqual() {

return equal;

}

@Value("#{2 == 2}")

public void setEqual(boolean equal) {

this.equal = equal;

}

public boolean isNotEqual() {

return notEqual;

}

@Value("#{1 != 1}")

public void setNotEqual(boolean notEqual) {

this.notEqual = notEqual;

}

public boolean isLessThan() {

return lessThan;

}

@Value("#{1 

public void setLessThan(boolean lessThan) {

this.lessThan = lessThan;

}

public boolean isLessThanOrEqual() {

return lessThanOrEqual;

}

@Value("#{1 <= 1}")

public void setLessThanOrEqual(boolean lessThanOrEqual) {

this.lessThanOrEqual = lessThanOrEqual;

}

public boolean isGreaterThan() {

return greaterThan;

}

@Value("#{1 > 0}")

public void setGreaterThan(boolean greaterThan) {

this.greaterThan = greaterThan;

}

public boolean isGreaterThanOrEqual() {

return greaterThanOrEqual;

}

@Value("#{1 >= 1}")

public void setGreaterThanOrEqual(boolean greaterThanOrEqual) {

this.greaterThanOrEqual = greaterThanOrEqual;

}

public boolean isAnd() {

return and;

}

@Value("#{1 == 1 and 1 

public void setAnd(boolean and) {

this.and = and;

}

public boolean isOr() {

return or;

}

@Value("#{1 == 1 or 1 

public void setOr(boolean or) {

this.or = or;

}

public boolean isNot() {

return not;

}

@Value("#{!(1 == 999)}")

public void setNot(boolean not) {

this.not = not;

}

public double getAdd() {

return add;

}

@Value("#{1 + 1}")

public void setAdd(double add) {

this.add = add;

}

public String getAddString() {

return addString;

}

@Value("#{‘1‘ + ‘-‘ + ‘1‘}")

public void setAddString(String addString) {

this.addString = addString;

}

public double getSubtraction() {

return subtraction;

}

@Value("#{1 - 1}")

public void setSubtraction(double subtraction) {

this.subtraction = subtraction;

}

public double getMultiplication() {

return multiplication;

}

@Value("#{1 * 2}")

public void setMultiplication(double multiplication) {

this.multiplication = multiplication;

}

public double getDivision() {

return division;

}

@Value("#{10 / 2}")

public void setDivision(double division) {

this.division = division;

}

public double getModulus() {

return modulus;

}

@Value("#{10 % 2}")

public void setModulus(double modulus) {

this.modulus = modulus;

}

public double getExponentialPower() {

return exponentialPower;

}

@Value("#{2 ^ 3}")

public void setExponentialPower(double exponentialPower) {

this.exponentialPower = exponentialPower;

}

}

5.在src/main/resource下创建核心的配置文件applicationContext.xml,如图所示

f47c625334811e3e8009cf1207bbde19.png

6.核心的配置文件applicationContext.xml的内容如下

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-4.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd">

7.在src/test/java下创建测试文件AppTest,包名(com.mycompany.shequ.test)如图所示

50c0511ab105040fccd81d164cc575ef.png

8.测试文件AppTest的内容如下package com.mycompany.shequ.test;

import org.junit.Test;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mycompany.shequ.bean.Forum;

public class AppTest {

@Test

public void beanTest(){

ConfigurableApplicationContext context = new

ClassPathXmlApplicationContext("applicationContext.xml");

Forum forum = (Forum) context.getBean("forumBean");

System.out.println(forum.getAdd());

System.out.println(forum.getAddString());

//其他的值请自行输出查看

}

}

9.在测试类AppTest的beanTest方法上右键运行,输出结果如图所示

19a514d7e8d4e004a8a09f4cfdb22e7d.png

本文出自 “素颜” 博客,谢绝转载!

原文:http://suyanzhu.blog.51cto.com/8050189/1910265

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值