我有以下表达:
<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'。
我已经找到了解决方案:
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'}"
但是它太长了,我想这不是最佳的