サーブレットが実行されていて、フィルタにプロパティ値を注入しようとしています。@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();
}
}
あなたのフィルタは、実際にも、春の豆のですか?基本的には、解決できない場合はフィールドを 'null'にすることはできません。あなたが指定したデフォルトにフォールバックします。実際にはそうしていないので、私が使用するフィルタのインスタンスがSpringが知っているインスタンスであることは疑いの余地があります。 –
@Valueによって入力されるフィールドをpublicに設定しようとしましたか? –