spring 第6天SpEL,P命名空间,Util Schema

[b]使用p名称空间配置属性[/b]
使用p命名空间的功能 和 <property>

<bean id="p_chinese" class="cn.sh.springmvc_java.Chinese" p:name="admin" p:axe-ref="stoneAxe"/>

<bean id="u_chinese" class="cn.sh.springmvc.model.Chinese"
p:age-ref="chin.age" p:axe-ref="stoneAxe"
p:schools-ref="chin.schools"
p:axes-ref="chin.axes"
p:scores-ref="chin.scores"/>
<!--如果某个bean的属性名以"-ref"结尾,这样p命名空间会出现冲突--->


[b]使用util Schema[/b]


<!-- 将指定类的静态Field暴露出来 -->
<util:constant id="chin.age" static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>

<!-- 将指定bean的属性 暴露出来 -->
<util:property-path id="test" path="u_chinese.age"/>

<!-- 加载指定资源文件 -->
<util:properties id="confTest" location="classpath:message_zh_CN.properties"/>

<!-- 定义一个list -->
<util:list id="chin.schools" list-class="java.util.LinkedList">
<value>小学</value>
<value>中学</value>
<value>大学</value>
</util:list>

<!-- 定义一个set对象 -->
<util:set id="chin.axes" set-class="java.util.HashSet">
<value>字符串斧子</value>
<bean class="cn.sh.springmvc.model.SteelAxe"/>
<ref local="stoneAxe"/>
</util:set>

<!-- 定一个 map对象 -->
<util:map id="chin.scores" map-class="java.util.TreeMap" key-type="java.lang.String" value-type="java.lang.Double">
<entry key="数学" value="89"/>
<entry key="英语" value="89"/>
<entry key="语文" value="89"/>
</util:map>

[b]spring表达式 简称 SpEL[/b]
先把 表达式在bean中的使用给出来

<bean id="spel_chinese" class="cn.sh.springmvc.model.Chinese"
p:name="#{T(java.lang.Math).random()}"
p:age="#{new java.util.Random().nextInt(100)}"
p:axe="#{new cn.sh.springmvc.model.SteelAxe('made in china')}">
<property name="books">
<list>
<value>#{confTest.hello}</value>
<value>#{confTest.now}</value>
</list>
</property>
</bean>





//测试 SpEL 表达式语言
@Test
public void test2() {
ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("'Hello World'");
System.out.println("Hello World的结果"+exp.getValue());

exp=parser.parseExpression("'Hello World'.concat('!')");
System.out.println("concat后的结果"+exp.getValue());

exp=parser.parseExpression("'Hello World'.bytes");
System.out.println("调用getBytes方法后的结果"+exp.getValue());

exp=parser.parseExpression("'Hello World'.bytes.length");
System.out.println("方法返回值后的属性的结果"+exp.getValue());

exp=parser.parseExpression("new String('Hello World').toUpperCase()");
System.out.println("upperCase后的结果:"+exp.getValue(String.class));


exp=parser.parseExpression("age");
cn.sh.springmvc.model.Chinese c=act.getBean("u_chinese",cn.sh.springmvc.model.Chinese.class);
System.out.println("以Chinese为root,age的表达式的值是:"+exp.getValue(c, Integer.class));


exp=parser.parseExpression("age==15");
StandardEvaluationContext ctx=new StandardEvaluationContext();
ctx.setRootObject(c);
System.out.println(exp.getValue(ctx, Boolean.class));

List<Boolean> list=new ArrayList<Boolean>();
list.add(true);
EvaluationContext ctx2=new StandardEvaluationContext();
ctx2.setVariable("list",list);

parser.parseExpression("#list[0]").setValue(ctx2, "false");
System.out.println("List集合中的第一个元素:"+list.get(0));

cn.sh.springmvc.model.Chinese c1=act.getBean("spel_chinese",cn.sh.springmvc.model.Chinese.class);
c1.test();
System.out.println(c1.getAxe().chop());


}
@Test
public void test3() {
//直接变量表达式
ExpressionParser parser=new SpelExpressionParser();
String helloword=parser.parseExpression("'Hello World'").getValue(String.class);
double num=parser.parseExpression("0.23").getValue(Double.class);

//表达式中创建数组
Expression exp=parser.parseExpression("new String[]{'java','struts','spring'}");
System.out.println(exp.getValue());

exp=parser.parseExpression("new int[2][4]");
System.out.println(exp.getValue());

//创建list
exp=parser.parseExpression("{'java','struts','spring'}");
System.out.println(exp.getValue());

//创建 二维 list
exp=parser.parseExpression("{{'疯狂java讲义',''},{'左转','战国策'}}");
System.out.println(exp.getValue());


}

