过滤器无法使用@Value读取配置文件解决办法

1.新建一个中间静态实体

public class HostConstants {

    public static String Hosts;

    public static String getHosts() {
        return Hosts;
    }

    public static void setHosts(String hosts) {
        Hosts = hosts;
    }

    public void initBean() {
        Hosts = this.Hosts;
    }
}

2、注入bean并赋值给中间实体

<bean id="hostConstants" class="com.xuanwu.web.common.entity.HostConstants" init-method="initBean">
	<property name="Hosts" value="${legalHost}">property>
bean>

3、过滤器读取中间实体的值

public class StaticSourceFilter implements Filter {

    private List<String> hosts;
    private static Logger logger = LoggerFactory.getLogger(StaticSourceFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String legalHost = HostConstants.Hosts;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    
    }

    @Override
    public void destroy() {

    }
}

你可能感兴趣的:(java,开发语言)