2017-04-24 7 views
0

저는 배포에서 배포로 변경되는 컨텍스트를 가지고 있으며, 일부 컨텍스트는 모든 배포에서 일정합니다. 배포 종속 비트는 jar 또는 war 외부의 config.xml 파일에 있습니다. 정적 컨텍스트 stuff는 주석 구성 클래스에 있습니다. 그 속임수는 config config.xml의 bean을 사용하는 주석입니다. 앱의 경우 config 클래스로 가져옵니다. 그러나 webapp의 경우 해당 파일을 찾을 수 없습니다.SpringConfig.class의 config.xml에있는 spring - beans를 사용합니다.

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> 

SpringConfig이 배포 폴더의 Config.xml을 찾고 특정 배포를 사용하도록 config.xml 파일이 항아리에 포함되지 않습니다 문맥. 그러나 WebApplicationInitializer를 통해 webapp에 SpringConfig를 배포 할 때 더 이상 config.xml을 찾을 수 없습니다. 당연하지. 그렇다면 어떻게 전개 종속적 인 컨텍스트의 빈을 웹 애플리케이션의 정적 컨텍스트에로드 할 수 있습니까? @Configuration 및 @Components에 대한 패키지를 검사하도록 AnnotationConfigApplicationContext에 지시하는 방법이 있습니까? webapp의 xml 구성에서 beans를 등록한 후에 ?

아마도 생성자에서 setParent (config.xml 컨텍스트)를 호출 한 다음 AnnotationConfigWebApplicationContext (SpringConfig.class)를 호출하는 AnnotationConfigWebApplicationContext에서 파생 된 클래스라고 생각합니다.

또는 AnnotationConfigApplicationContext (String ... basePackages)를 호출하는 대신 AnnotationConfigApplicationContext()를 호출 한 다음 setParent (...)를 호출 한 다음 scan (...)을 호출합니다.

답변

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 ... 
}