@Test
public void test4() {
//访问 list,Map
List<String> list=new ArrayList<String>();
list.add("Java");
list.add("Spring");
Map<String,Double> map=new HashMap<String,Double>();
map.put("Java", 80.0);
map.put("Spring",89.0);
ExpressionParser parser=new SpelExpressionParser();
EvaluationContext ctx=new StandardEvaluationContext();
ctx.setVariable("list", list);
ctx.setVariable("map", map);
System.out.println(parser.parseExpression("#list[0]").getValue(ctx));
System.out.println(parser.parseExpression("#map['java']").getValue(ctx));
}

@Test
public void test5() {
ExpressionParser parser=new SpelExpressionParser();
EvaluationContext ctx=new StandardEvaluationContext();
System.out.println(parser.parseExpression("'helloworld'.substring(2,5)").getValue());

List<String> list=new ArrayList<String>();
list.add("Java");
list.add("Spring");
list.add("javascript");
list.add("ios");
list.add("hibernate");
list.add("android");

ctx.setVariable("myList", list);
System.out.println(parser.parseExpression("#myList.subList(1,3)").getValue(ctx));
}

//运算
@Test
public void test6() {
List<String> list=new ArrayList<String>();
list.add("Java");
list.add("Spring");
list.add("javascript");
list.add("ios");
list.add("hibernate");
list.add("android");


ExpressionParser parser=new SpelExpressionParser();
EvaluationContext ctx=new StandardEvaluationContext();
ctx.setVariable("myList", list);

parser.parseExpression("#myList[0]='疯狂java讲义'").getValue(ctx);
System.out.println(list.get(0));

System.out.println(parser.parseExpression("#myList.size() > 3 ? '长度大于3':'长度小于3'").getValue());

}
//类型运算符 T() 如果类型 在 java.lang包下,T()可以省略 其他包不行
@Test
public void test7() {
ExpressionParser parser=new SpelExpressionParser();
System.out.println(parser.parseExpression("T(java.lang.Math).random()").getValue());
System.out.println(parser.parseExpression("T(System).getProperty('os.name')").getValue());
}

//使用 new 创建对象
@Test
public void test8() {
ExpressionParser parser=new SpelExpressionParser();

System.out.println(parser.parseExpression("new String('Helloworld').substring(2,4)").getValue());

System.out.println(parser.parseExpression("new javax.swing.JFrame('测试').setVisible('true')").getValue());

//三目运算的使用
//下面两个 相等
System.out.println(parser.parseExpression("1 !=null ? 1:2").getValue());
System.out.println(parser.parseExpression("1?:2").getValue());

}

//安全导航 ?
@Test
public void test9() {
ExpressionParser parser=new SpelExpressionParser();

//这里没有采用 安全导航 会出现 NullPointerException
System.out.println(parser.parseExpression("#foo.bar").getValue());

//这里采用了,不会出现 NullPointerException
System.out.println(parser.parseExpression("#foo?.bar").getValue());
}

//集合中元素 选择
@Test
public void test10() {
List<String> list=new ArrayList<String>();
list.add("Java");
list.add("Spring");
list.add("javascript");
list.add("ios");
list.add("hibernate");
list.add("android");


ExpressionParser parser=new SpelExpressionParser();
EvaluationContext ctx=new StandardEvaluationContext();
ctx.setVariable("myList", list);

//集合中元素的值的长度大于 7 就寻找出来
System.out.println(parser.parseExpression("#myList.?[length()>7]")
.getValue(ctx));


Map<String,Double> map=new HashMap<String,Double>();
map.put("Java", 89.9);
map.put("hibernate", 60.9);
map.put("javascript", 79.9);
map.put("Spring", 50.9);
ctx.setVariable("myMap", map);

//map中元素的值 大于 80才被选择出来
System.out.println(parser.parseExpression("#myMap.?[value>80]").getValue(ctx));


}

//集合投影 便利集合中的元素,然后进行指定的操作
@Test
public void test11() {
List<String> list=new ArrayList<String>();
list.add("Java");
list.add("Spring");
list.add("javascript");
list.add("ios");
list.add("hibernate");
list.add("android");


ExpressionParser parser=new SpelExpressionParser();
EvaluationContext ctx=new StandardEvaluationContext();
ctx.setVariable("myList", list);

//得到的是 原集合中 元素的长度
System.out.println(parser.parseExpression("#myList.![length()]").getValue(ctx));

List<Chinese> list1=new ArrayList<Chinese>();
list1.add(new Chinese("java"));
list1.add(new Chinese("Spring"));
list1.add(new Chinese("javascript"));
list1.add(new Chinese("ios"));
list1.add(new Chinese("hibernate"));
list1.add(new Chinese("android"));

ctx.setVariable("myList1", list1);

//获取的是原集合中元素的name属性
System.out.println(parser.parseExpression("#myList1.![name]").getValue(ctx));
}


//表达式模版
@Test
public void test12() {
ExpressionParser parser=new SpelExpressionParser();
List<Chinese> list1=new ArrayList<Chinese>();
list1.add(new Chinese("java"));
list1.add(new Chinese("Spring"));
Expression exp=parser.parseExpression("我的名字是#{name}",new TemplateParserContext());
System.out.println(exp.getValue(list1.get(0)));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值