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