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


Java StandardEvaluationContext.addPropertyAccessor方法代码示例

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


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

示例1: testAddingRemovingAccessors

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void testAddingRemovingAccessors() {
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// reflective property accessor is the only one by default
	List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
	assertEquals(1,propertyAccessors.size());

	StringyPropertyAccessor spa = new StringyPropertyAccessor();
	ctx.addPropertyAccessor(spa);
	assertEquals(2,ctx.getPropertyAccessors().size());

	List<PropertyAccessor> copy = new ArrayList<PropertyAccessor>();
	copy.addAll(ctx.getPropertyAccessors());
	assertTrue(ctx.removePropertyAccessor(spa));
	assertFalse(ctx.removePropertyAccessor(spa));
	assertEquals(1,ctx.getPropertyAccessors().size());

	ctx.setPropertyAccessors(copy);
	assertEquals(2,ctx.getPropertyAccessors().size());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:22,代码来源:PropertyAccessTests.java

示例2: indexIntoGenericPropertyContainingMapObject

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void indexIntoGenericPropertyContainingMapObject() {
	Map<String, Map<String, String>> property = new HashMap<String, Map<String, String>>();
	Map<String, String> map =  new HashMap<String, String>();
	map.put("foo", "bar");
	property.put("property", map);
	SpelExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new MapAccessor());
	context.setRootObject(property);
	Expression expression = parser.parseExpression("property");
	assertEquals("java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(context).toString());
	assertEquals(map, expression.getValue(context));
	assertEquals(map, expression.getValue(context, Map.class));
	expression = parser.parseExpression("property['foo']");
	assertEquals("bar", expression.getValue(context));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:IndexingTests.java

示例3: propertyAccessorOrder_8211

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
/**
 * We add property accessors in the order:
 * First, Second, Third, Fourth.
 * They are not utilized in this order; preventing a priority or order of operations
 * in evaluation of SPEL expressions for a given context.
 */
@Test
public void propertyAccessorOrder_8211() {
	ExpressionParser expressionParser = new SpelExpressionParser();
	StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new ContextObject());

	evaluationContext.addPropertyAccessor(new TestPropertyAccessor("firstContext"));
	evaluationContext.addPropertyAccessor(new TestPropertyAccessor("secondContext"));
	evaluationContext.addPropertyAccessor(new TestPropertyAccessor("thirdContext"));
	evaluationContext.addPropertyAccessor(new TestPropertyAccessor("fourthContext"));

	assertEquals("first", expressionParser.parseExpression("shouldBeFirst").getValue(evaluationContext));
	assertEquals("second", expressionParser.parseExpression("shouldBeSecond").getValue(evaluationContext));
	assertEquals("third", expressionParser.parseExpression("shouldBeThird").getValue(evaluationContext));
	assertEquals("fourth", expressionParser.parseExpression("shouldBeFourth").getValue(evaluationContext));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:22,代码来源:SpelReproTests.java

示例4: testScenario_AddingYourOwnPropertyResolvers_1

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的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

示例5: testScenario_AddingYourOwnPropertyResolvers_2

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的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

示例6: evaluateTemplate

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Override
public String evaluateTemplate(String expressionTemplate, Map<String, Object> evaluationContext) {
    Optional.ofNullable(expressionTemplate)
        .filter(template -> !template.isEmpty())
        .orElseThrow(() -> new IllegalArgumentException("Expression template cannot be null or empty"));

    Optional.ofNullable(evaluationContext)
        .orElseThrow(() -> new IllegalArgumentException("Evaluation context cannot be null"));

    Expression expression = new SpelExpressionParser().parseExpression(expressionTemplate
            ,new TemplateParserContext("@{","}"));

    StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    try {
        standardEvaluationContext.registerFunction("urlEncode",
                Functions.class.getDeclaredMethod("urlEncode", new Class[] {String.class}));
    } catch (NoSuchMethodException e) {
        throw new EvaluationException("Fail to register function to evaluation context", e);
    }
    standardEvaluationContext.addPropertyAccessor(new MapAccessor());
    standardEvaluationContext.setVariables(evaluationContext);

    return expression.getValue(standardEvaluationContext,String.class);
}
 
开发者ID:KonkerLabs,项目名称:konker-platform,代码行数:25,代码来源:ExpressionEvaluationServiceImpl.java

示例7: evaluateConditional

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Override
public boolean evaluateConditional(String conditionalExpression, Map<String, Object> evaluationContext) {
    Optional.ofNullable(conditionalExpression)
            .filter(template -> !template.isEmpty())
            .orElseThrow(() -> new IllegalArgumentException("Conditional expression cannot be null or empty"));

    Optional.ofNullable(evaluationContext)
            .orElseThrow(() -> new IllegalArgumentException("Evaluation context cannot be null"));

    Expression expression = new SpelExpressionParser().parseExpression(conditionalExpression);

    StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    standardEvaluationContext.addPropertyAccessor(new MapAccessor());
    standardEvaluationContext.setVariables(evaluationContext);

    return expression.getValue(standardEvaluationContext,Boolean.class);
}
 
开发者ID:KonkerLabs,项目名称:konker-platform,代码行数:18,代码来源:ExpressionEvaluationServiceImpl.java

示例8: mapAccessorCompilable

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void mapAccessorCompilable() {
	Map<String, Object> testMap = getSimpleTestMap();
	StandardEvaluationContext sec = new StandardEvaluationContext();		
	sec.addPropertyAccessor(new MapAccessor());
	SpelExpressionParser sep = new SpelExpressionParser();
	
	// basic
	Expression ex = sep.parseExpression("foo");
	assertEquals("bar",ex.getValue(sec,testMap));
	assertTrue(SpelCompiler.compile(ex));		
	assertEquals("bar",ex.getValue(sec,testMap));

	// compound expression
	ex = sep.parseExpression("foo.toUpperCase()");
	assertEquals("BAR",ex.getValue(sec,testMap));
	assertTrue(SpelCompiler.compile(ex));		
	assertEquals("BAR",ex.getValue(sec,testMap));
	
	// nested map
	Map<String,Map<String,Object>> nestedMap = getNestedTestMap();
	ex = sep.parseExpression("aaa.foo.toUpperCase()");
	assertEquals("BAR",ex.getValue(sec,nestedMap));
	assertTrue(SpelCompiler.compile(ex));		
	assertEquals("BAR",ex.getValue(sec,nestedMap));
	
	// avoiding inserting checkcast because first part of expression returns a Map
	ex = sep.parseExpression("getMap().foo");
	MapGetter mapGetter = new MapGetter();
	assertEquals("bar",ex.getValue(sec,mapGetter));
	assertTrue(SpelCompiler.compile(ex));		
	assertEquals("bar",ex.getValue(sec,mapGetter));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:34,代码来源:MapAccessorTests.java

示例9: createEvaluationContext

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:13,代码来源:EvalTag.java

示例10: testAddingSpecificPropertyAccessor

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的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

示例11: testCustomMapAccessor

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void testCustomMapAccessor() throws Exception {
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
	ctx.addPropertyAccessor(new MapAccessor());

	Expression expr = parser.parseExpression("testMap.monday");
	Object value = expr.getValue(ctx, String.class);
	assertEquals("montag", value);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:11,代码来源:MapAccessTests.java

示例12: create

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Override
public MailMessage create(MailTemplate mailTemplate, MailSource source) {
    MailMessageImpl.Builder b = MailMessageImpl.builder();
    final StandardEvaluationContext ctx = new StandardEvaluationContext(source.getVariables());
    ctx.addPropertyAccessor(new MapAccessor());
    UnaryOperator<Object> processor = (o) -> MailTemplateUtils.process((s) -> evaluate(ctx, s), o);
    b.setHead(MailHeadImpl.builder().from(mailTemplate.getHeadSource(), processor).build());
    MailPartTemplate bs = mailTemplate.getBodySource();
    String bodyText = (String) evaluate(ctx, bs.getData());
    b.setBody(new MailTextBody(bodyText, bs.getMime()));
    return b.build();
}
 
开发者ID:codeabovelab,项目名称:haven-platform,代码行数:13,代码来源:SpelMailTemplateEngine.java

示例13: SPR5804

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void SPR5804() throws Exception {
	Map<String, String> m = new HashMap<String, String>();
	m.put("foo", "bar");
	StandardEvaluationContext eContext = new StandardEvaluationContext(m); // root is a map instance
	eContext.addPropertyAccessor(new MapAccessor());
	Expression expr = new SpelExpressionParser().parseRaw("['foo']");
	assertEquals("bar", expr.getValue(eContext));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:10,代码来源:SpelReproTests.java

示例14: SPR5847

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
@Test
public void SPR5847() throws Exception {
	StandardEvaluationContext eContext = new StandardEvaluationContext(new TestProperties());
	String name = null;
	Expression expr = null;

	expr = new SpelExpressionParser().parseRaw("jdbcProperties['username']");
	name = expr.getValue(eContext, String.class);
	assertEquals("Dave", name);

	expr = new SpelExpressionParser().parseRaw("jdbcProperties[username]");
	name = expr.getValue(eContext, String.class);
	assertEquals("Dave", name);

	// MapAccessor required for this to work
	expr = new SpelExpressionParser().parseRaw("jdbcProperties.username");
	eContext.addPropertyAccessor(new MapAccessor());
	name = expr.getValue(eContext, String.class);
	assertEquals("Dave", name);

	// --- dotted property names

	// lookup foo on the root, then bar on that, then use that as the key into
	// jdbcProperties
	expr = new SpelExpressionParser().parseRaw("jdbcProperties[foo.bar]");
	eContext.addPropertyAccessor(new MapAccessor());
	name = expr.getValue(eContext, String.class);
	assertEquals("Dave2", name);

	// key is foo.bar
	expr = new SpelExpressionParser().parseRaw("jdbcProperties['foo.bar']");
	eContext.addPropertyAccessor(new MapAccessor());
	name = expr.getValue(eContext, String.class);
	assertEquals("Elephant", name);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:36,代码来源:SpelReproTests.java

示例15: dollarPrefixedIdentifier_SPR7100

import org.springframework.expression.spel.support.StandardEvaluationContext; //导入方法依赖的package包/类
/** $ related identifiers */
@Test
public void dollarPrefixedIdentifier_SPR7100() {
	Holder h = new Holder();
	StandardEvaluationContext eContext = new StandardEvaluationContext(h);
	eContext.addPropertyAccessor(new MapAccessor());
	h.map.put("$foo", "wibble");
	h.map.put("foo$bar", "wobble");
	h.map.put("foobar$$", "wabble");
	h.map.put("$", "wubble");
	h.map.put("$$", "webble");
	h.map.put("$_$", "tribble");
	String name = null;
	Expression expr = null;

	expr = new SpelExpressionParser().parseRaw("map.$foo");
	name = expr.getValue(eContext, String.class);
	assertEquals("wibble", name);

	expr = new SpelExpressionParser().parseRaw("map.foo$bar");
	name = expr.getValue(eContext, String.class);
	assertEquals("wobble", name);

	expr = new SpelExpressionParser().parseRaw("map.foobar$$");
	name = expr.getValue(eContext, String.class);
	assertEquals("wabble", name);

	expr = new SpelExpressionParser().parseRaw("map.$");
	name = expr.getValue(eContext, String.class);
	assertEquals("wubble", name);

	expr = new SpelExpressionParser().parseRaw("map.$$");
	name = expr.getValue(eContext, String.class);
	assertEquals("webble", name);

	expr = new SpelExpressionParser().parseRaw("map.$_$");
	name = expr.getValue(eContext, String.class);
	assertEquals("tribble", name);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:40,代码来源:SpelReproTests.java


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