【问题标题】:ternary operator in SpEL Spring [duplicate]SpEL Spring中的三元运算符[重复]
【发布时间】:2020-06-23 11:09:13
【问题描述】:

我有以下表达:

 <bean class="java.net.InetAddress" id="inetAddress" />

<bean id="dataSource"
      class="org.apache.tomcat.jdbc.pool.DataSource"
      p:driverClassName="org.postgresql.Driver"
      p:url="#{'jdbc:postgresql://' + inetAddress.getLocalHost().getHostName()=='alex-HP-290-G1-SFF-Business-PC'?'localhost':'172.18.0.2' + ':5432/infostock'}"/>

这个表情

 inetAddress.getLocalHost().getHostName()=='alex-HP-290-G1-SFF-Business-PC'?'localhost':'172.18.0.2'

必须将主机名值(从 getHostName 检索)与“alex-HP-290-G1-SFF-Business-PC”进行比较。如果为 true,则返回 'localhost',否则返回 '172.18.0.2'。

主机名的实际值为“alex-HP-290-G1-SFF-Business-PC”。我从:

 System.out.println(InetAddress.getLocalHost().getHostName());

所以,三元运算符必须返回'localhost'

但它返回“172.18.0.2”。

【问题讨论】:

  • 这能回答你的问题吗? How do I compare strings in Java?
  • 我试过了 (inetAddress.getLocalHost().getHostName().equals('andrej-HP-290-G1-SFF-Business-PC') ? 'localhost' : '172.18.0.2 ') 但我在日志中看到:无效的布尔值 'jdbc:postgresql://true'

标签: java spring hostname conditional-operator spring-el


【解决方案1】:

我找到了这个解决方案:

    p:url="#{inetAddress.getLocalHost().getHostName().equals('andrej-HP-290-G1-SFF-Business-PC')?'jdbc:postgresql://localhost:5432/infostock':'jdbc:postgresql://172.18.0.2:5432/infostock'}"

但它太长了,我想它不是最佳的

【讨论】:

    猜你喜欢
    • 2020-02-08
    • 2019-07-14
    • 2011-05-05
    • 2014-09-29
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多