spring 设置通过代码启动加载配置文件和设置启动顺序



通过这种方式加载可以使用maven模块化,抛开web.xml的束缚。


@Order(1)
public class SpringConfigInitializer extends AbstractContextLoaderInitializer
  implements WebApplicationInitializer
{
  protected WebApplicationContext createRootApplicationContext()
  {
    String allXml = getRootContextConfigLocation();
    XmlWebApplicationContext s = new XmlWebApplicationContext();
    s.setConfigLocation(allXml);
    return s;
  }


  public String getRootContextConfigLocation() {
    String configLocationParam = "classpath:config/bomf/spring/applicationContext.xml";
    Properties props = StrTool.getProperties("/config/bomf/dssp.properties");
    boolean webOnly = 
      Boolean.valueOf(props
      .getProperty("webOnly", "false")).booleanValue();


    if (!webOnly) {
      configLocationParam = configLocationParam + ",classpath:config/bomf/server/*.xml";
    }
    else {
      configLocationParam = configLocationParam + ",classpath:config/bomf/client/*.xml";
    }


    boolean jaxws = Boolean.valueOf(props.getProperty("jaxws", "false")).booleanValue();
    if (jaxws) {
      configLocationParam = configLocationParam + ",classpath:config/bomf/spring/jax-ws.xml";
    }


    boolean hessianConsumer = Boolean.valueOf(props.getProperty("hessianConsumer", "false")).booleanValue();


    if (hessianConsumer) {
      configLocationParam = configLocationParam + ",classpath:config/bomf/spring/hessian-comsumer.xml";
    }


    String cacheProvider = props.getProperty("cache.provider", "ehcache");
    configLocationParam = configLocationParam + ",classpath:config/bomf/spring/application-cache-" + cacheProvider + ".xml";


    boolean dubbo = Boolean.valueOf(props.getProperty("dubbo", "false")).booleanValue();


    if (dubbo) {
      configLocationParam = configLocationParam + ",classpath:config/bomf/spring/dubbo.xml";
    }


    if (!webOnly) {
      boolean isJndi = Boolean.valueOf(props.getProperty("isJndi", "false")).booleanValue();


      if (isJndi) {
        configLocationParam = configLocationParam + ",classpath:config/bomf/spring/jndi_db.xml";
      }
      else {
        configLocationParam = configLocationParam + ",classpath:config/bomf/spring/c3p0_db.xml";
      }
    }


    String otherspringxml = props.getProperty("otherspringxml");
    if (StrTool.isValid(otherspringxml)) {
      configLocationParam = configLocationParam + "," + otherspringxml;
    }
    String securityType = props.getProperty("securityType", "spring");
    if ((StrTool.isValid(securityType)) && (!"none".equals(securityType))) {
      configLocationParam = configLocationParam + ",classpath:config/bomf/spring/" + securityType + "-security.xml";
    }


    return configLocationParam;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值