0

my qualifier is :

@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
public @interface JPADAOQ {
}

this is the bean that i want inject

@JPADAOQ
@ApplicationScoped
public class PlotDaoImpl extends JpaDao<Long, Plot> implements PlotDao {
    public PlotDaoImpl() {
      super(Plot.class);
    }
}

finally here is where i want my bean to be injected

@ManagedBean
@SessionScoped
public class PlotController {
    @Inject @JPADAOQ
    private PlotDaoImpl plotDaoImpl;

    @PostConstruct  
    public void init() {        
      if (plotDaoImpl==null) {
        System.out.println("plotDaoImpl null");
      } else {
        System.out.println("plotDaoImpl not null"); 
      }
    }

But i always got it null so i can't use it.

My configuration :
1.beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                  http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all">
</beans>

2.pom.xml based on wildfly quickstart form here.

   <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-7.0</artifactId>
            <version>${version.jboss.spec.javaee.7.0}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>        
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.spec.javax.annotation</groupId>
        <artifactId>jboss-annotations-api_1.2_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.spec.javax.servlet</groupId>
        <artifactId>jboss-servlet-api_3.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

I can't figure out why my bean is not injected int @PostConstruct method.

jsf
  • 68
  • 3
  • 13

2 Answers2

2

@javax.faces.bean.ApplicationScoped specify that the JSF managed bean is application scoped, yet you must annotated to be a managed bean.

@javax.enterprise.context.ApplicationScoped is a CDI story. You must produce it somehow. Maybe add a method in PlotDaoImpl annotated with @Produces that returns this.

Guillermo
  • 1,523
  • 9
  • 19
0

Firstly, I assume your @ApplicationScoped is in fact javax.enterprise.context.ApplicationScoped. Although it would cause no harm if it wasn't as all discovery mode will still pick the class as a bean.

Secondly, @ManagedBean annotation can be removed as the CDI will automatically pick up the class as a managed bean.

And thirdly, make sure you actually have an active session when you test your PlotController. You have to let CDI create the PlotController bean for you in order to make injection work.

If naught else helps, you might try to replace @SessionScoped with @RequestScoped as the behaviour might slightly differ when it comes to bean availability during @PostConstruct methods but that is just a guess.

Siliarus
  • 6,393
  • 1
  • 14
  • 30