当前位置: 首页>>代码示例>>Java>>正文


Java SpelExpressionParser.parseRaw方法代码示例

本文整理汇总了Java中org.springframework.expression.spel.standard.SpelExpressionParser.parseRaw方法的典型用法代码示例。如果您正苦于以下问题:Java SpelExpressionParser.parseRaw方法的具体用法?Java SpelExpressionParser.parseRaw怎么用?Java SpelExpressionParser.parseRaw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.expression.spel.standard.SpelExpressionParser的用法示例。


在下文中一共展示了SpelExpressionParser.parseRaw方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testScenario01_Roles

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void testScenario01_Roles() throws Exception {
	try {
		SpelExpressionParser parser = new SpelExpressionParser();
		StandardEvaluationContext ctx = new StandardEvaluationContext();
		Expression expr = parser.parseRaw("hasAnyRole('MANAGER','TELLER')");

		ctx.setRootObject(new Person("Ben"));
		Boolean value = expr.getValue(ctx,Boolean.class);
		assertFalse(value);

		ctx.setRootObject(new Manager("Luke"));
		value = expr.getValue(ctx,Boolean.class);
		assertTrue(value);

	} catch (EvaluationException ee) {
		ee.printStackTrace();
		fail("Unexpected SpelException: " + ee.getMessage());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:ScenariosForSpringSecurity.java

示例2: testScenario03_Arithmetic

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void testScenario03_Arithmetic() throws Exception {
	SpelExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// Might be better with a as a variable although it would work as a property too...
	// Variable references using a '#'
	Expression expr = parser.parseRaw("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");

	Boolean value = null;

	ctx.setVariable("a",1.0d); // referenced as #a in the expression
	ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it
	value = expr.getValue(ctx,Boolean.class);
	assertTrue(value);

	ctx.setRootObject(new Manager("Luke"));
	ctx.setVariable("a",1.043d);
	value = expr.getValue(ctx,Boolean.class);
	assertFalse(value);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:22,代码来源:ScenariosForSpringSecurity.java

示例3: testScenario04_ControllingWhichMethodsRun

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
	public void testScenario04_ControllingWhichMethodsRun() throws Exception {
		SpelExpressionParser parser = new SpelExpressionParser();
		StandardEvaluationContext ctx = new StandardEvaluationContext();

		ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);

		ctx.addMethodResolver(new MyMethodResolver()); // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
		// Might be better with a as a variable although it would work as a property too...
		// Variable references using a '#'
//		SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
		Expression expr = parser.parseRaw("(hasRole(3) or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");

		Boolean value = null;

		ctx.setVariable("a",1.0d); // referenced as #a in the expression
		value = expr.getValue(ctx,Boolean.class);
		assertTrue(value);

//			ctx.setRootObject(new Manager("Luke"));
//			ctx.setVariable("a",1.043d);
//			value = (Boolean)expr.getValue(ctx,Boolean.class);
//			assertFalse(value);
	}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:25,代码来源:ScenariosForSpringSecurity.java

示例4: testScenario_UsingStandardInfrastructure

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
/**
 * Scenario: using the standard infrastructure and running simple expression evaluation.
 */
@Test
public void testScenario_UsingStandardInfrastructure() {
	try {
		// Create a parser
		SpelExpressionParser parser = new SpelExpressionParser();
		// Parse an expression
		Expression expr = parser.parseRaw("new String('hello world')");
		// Evaluate it using a 'standard' context
		Object value = expr.getValue();
		// They are reusable
		value = expr.getValue();

		assertEquals("hello world", value);
		assertEquals(String.class, value.getClass());
	} catch (EvaluationException ee) {
		ee.printStackTrace();
		fail("Unexpected Exception: " + ee.getMessage());
	} catch (ParseException pe) {
		pe.printStackTrace();
		fail("Unexpected Exception: " + pe.getMessage());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:ExpressionLanguageScenarioTests.java

示例5: testScenario_DefiningVariablesThatWillBeAccessibleInExpressions

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
/**
 * Scenario: using the standard context but adding your own variables
 */
@Test
public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception {
	// Create a parser
	SpelExpressionParser parser = new SpelExpressionParser();
	// Use the standard evaluation context
	StandardEvaluationContext ctx = new StandardEvaluationContext();
	ctx.setVariable("favouriteColour","blue");
	List<Integer> primes = new ArrayList<Integer>();
	primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
	ctx.setVariable("primes",primes);

	Expression expr = parser.parseRaw("#favouriteColour");
	Object value = expr.getValue(ctx);
	assertEquals("blue", value);

	expr = parser.parseRaw("#primes.get(1)");
	value = expr.getValue(ctx);
	assertEquals(3, value);

	// all prime numbers > 10 from the list (using selection ?{...})
	expr = parser.parseRaw("#primes.?[#this>10]");
	value = expr.getValue(ctx);
	assertEquals("[11, 13, 17]", value.toString());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:28,代码来源:ExpressionLanguageScenarioTests.java

示例6: testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
/**
 * Scenario: using your own java methods and calling them from the expression
 */
@Test
public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException {
	try {
		// Create a parser
		SpelExpressionParser parser = new SpelExpressionParser();
		// Use the standard evaluation context
		StandardEvaluationContext ctx = new StandardEvaluationContext();
		ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class));

		Expression expr = parser.parseRaw("#repeat('hello')");
		Object value = expr.getValue(ctx);
		assertEquals("hellohello", value);

	} catch (EvaluationException ee) {
		ee.printStackTrace();
		fail("Unexpected Exception: " + ee.getMessage());
	} catch (ParseException pe) {
		pe.printStackTrace();
		fail("Unexpected Exception: " + pe.getMessage());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:25,代码来源:ExpressionLanguageScenarioTests.java

示例7: testScenario_AddingYourOwnPropertyResolvers_1

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
/**
 * Scenario: add a property resolver that will get called in the resolver chain, this one only supports reading.
 */
@Test
public void testScenario_AddingYourOwnPropertyResolvers_1() throws Exception {
	// Create a parser
	SpelExpressionParser parser = new SpelExpressionParser();
	// Use the standard evaluation context
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	ctx.addPropertyAccessor(new FruitColourAccessor());
	Expression expr = parser.parseRaw("orange");
	Object value = expr.getValue(ctx);
	assertEquals(Color.orange, value);

	try {
		expr.setValue(ctx, Color.blue);
		fail("Should not be allowed to set oranges to be blue !");
	} catch (SpelEvaluationException ee) {
		assertEquals(ee.getMessageCode(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:ExpressionLanguageScenarioTests.java

示例8: testScenario_AddingYourOwnPropertyResolvers_2

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void testScenario_AddingYourOwnPropertyResolvers_2() throws Exception {
	// Create a parser
	SpelExpressionParser parser = new SpelExpressionParser();
	// Use the standard evaluation context
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	ctx.addPropertyAccessor(new VegetableColourAccessor());
	Expression expr = parser.parseRaw("pea");
	Object value = expr.getValue(ctx);
	assertEquals(Color.green, value);

	try {
		expr.setValue(ctx, Color.blue);
		fail("Should not be allowed to set peas to be blue !");
	}
	catch (SpelEvaluationException ee) {
		assertEquals(ee.getMessageCode(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:ExpressionLanguageScenarioTests.java

示例9: testAddingSpecificPropertyAccessor

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
// Adding a new property accessor just for a particular type
public void testAddingSpecificPropertyAccessor() throws Exception {
	SpelExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// Even though this property accessor is added after the reflection one, it specifically
	// names the String class as the type it is interested in so is chosen in preference to
	// any 'default' ones
	ctx.addPropertyAccessor(new StringyPropertyAccessor());
	Expression expr = parser.parseRaw("new String('hello').flibbles");
	Integer i = expr.getValue(ctx, Integer.class);
	assertEquals((int) i, 7);

	// The reflection one will be used for other properties...
	expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
	Object o = expr.getValue(ctx);
	assertNotNull(o);

	expr = parser.parseRaw("new String('hello').flibbles");
	expr.setValue(ctx, 99);
	i = expr.getValue(ctx, Integer.class);
	assertEquals((int) i, 99);

	// Cannot set it to a string value
	try {
		expr.setValue(ctx, "not allowed");
		fail("Should not have been allowed");
	} catch (EvaluationException e) {
		// success - message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
		// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
		// System.out.println(e.getMessage());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:35,代码来源:PropertyAccessTests.java

示例10: testScenario02_ComparingNames

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void testScenario02_ComparingNames() throws Exception {
	SpelExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	ctx.addPropertyAccessor(new SecurityPrincipalAccessor());

	// Multiple options for supporting this expression: "p.name == principal.name"
	// (1) If the right person is the root context object then "name==principal.name" is good enough
	Expression expr = parser.parseRaw("name == principal.name");

	ctx.setRootObject(new Person("Andy"));
	Boolean value = expr.getValue(ctx,Boolean.class);
	assertTrue(value);

	ctx.setRootObject(new Person("Christian"));
	value = expr.getValue(ctx,Boolean.class);
	assertFalse(value);

	// (2) Or register an accessor that can understand 'p' and return the right person
	expr = parser.parseRaw("p.name == principal.name");

	PersonAccessor pAccessor = new PersonAccessor();
	ctx.addPropertyAccessor(pAccessor);
	ctx.setRootObject(null);

	pAccessor.setPerson(new Person("Andy"));
	value = expr.getValue(ctx,Boolean.class);
	assertTrue(value);

	pAccessor.setPerson(new Person("Christian"));
	value = expr.getValue(ctx,Boolean.class);
	assertFalse(value);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:35,代码来源:ScenariosForSpringSecurity.java

示例11: projectionTypeDescriptors_1

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void projectionTypeDescriptors_1() throws Exception {
	StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
	SpelExpressionParser parser = new SpelExpressionParser();
	String el1 = "ls.![#this.equals('abc')]";
	SpelExpression exp = parser.parseRaw(el1);
	List<?> value = (List<?>) exp.getValue(ctx);
	// value is list containing [true,false]
	assertEquals(Boolean.class, value.get(0).getClass());
	TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
	assertEquals(null, evaluated.getElementTypeDescriptor());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:13,代码来源:SpelReproTests.java

示例12: projectionTypeDescriptors_2

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void projectionTypeDescriptors_2() throws Exception {
	StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
	SpelExpressionParser parser = new SpelExpressionParser();
	String el1 = "as.![#this.equals('abc')]";
	SpelExpression exp = parser.parseRaw(el1);
	Object[] value = (Object[]) exp.getValue(ctx);
	// value is array containing [true,false]
	assertEquals(Boolean.class, value[0].getClass());
	TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
	assertEquals(Boolean.class, evaluated.getElementTypeDescriptor().getType());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:13,代码来源:SpelReproTests.java

示例13: projectionTypeDescriptors_3

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void projectionTypeDescriptors_3() throws Exception {
	StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
	SpelExpressionParser parser = new SpelExpressionParser();
	String el1 = "ms.![key.equals('abc')]";
	SpelExpression exp = parser.parseRaw(el1);
	List<?> value = (List<?>) exp.getValue(ctx);
	// value is list containing [true,false]
	assertEquals(Boolean.class, value.get(0).getClass());
	TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
	assertEquals(null, evaluated.getElementTypeDescriptor());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:13,代码来源:SpelReproTests.java

示例14: greaterThanWithNulls_SPR7840

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void greaterThanWithNulls_SPR7840() throws Exception {
	List<D> list = new ArrayList<D>();
	list.add(new D("aaa"));
	list.add(new D("bbb"));
	list.add(new D(null));
	list.add(new D("ccc"));
	list.add(new D(null));
	list.add(new D("zzz"));

	StandardEvaluationContext ctx = new StandardEvaluationContext(list);
	SpelExpressionParser parser = new SpelExpressionParser();

	String el1 = "#root.?[a < 'hhh']";
	SpelExpression exp = parser.parseRaw(el1);
	Object value = exp.getValue(ctx);
	assertEquals("[D(aaa), D(bbb), D(null), D(ccc), D(null)]", value.toString());

	String el2 = "#root.?[a > 'hhh']";
	SpelExpression exp2 = parser.parseRaw(el2);
	Object value2 = exp2.getValue(ctx);
	assertEquals("[D(zzz)]", value2.toString());

	// trim out the nulls first
	String el3 = "#root.?[a!=null].?[a < 'hhh']";
	SpelExpression exp3 = parser.parseRaw(el3);
	Object value3 = exp3.getValue(ctx);
	assertEquals("[D(aaa), D(bbb), D(ccc)]", value3.toString());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:30,代码来源:SpelReproTests.java

示例15: reservedWordProperties_9862

import org.springframework.expression.spel.standard.SpelExpressionParser; //导入方法依赖的package包/类
@Test
public void reservedWordProperties_9862() throws Exception {
	StandardEvaluationContext ctx = new StandardEvaluationContext();
	SpelExpressionParser parser = new SpelExpressionParser();
	SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
	Object value = expression.getValue(ctx);
	assertEquals(value, Reserver.CONST);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:9,代码来源:SpelReproTests.java


注:本文中的org.springframework.expression.spel.standard.SpelExpressionParser.parseRaw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。