0

내 웹 애플리케이션에서 AbstractAnnotationConfigDispatcherServletInitializer에서 확장되는 CustomWebApplicationInitializer를 작성 중입니다. 응용 프로그램의 다른 속성 소스를 추가해야합니다. 내가 servletContext에 InitParameter를 설정하여 아래와 같이 onStartup 메소드에서 그렇게하고 있습니다. 특정 속성의 값이 참으로 설정된 경우스프링 웹 애플리케이션 컨텍스트

public class MvcWebApplicationInitializer extends 
     AbstractAnnotationConfigDispatcherServletInitializer { 
    private static final String[] SERVLET_MAPPINGS = new String[] {"/"}; 
    private static final String SESSION_COOKIE_PATH = "/"; 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[] {AppConfig.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] {WebConfig.class}; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return SERVLET_MAPPINGS; 
    } 


    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     super.onStartup(servletContext); 
     servletContext.setInitParameter("contextInitializerClasses", "com.test.myproject.MyTestPropertySourceInitializer"); 
    } 
    } 

는 MyTestPropertySourceInitializer는 ApplicationContextInitializer 및 검사를 실행한다.

public void initialize(ConfigurableApplicationContext applicationContext) { 
    ConfigurableEnvironment env = applicationContext.getEnvironment(); 
    Boolean testPropEnabled = (Boolean) env.getProperty("testProperty.enabled", Boolean.class); 

이 속성 값을 true로 설정했습니다. 그러나 로그 값은 false이며 예상대로 처리되지 않습니다. 그래서 initialize 메서드가 호출 될 때까지 속성 파일이 applicationContext에로드되지 않았다고 생각합니다. 이 기능을 어떻게 사용할 수 있는지 알려주십시오. 미리 감사드립니다.

답변

0

시도, 강제로 속성이 혼란에 대한 설명은 죄송

public void initialize(ConfigurableApplicationContext applicationContext) { 
    ConfigurableEnvironment env = applicationContext.getEnvironment(); 
    env.getPropertySources().addFirst(new ResourcePropertySource("classpath:application.properties")) 

    Boolean testPropEnabled = (Boolean) env.getProperty("testProperty.enabled", Boolean.class); 

} 
+0

파일로드합니다. initialize 메소드는 많은 다른 프로젝트에서 사용되는 또 다른 일반 라이브러리에 있습니다. 그래서 우리는 거기에로드 할 수 없습니다. 내가 servletcontext setInitParameter 메소드를 호출하기 전에 내 프로젝트에서로드를 시도하고 그게 작동하는지 알려주지. – NewQueries

관련 문제