如何在XML配置中执行以下XML配置

时间:2019-03-25 15:25:01

标签: java spring spring-boot

我正在将XML配置转换为Java配置。我不确定如何执行以下XML配置。请帮忙。谢谢

  <beans profile="!cloud">
    <bean name="remotingURL" class="java.lang.String">
        <constructor-arg value="${web.remoting.url.base}/#{applicationID.toLowerCase()}server/remoting" />
    </bean>
</beans>
<beans profile="cloud">
    <bean name="remotingURL" class="java.lang.String">
        <constructor-arg value="${web.remoting.url.base}/remoting" />
    </bean>
</beans>

2 个答案:

答案 0 :(得分:1)

为所需的每个配置文件创建一个配置类。然后使用@Value从属性中注入值。

@Configuration
@Profile("cloud")
public class CloudConfig{
@Bean
public String remoteURL(@Value("${web.remoting.url.base}") String url) {
return url + "/remoting";
}
}

@Configuration
@Profile("!cloud")
public class RemoteConfig{
@Bean
public String remoteURL(@Value("${web.remoting.url.base}") String url, String applicationID) {
return url + "/" + applicationId.toLowerCase() + "server/remoting";
}
}

答案 1 :(得分:0)

@Configuration
  public class Test {

  @Bean(name = "remotingURL")
  @Conditional(NotCloud.class)
  public String remotingURL1(@Value("${web.remoting.url.base}") String url, String applicationID) {
    return new String(url + "/" + applicationId.toLowerCase() + "server/remoting");
  }

  @Bean(name = "remotingURL")
  @Conditional(Cloud.class)
  public String remotingURL2(@Value("${web.remoting.url.base}") String url) {
    return new String(url + "/remoting");
  }


    }        

     @Override
     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
                return !context.getEnvironment().getActiveProfiles().toString().equalsIgnoreCase("cloud");
              }
            }



    public class NotCloud implements Condition {

      @Override
      public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return !context.getEnvironment().getActiveProfiles().toString().equalsIgnoreCase("cloud");
      }
    }