1

I have an application scoped managed bean which I am trying to inject into a session filter to filter rules based on the map provided from application scoped bean .

The Application scoped beans purpose was to load application configuration from the database into the Map which can be accessed during the scope of the application.

@Named
@ApplicationScoped
public class ApplicationConfig implements Serializable {

    private  Map<String,String> accessRule;

    private static final long serialVersionUID = -7984677603595580195L;

    @PostConstruct
    public void init() throws SQLException, Exception {
        System.out.println("ApplicationContainer INIT");

        accessRule.put("A", "A");
    }

    public Map<String,String> getAccessRule() {
        return accessRule;
    }

    public void setAccessRule(Map<String,String> accessRule) {
        this.accessRule = accessRule;
    }
}

I have tried @PostConstruct and also tried using the constructor too but the bean is not being called.This how the Named bean is being injected

@WebFilter(urlPatterns = { "/*" })
public class ApplicationFilter implements Filter {

    private static final String FACES_RESOURCES = "javax.faces.resource";

    private static final Logger log = Logger.getLogger(ApplicationFilter.class.getName());

    private boolean disableFilter;

    private String errorPage;

    private String indexPage;

    @Inject
    public ApplicationConfig applicationConfig;

    private List<String> ignoredResources = new ArrayList<>();

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
        System.out.println(applicationConfig.getAccessRule());
        arg2.doFilter(arg0, arg1);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }
}

I have used @Named / @Inject and still doesn't work. I want to use a application scoped bean that takes details from DB and used it in a WebFilter. Kindly help

Kukeltje
  • 11,924
  • 4
  • 19
  • 44
  • @balusc Follwing the suggestion that you made on another post i update my code ApplicationConfig config = (ApplicationConfig) arg0.getServletContext().getAttribute("applicationConfig"); System.out.println(config.getAccessRule()); I am getting null pointer again – boilerplate.code May 04 '20 at 11:24
  • @BalusC Done but still get null values Inject private ApplicationConfig config; – boilerplate.code May 04 '20 at 11:59
  • @BalusC . I have used Named / Inject and still doesn't work. I want to use a application scoped bean that takes details from DB and used it in a WebFilter. Kindly help – boilerplate.code May 07 '20 at 14:38
  • @BalusC I am using tomcat as the server – boilerplate.code May 09 '20 at 19:25
  • Checked this? https://stackoverflow.com/questions/18995951/how-to-install-and-use-cdi-on-tomcat – areus May 12 '20 at 17:34

0 Answers0