如何在spring EL中将null作为可选值传递?

Posted

技术标签:

【中文标题】如何在spring EL中将null作为可选值传递?【英文标题】:how to pass null as optional value in spring EL? 【发布时间】:2015-12-13 13:38:28 【问题描述】:

正如post 中所建议的那样,我正在尝试从属性文件中读取strings 的列表。

@Value("#'$app.messages'.split(',')")
    private List<String> messages;

我收到以下错误:

Error creating bean with name 'MyController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.app.controller.HealthManager com.app.MyController.Manager; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.app.Manager.messages; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'health.messages' in string value "#'$health.messages'.split(',')"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1113)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1671)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我也尝试了以下两种方式。我仍然得到错误。

@Value("#'$app.messages'.split(','): null")
    private List<String> messages;


@Value("#'$app.messages'.split(','): #null")
    private List<String> messages;

我假设传递 null 可能会解决此问题,因为 app.messages 在我的情况下是完全可选的。

【问题讨论】:

【参考方案1】:

试试

@Value("#'$app.messages:'.length() &gt; 0 ? '$app.messages:'.split(',') : null")

末尾的: 表示如果找不到该属性,则使用零长度字符串。

【讨论】:

以上是关于如何在spring EL中将null作为可选值传递?的主要内容,如果未能解决你的问题,请参考以下文章

MVVM 并将可选值传递给 viewModel

向后传递数据时,在展开可选值时意外发现 nil

将展开的值传递给 Text() 成功,但传递给 Link() 会导致“在展开可选值时意外发现 nil”

在读取 JSON 时打开可选值时意外发现 nil [关闭]

Swift初见Swift可选值Optionals

prepareForSegue 出错“”在展开可选值时意外发现 nil“”