2017-04-24 53 views
0

我有一些從部署到部署的變化,有些變化對於所有部署都是不變的。依賴於部署的位位於jar或war之外的config.xml文件中。靜態上下文的東西在註釋配置類中。訣竅是註釋配置使用config.xml中的bean。對於應用程序,它將在配置類中導入。但對於webapp,它無法找到該文件。spring - 在SpringConfig.class中使用config.xml中的bean

SpringConfig類

@Component 
@Configuration 
@ImportResource("file:./config.xml") 
public class SpringConfig { 

    @Autowired private String local_timezone_string; 
    @Autowired private String startDateFormatString; 
    @Autowired private String startDateString; 

    @Bean 
    public TimeZone localTimeZone() { 
     return TimeZone.getTimeZone(local_timezone_string); 
    } 

    @Bean 
    public SimpleDateFormat startDateFormat() { 
     SimpleDateFormat sdf = new SimpleDateFormat(startDateFormatString); 
     sdf.setTimeZone(localTimeZone()); 
     return sdf; 
    } 

    @Bean 
    public long jaceThreadStartDate() throws ParseException { 
     return startDateFormat().parse(jaceThreadStartDateString).getTime(); 
    } 

    ... a bunch of other beans 
} 

和config.xml中

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="local_timezone_string" class="java.lang.String"><constructor-arg value="Canada/Atlantic"/></bean> 
    <bean id="startDateFormatString" class="java.lang.String"><constructor-arg value="yyyy-MM-dd"/></bean> 
    <bean id="startDateString" class="java.lang.String"><constructor-arg value="2017-04-06"/></bean> 

    ... other beans 

</beans> 

config.xml文件中不包括罐,以便SpringConfig查找config.xml文件在部署文件夾,並使用特定的部署上下文。但是當通過WebApplicationInitializer在webapp中部署SpringConfig時 - 它不能再找到config.xml。當然。那麼,如何將依賴於部署的上下文中的bean加載到webapp中的靜態上下文中?有沒有辦法告訴AnnotationConfigApplicationContext掃描@Configuration的包和@Components 註冊來自webapp中xml配置的bean?我在想也許一個派生自AnnotationConfigWebApplicationContext的類 - 在構造函數中 - 調用setParent(config.xml上下文),然後調用AnnotationConfigWebApplicationContext(SpringConfig.class)。而不是調用AnnotationConfigApplicationContext(String ... basePackages),而不是調用AnnotationConfigApplicationContext(),然後setParent(...),然後掃描(...)。

回答

0

所以有一種使用setParent()和scan()的方法。

@Component 
@Configuration 
public class SpringConfig { 

    private static final Logger log = Logger.getLogger(SpringConfig.class); 
    @Autowired private DriverManagerDataSource pgDataSource; 
    @Autowired private String local_timezone_string; 
    @Autowired private String startDateFormatString; 
    @Autowired private String startDateString; 

    private static ApplicationContext getVariableContext() throws Exception { 

     ApplicationContext variableContext = null; 
     try { 
      variableContext = new FileSystemXmlApplicationContext("./config.xml"); 
      log.info("got config.xml from file system"); 
     } catch (Exception e) { 
      try { 
       variableContext = new ClassPathXmlApplicationContext("config.xml"); 
       log.info("got config.xml from class path"); 
      } catch (Exception ex) { 
       log.info("failed to get config.xml"); 
       throw new Exception(...); 
      } 
     } 
     return variableContext; 
    } 

    public static WebApplicationContext getWebApplicationContext() throws ServletException { 

     try { 
      ApplicationContext variableContext = getVariableContext(); 
      AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); 
      appContext.setParent(variableContext); 
      appContext.scan(
        "com.us.systemdiagnostics", 
        "com.us.systemdiagnostics.webservice" 
      ); 
      appContext.refresh(); 
      return appContext; 

     } catch (Exception e) { 
      throw new ServletException(...); 
     } 
    } 

    public static ApplicationContext getStandAloneApplicationContext() throws ServletException { 

     try { 
      ApplicationContext variableContext = getVariableContext(); 
      AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(); 
      appContext.setParent(variableContext); 
      appContext.scan(
        "com.us.systemdiagnostics", 
        "com.us.systemdiagnostics.webservice" 
      ); 
      appContext.refresh(); 
      return appContext; 

     } catch (Exception e) { 
      throw new Exception(...); 
     } 
    } 

    ... beans, etc ... 
} 
相關問題