當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。