2017-02-27 12 views
1

サーブレットが実行されていて、フィルタにプロパティ値を注入しようとしています。@Valueはプロパティを挿入しません。nullのままです。

私はappConfigファイルがロードされていると確信しています(ファイル名を変更すると、FileNotFound例外が発生します)。 properties-fileと同じ数です。

私がプロパティを注入しようとするクラスは、Springによって何とか無視されるようです。これはフィルターです(下記参照)。私はアノテーション自体にプロパティ値を追加することでこれを試しました。 (@value( "$ {filter.weburl: 'いくつかの'}" は。。?)しかし、文字列webURLがNULLのまま

誰も私がここで何が起こっているかを把握助けることはでき

package example.servlet.filters; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 

import javax.servlet.*; 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 


@Component 
public class AuthenticationFilter implements Filter{ 

    private ServletContext context; 
    private final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilter.class); 
    @Value("${filter.weburl:'some'}") 
    private String webURL; 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
     this.context = filterConfig.getServletContext(); 
     this.context.log("AuthenticationFilter initialized"); 

    } 

    @Override 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
     HttpServletRequest request = (HttpServletRequest) servletRequest; 
     Cookie[] cookies = request.getCookies(); 
     if(cookies != null) { 
      for (Cookie cookie : cookies) { 
       System.out.println(cookie.getName() + " " + cookie.getValue() + "\n"); 
      } 
     } else { 
      ((HttpServletResponse)servletResponse).sendRedirect(webURL + "/inloggen"); 
     } 
     filterChain.doFilter(servletRequest, servletResponse); 
    } 

    @Override 
    public void destroy() { 

    } 
} 

マイAppConfigファイル:

package example; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 

@Configuration 
@ComponentScan("example") 
@PropertySource("WEB-INF/service.properties") 
public class AppConfig { 
    @Bean 
    public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Bean 
    public FilterRegistrationBean authenticationFilterRegistrationBean() { 
     FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); 
     filterRegistrationBean.setFilter(getAuthenticationFilter()); 
     filterRegistrationBean.addUrlPatterns("/*"); 
     filterRegistrationBean.setName("authenticationFilter"); 
     filterRegistrationBean.setOrder(1); 
     return null; 
    } 

    @Bean(name="authenticationFilter") 
    public AuthenticationFilter getAuthenticationFilter() { 
     return new AuthenticationFilter(); 
    } 
} 
+0

あなたのフィルタは、実際にも、春の豆のですか?基本的には、解決できない場合はフィールドを 'null'にすることはできません。あなたが指定したデフォルトにフォールバックします。実際にはそうしていないので、私が使用するフィルタのインスタンスがSpringが知っているインスタンスであることは疑いの余地があります。 –

+0

@Valueによって入力されるフィールドをpublicに設定しようとしましたか? –

答えて

1

あなたのコンフィギュレーションクラスで、次のしている必要があり

@Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 

web.xmを使用してフィルタを設定します。あなたはそれが、すべての要求のために登録されます、あなたがFilterRegistrationBeanを使用している場合、あなたはフィルタを適用するURLパスをカスタマイズすることができ、アプリケーションのコンテキストでのフィルタを登録した場合、Lはこの

<filter> 
    <filter-name>authenticationFilter</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>authenticationFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
+0

私はこれを私の質問に追加したはずですが、すでにそこにあります:-)。私はこれを明確にするために私の質問を編集しました。 –

+0

どのようにフィルタを登録していますか?あなたはそのコードを共有できますか? – mhshimul

+0

ここに私の答えを見なさいhttp://stackoverflow.com/questions/42176625/spring-boot-1-5-1-inside-registered-filter-can-not-access-spring-context-valu – mhshimul

1

を行います。あなたは両方を持っているようだし、おそらくあらゆる種類の問題を引き起こしているでしょう。また、フィルタに@Componentの注釈があり、構成クラスのBeanとしてフィルタを作成しています。

これは、あなたがそれを動作させるためにあなたのコードを構造化する方法である。

// No @Component annotation keeps this class pure as you're using your configuration class to create beans 
public class AuthenticationFilter implements Filter{ 

    private ServletContext context; 
    private final Logger LOGGER = LoggerFactory.getLogger(AuthenticationFilter.class); 
    private String webURL; 

    public AuthenticationFilter(String webURL) { 
     this.webURL = webURL; 
    } 

    // rest of filter 
} 

Configurationクラス:

@Configuration 
@ComponentScan("example") //if you have other components to scan, otherwise not required 
@PropertySource("WEB-INF/service.properties") 
public class AppConfig { 

    @Value("${filter.weburl:some}") 
    String webURL; 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Bean 
    public FilterRegistrationBean authenticationFilterRegistrationBean() { 
     FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); 
     filterRegistrationBean.setFilter(new AuthenticationFilter(this.webURL)); 
     filterRegistrationBean.addUrlPatterns("/*"); 
     filterRegistrationBean.setName("authenticationFilter"); 
     filterRegistrationBean.setOrder(1); 
     return filterRegistrationBean; 
    } 
} 
関連する問題