标签: spring-el

在spring xml配置中使用应用程序常量的最佳方法是什么?

我想在spring xml配置中使用我的应用程序常量.

我知道用Spring SpEl这样做是这样的:

<bean class="example.SomeBean">
    <property name="anyProperty" value="#{ T(example.AppConfiguration).EXAMPLE_CONSTANT}" />
    <!-- Other config -->
</bean>
Run Code Online (Sandbox Code Playgroud)

那么,有更好的方法吗?

java spring constants spring-el

12
推荐指数
1
解决办法
1万
查看次数

在Spring EL中引用当前bean的属性

我想从单个类创建许多bean,所有bean都要在当前应用程序上下文中实例化,每个bean都基于属性文件中的前缀属性.我给出了一个我想要实现的例子.任何有关如何在没有过多代码的情况下执行此操作的提示(例如,没有多个类,复杂的工厂等)将不胜感激.

XML配置:

<bean id="bean1" class="Mybean">
    <property name="prefix" value="bean1"/>
</bean>

<bean id="bean2" class="Mybean">
    <property name="prefix" value="bean2"/>
</bean>

<bean id="bean3" class="Mybean">
    <property name="prefix" value="bean3"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

属性文件:

bean1.name=alfred
bean2.name=bobby
bean3.name=charlie
Run Code Online (Sandbox Code Playgroud)

类:

class Mybean {
    @Value("${#{prefix}.name}")
    String name;
}
Run Code Online (Sandbox Code Playgroud)

主类:

