CDI session bean initialization - jsf-2

I have two beans:
#Named
#SessionScoped
public class Session implements Serializable {
private String temp;
+getter, setter
}
#Named
#RequestScoped
public class Test {
#Inject
private Session s;
#PostConstruct
public void init() {
this.sth = s.getTemp(); //here is exception
}
}
When I try to fetch in init same value from session I get NullPointerException. What am I doing wrong? Server is JBoss eap 6.0, JSF v2.1. Thanks in advance
Edit: I investigated that problem appears only when I have more than 11 items in primefaces submenu component. If I have less than 11 items every thinks work OK. I am using PrimeFaces v 3.5.
Edit 2: On session bean only constructor is invoked. I have method annotated with PostConstruct but it is not called. I always get NullPointerException after invoking any method on session from Test bean.

Related

Can't inject the guice dependency in the jersey filter

In the process of setup a bridge between guice and jersey, I ran into one problem.
When trying to create a jersey filter, I was unable to inject guice dependencies into it.
I found a duplicate, however there is no solution to the problem there.
Everything is exactly the same.
The only difference is that I don't get a startup error. The filter works, but my dependencies are null.
Interestingly, Filter and HttpFilter work fine. But it doesn't really work for me.
There's another thing that's interesting. In the resource, which I understand is an HK2 dependency, I can inject guice bean.
#ApplicationPath("/test")
private static class TestApplicationConfig extends ResourceConfig
{
public TestApplicationConfig()
{
register(JacksonFeature.class);
register(AuthFilter.class);
register(new ContainerLifecycleListener()
{
public void onStartup(Container container)
{
ServletContainer servletContainer = (ServletContainer) container;
ServiceLocator serviceLocator = container.getApplicationHandler().getServiceLocator();
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
Injector injector = (Injector) servletContainer
.getServletContext()
.getAttribute(Injector.class.getName());
guiceBridge.bridgeGuiceInjector(injector);
}
public void onReload(Container container)
{
}
public void onShutdown(Container container)
{
}
});
}
}
In ServletModule child.
serve(path).with(ServletContainer.class, ImmutableMap.of(
"javax.ws.rs.Application", TestApplicationConfig.class.getName(),
"jersey.config.server.provider.packages", sb.toString()));
I trying with register(AuthFilter.class) and #Provider
#Singleton
#Provider
public class AuthFilter implements ContainerRequestFilter
{
#Inject
private SomeInjectedService someInjectedService; **// null here**
#Context
private ResourceInfo resourceInfo;
#Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
// some code
}
}
SomeInjectedService I register by guice
bind(SomeInjectedService.class).asEagerSingleton();
Where can I start diagnosing and what can I do?
UPD:
I noticed different behavior when using different annotations.
If I use javax.inject.Inject, I get the following error message.
org.glassfish.hk2.api.MultiException: A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=SomeInjectedService,parent=AuthFilter,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1496814489)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of some.package.AuthFilter errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on some.package.AuthFilter
If com.google.inject.Inject, just null. As I understand this method is not correct.
Considering that javax Inject is trying to inject the service but can't find it. Can we conclude that the bridge is not working correctly? But if it's not working correctly, why can I inject this service into my resource?
#Path("/test")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public class SomeResource
{
private final SomeInjectedService someInjectedResource;
#Inject // here I use javax annotation and this code working correctry
public SomeResource(SomeInjectedService someInjectedResource)
{
this.someInjectedResource = someInjectedResource;
}
#GET
#Path("/{user}")
public Response returnSomeResponse(#PathParam("user") String user) throws Exception
{
// some code
}
}

SLSB EJB 3.1 references in weblgioc

I have implement simple EJB 3.0 deployed it on weblogic 12c. The EJB contains a reference to a different EJB as a member declared as a reference of implemented local interface with annotation #EJB. When I invoke a method of the encapsulated EJB I get a null pointer exception. The same does not happen when I inject session context or data source using #SessionContext or #Resource respectivley. What I want to understand is do I have to define a reference in
in weblogic.xml for linking two EJBs. Is this same for all production grade application servers like jboss, websphere or Tomcat EE.
Here is more details about code & deployment:
#Remote
public interface MyEjbRemote1 {
abstract public void func1();
}
#Stateless
public class MyEJB1 implements MyEjbRemote1 {
public void func1() {}
}
#Remote
public interface MyEjbRemote {
abstract public void func();
}
#Stateless
public class MyEjb implements MyEjbRemote, MyEjbLocal {
#EJB
MyEjbRemote1 ejb1;
#Resource
SessionContext sessionContext;
public void func() {
ejb1.func1();
}
public void func1() {
System.out.println("Local interface call for EJB");
}
}
The call to func1 method using remote interface is throwing a null pointer exception.
I have not put any details in ejb-jar.xml, weblogic-ejb-jar.xml or weblogic.xml. My question is what addition details is required.

#PostConstruct not invoked in PhaseListener

I am using jsf2.2 with wildfly 8.1 and javaee7.
My CDI bean injection in the phaselistener works as expected, but the #PostConstuct method is never invocked
I have tried to annotate the phaselistener with #javax.enterprise.context.ApplicationScope, SessionScope and Dependent to no avail.
Apart from naming, this is the exact thing i do in my post construct.
//#ApplicationScope
//#SessionScope
//#Dependent
public class MyPhaseListener implements PhaseListener {
#Inject
#Any
private Instance<MyOrderedUrlHandler> myOrderedUrlhandlers;
private Map<String, List<MyOrderedUrlHandler> orderedUrlHandlersMap;
#PostConstruct
void mapOrderedUrlHandlers() {
LOG.info("Executing postconstruct");
orderedUrlHandlersMap = Maps.newHashMap();
for(final MyOrderedUrlHandler urlhandler : myOrderedUrlhandlers) {
final String handles = urlhandler.url();
final List<MyOrderedUrlHandler> registeredHandlers = orderedUrlHandlersMap.get(handles);
if(registeredHandlers == null) {
registeredHandlers = Lists.newArraList();
}
registeredHandlers.add(urlHandler);
orderedUrlHandlersMap.put(handles, registeredHandlers);
}
}
}
Method level injection also works fine.
Is it the case that #PostConstruct callback is not part of jsf phaselistener specs?
According to section 5.4.1 of the JSF 2.2 spec, PhaseListener is not a managed bean but is injectable.
According to section 5.4.2, managed beans must support lifecycle annotations #PostConstruct and #PreDestroy.
Since a PhaseListener is not a managed bean in the sense of JSF, it does not follow from the spec that a phase listener implementation must support #PostConstruct.

Converter attribute of outputText set to bean property breaks ViewScope

Based on the third option/example provided in this answer https://stackoverflow.com/a/7531284/944396 I tried to have a converter be a property in my ViewScoped bean; however, when I do so it breaks the view scope for that bean (it gets constructed multiple times when it should not be).
I am not sure if I am doing something wrong, or if that example is assuming you are running a newer version of Mojarra that has fixed the ViewScope/binding issue. I myself am running Mojarra 2.1.6. Here is the bean code:
#ManagedBean
#ViewScoped
public class Bean {
private Double myVal;
private Converter converter;
public void Bean() {
converter = new MyCustomConverterClass();
}
public Converter getConverter() {
return converter;
}
public Double getMyVal() {
return myVal;
}
public void setMyVal(Double myVal) {
this.myVal = myVal;
}
}
And the usage:
<h:outputText value="#{bean.myVal}" converter="#{bean.converter}" />
You're right, it breaks the view scope. The converter attribute is evaluated during view build time. Apart from using other options listed in the answer you found How to set converter properties for each row of a datatable?, you'd need to upgrade to at least Mojarra 2.1.18.

Why can't I Init attribute in Managed Bean constructor?

I have a Managed Bean:
public class CategoriaManagedBean {
#EJB
private CategoriaBeanLocal categoriaBean;
private Categoria categoria;
private List<Categoria> menu;
}
In my constructor I try:
public CategoriaManagedBean() {
menu = categoriaBean.findByIdCategoriaPadre(0);
}
But I get a error "Cannot create the instance of the class", why can't I initialize the attribute in the constructor?
I fix the problem with:
#PostConstruct
public void init() {
menu = categoriaBean.findByIdCategoriaPadre(0);
}
But I want to know the reason and if I am doing well with #PostConstruct
Greetings.
Using #PostConstruct is the correct approach.
EJBs are injected after the constructor is invoked on a ManagedBean.
That's why there is a #PostConstruct annotation.
Here's the first line from the documentation:
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

Resources