Spring Class类型表达式

版权声明:转载请随意! https://blog.csdn.net/qq_41723615/article/details/89183929

在之前所见到的都是最基础的表达式,与其程序的结构是相同的,但是在Spring里面对于Class反射机制也有自己的表达式处理。

No 表达式 范例 计算结果
1 Class Expression exp = parpser.parseExpression("T(String)"); Class<String>
Expression exp = parpser.parseExpression("T(java.util.Date)"); Class<java.util.Date>
2 静态属性 Expression exp = parpser.parseExpression("T(Integer).MAX_VALUE"); 2147483647
3 静态方法 Expression exp = parpser.parseExpression("T(Integer).parseInt(123)"); 123
4. 实例化对象 Expression exp = parpser.parseExpression("new java.util.Date()"); Wed Apr 10 15:33:53 CST 2019
5 实例化并调用有参构造 Expression exp = parpser.parseExpression("new String('hello')"); hello
6 instanceof Expression exp = parpser.parseExpression("'hello' instanceof T(String)"); true

                     使用T(类)的形式可以取得一个指定泛型类型的Class对象。

1.范例:取得Class类型的对象

public class TestELSimple {

	 public static void main(String[] args) {
            ExpressionParser parpser = new SpelExpressionParser();
            Expression exp = parpser.parseExpression("T(String)");
            EvaluationContext context = new StandardEvaluationContext();
            Class<Date> clsClass = exp.getValue(context , Class.class);
            System.out.println(clsClass);
	 }
}

如果调用的静态属性则使用"T(类型).静态属性名称" 。

2.范例:调用Integer类中的MAX_VALUE属性

public class TestELSimple {

	 public static void main(String[] args) {
        ExpressionParser parpser = new SpelExpressionParser();
        Expression exp = parpser.parseExpression("T(Integer).MAX_VALUE");
        EvaluationContext context = new StandardEvaluationContext();
        System.out.println(exp.getValue(context));
	 }
}

既然静态属性可以调用了也可以调用静态方法,例如Integer类里面存在有一个parsInt()的static方法,此方法可以接收一个字符串同时返回一个int类型的数据。

3.范例:调用Integer.parseInt()方法

public class TestELSimple {

	 public static void main(String[] args) {
        ExpressionParser parpser = new SpelExpressionParser();
        Expression exp = parpser.parseExpression("T(Integer).parseInt(123)");
        EvaluationContext context = new StandardEvaluationContext();
        System.out.println(exp.getValue());
	 }
}

虽然给出了静态操作.但是严格来讲使用最多的情况一定是类产生实例化对象,那么依然可以使用同样的方式完成,可以直接使用"new 类型()"的方式来实例化对象

4.范例:实例化Date类对象

public class TestELSimple {

	 public static void main(String[] args) {
            ExpressionParser parpser = new SpelExpressionParser();
            Expression exp = parpser.parseExpression("new java.util.Date()");
            EvaluationContext context = new StandardEvaluationContext();
            System.out.println(exp.getValue());
	 }
}

但是现在所调用的只是无参构造,如果有需要也可以调用有参构造

5.范例:调用有参构造

public class TestELSimple {

	 public static void main(String[] args) {
	        ExpressionParser parpser = new SpelExpressionParser();
	        Expression exp = parpser.parseExpression("new String('hello')");
	        EvaluationContext context = new StandardEvaluationContext();
	        System.out.println(exp.getValue());
	 }
}

在对象的开发过程之中,也可以进行实例化对象类型的判断

6.范例:判断一个字符串是否是String的实例化

public class TestELSimple {

	 public static void main(String[] args) {
	        ExpressionParser parpser = new SpelExpressionParser();
	        Expression exp = parpser.parseExpression("'hello' instanceof T(String)");
	        EvaluationContext context = new StandardEvaluationContext();
	        System.out.println(exp.getValue());
	 }
}

利用字符串完整的实现了反射机制的各种操作,它的操作不能能很智能化,只能够处理很简单的功能。

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/89183929