public class Main {
    @Autowired
    List<MyBean> mybeans;
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-el

12
推荐指数
1
解决办法
710
查看次数

使用spring3 @Value访问PropertyPlaceholderConfigurer值?

@Value当我的属性源是其子类时,我正在尝试使用Spring设置字符串的值PropertyPlaceholderConfigurer.有人知道怎么做吗 ?

spring spring-el

11
推荐指数
2
解决办法
1万
查看次数

使用Spring Expression Language以编程方式评估bean表达式

我有一个简单的Spring Bean Expression,当我在应用程序上下文文件中定义它时,它会很好地评估:

<bean id="myConfigBean" class="com.example.myBeanConfigBean">
    <property name="myProperty" value="#{ someOtherBean.getData() }"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

现在,我想以编程方式进行相同的评估.我使用了以下代码:

final ExpressionParser parser = new SpelExpressionParser();
final TemplateParserContext templateContext = new TemplateParserContext();
Expression expression = parser.parseExpression("#{ someOtherBean.getData() }", templateContext);
final String value = (String) expression.getValue();
Run Code Online (Sandbox Code Playgroud)

这引发了一个异常:

EL1007E:(pos 22): Field or property 'someOtherBean' cannot be found on null
Run Code Online (Sandbox Code Playgroud)

我想我必须以某种方式设置一个根对象,允许配置的bean像属性一样.但我还没有让它工作.任何人,已经做过这个并且可以给出一个提示吗?

java spring spring-el

11
推荐指数
1
解决办法
1万
查看次数

如何使用SimpleDateFormat模式替换String中的占位符

在像这样的给定字符串中

".../uploads/${customer}/${dateTime('yyyyMMdd')}/report.pdf"
Run Code Online (Sandbox Code Playgroud)

我需要替换一个customer和一个yyyyMMdd时间戳.

要替换customer占位符,我可以使用StrSubstitutorApache Commons.但如何更换SimpleDateFormat?我们在Spring环境中运行,所以也许Spring EL是一个选择?

占位符的标记不固定,如果另一个库需要语法更改,则可以.

这个小测试显示了这个问题:

SimpleDateFormat            formatter   = new SimpleDateFormat("yyyyMMdd");

String                      template    = ".../uploads/${customer}/${dateTime('yyyyMMdd')}/report.pdf";

@Test
public void shouldResolvePlaceholder()
{
    final Map<String, String> model = new HashMap<String, String>();
    model.put("customer", "Mr. Foobar");

    final String filledTemplate = StrSubstitutor.replace(this.template, model);

    assertEquals(".../uploads/Mr. Foobar/" + this.formatter.format(new Date()) + "/report.pdf", filledTemplate);
}
Run Code Online (Sandbox Code Playgroud)

java apache-commons spring-el

11
推荐指数
2
解决办法
4万
查看次数

如何使用SpEL处理Thymeleaf中的本地化消息

我是ThymeLeaf的初学者,并且除了@PreAuthorize注释之外没有太多使用SpEL ,所以请非常友好地帮助我.

我正在使用ThymeLeaf(版本2.1.2)和Spring(4.0.2.RELEASE),thymeleaf-spring4并且(据我所知)它用SpEL替换了默认的OGNL脚本.

我想要实现的只是本地化的字符串通过#strings.capitalize函数大写.这是我到目前为止尝试的内容:

<h1 th:text="#{retrievable.key}">Text to be replaced</h1>
Run Code Online (Sandbox Code Playgroud)

完美地工作并给出预期的结果.

现在,当我尝试这个:

<h1 th:text="${#strings.capitalize(#{retrievable.key})}">Text to be replaced</h1>
Run Code Online (Sandbox Code Playgroud)

我得到以下异常(根本原因,为清楚起见省略了):

org.springframework.expression.spel.SpelParseException:
EL1043E:(pos 21): Unexpected token.  Expected 'identifier' but was 'lcurly({)'
Run Code Online (Sandbox Code Playgroud)

好的.只是为了好玩,我省略了大括号并获得了我所期望的:它<h1>是空的.

所以现在我认为可能需要对消息的检索进行预处理,retrievable.key以便在评估时 #strings.capitalize对其进行评估.虽然这对我来说似乎违反直觉和不合逻辑,因为这会破坏所有编程规则,我尝试了这种方法.它也不起作用:使用

${#strings.capitalize(__#retrievable.key__)}
Run Code Online (Sandbox Code Playgroud)

导致

org.thymeleaf.exceptions.TemplateProcessingException:
Could not parse as expression: "#retrievable.key"
Run Code Online (Sandbox Code Playgroud)

和使用

${#strings.capitalize(__#{retrievable.key}__)}
Run Code Online (Sandbox Code Playgroud)

导致(你猜对了)<h1></h1>.

我知道实际问题可以通过CSS或JavaScript解决,但它不一定是关于大写或大写,而是处理本地化字符串,这是一个例子.

那我在这里错过了什么?

Thymeleaf论坛提供的解决方案

ThymeLeaf论坛的Zemi提供了以下优雅的解决方案:

<h1 th:text="${#strings.capitalize('__#{retrievable.key}__')}">Text to be replaced</h1>
Run Code Online (Sandbox Code Playgroud)

请注意单引号.预处理似乎真的意味着在Thymeleaf中进行预处理.

不过,我接受了第一个工作答案.

java spring localization spring-el thymeleaf

11
推荐指数
1
解决办法
4297
查看次数

如何在SpEL中转义值?

我正在用XML编写一些SpEL语句,我无法让解析器确定何时需要转义字符.

我尝试过以下方法:

<... property="someProperty" value="#{ someBean.aMethodOnTheBean('st\'ring') }" />
Run Code Online (Sandbox Code Playgroud)

但是添加\'似乎没有逃脱单引号,我一直收到解析器异常.

有没有办法逃避这些价值观?

java spring spring-el

10
推荐指数
1
解决办法
1万
查看次数

我在哪里可以找到Java EL 3.0的实现

目前正在设计Expression Language 3.0版.它添加了各种很酷的功能,例如通过lambda表达式映射和过滤集合.Spring EL有一个类似的功能,但不使用lambdas.

尽管谷歌搜索EL3的(参考)实现我找不到任何东西,是否有(参考)实现可用或至少正在进行?

否则我现在可能需要使用Spring EL.

谢谢

java collections lambda el spring-el

10
推荐指数
1
解决办法
4527
查看次数

如何在Spring Boot中的application.yml中定义默认空值

我正在尝试使用SpringBoot版本1.3.0.RELEASE在application.yml中将默认值定义为null值.目标是能够使用带ConfigurationProperties注释的类来引用它

-- application.yml --
test.foo: ${test.bar:#{null}}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

如果test.bar未定义值,则设置test.foo为null(默认值)

我的依赖项中已经有了spring-el.我不想用PropertyPlaceholderConfigurer.setNullValue

它似乎工作@Value但不在application.yml(参见http://farenda.com/spring/spring-inject-null-value/)

这是一个错误,或者yaml不是为此而设计的?我在http://yaml.org/type/null.html中尝试了所有值,但它也没有用

谢谢

null yaml default-value spring-el spring-boot

10
推荐指数
1
解决办法
6600
查看次数

EL1008E:在'java.util.HashMap'类型的对象上找不到属性或字段'timestamp' - 可能不公开?

当我使用Spring Boot的全局异常处理程序时,我得到了:

org.springframework.expression.spel.SpelEvaluationException:EL1008E:在'java.util.HashMap'类型的对象上找不到属性或字段'timestamp' - 可能不公开?

这是我的代码,我在我的项目中导入了Spring Security和Thymeleaf.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta charset="UTF-8" />
    <title>??????</title>
</head>
<body>
<h1> ???? </h1>
<div th:text="${url ?: '????URL'}"></div>
<div th:text="${exception == null ? '??????' : exception.message}"></div>
</body>
</html
Run Code Online (Sandbox Code Playgroud)
@GetMapping("/test")
public String test() throws Exception {
    throw new Exception("????");
}
Run Code Online (Sandbox Code Playgroud)
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final String DEFAULT_ERROR_VIEW = "/error";
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
        LOGGER.error("????", e);
        ModelAndView mav …
Run Code Online (Sandbox Code Playgroud)

java spring spring-el spring-boot

10
推荐指数
1
解决办法
2万
查看次数