SpEL Spring中的三元运算符

问题描述 投票:0回答:1

我有以下表达:

 <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'。

java spring hostname conditional-operator spring-el
1个回答
0
投票

我已经找到了解决方案:

    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'}"

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

© www.soinside.com 2019 - 2024. All rights reserved.