CDI Java - Configuration

Asked

Viewed 1,016 times

2

I created a simple design containing a Test class and a Testeinterface interface.

In the interface test I created any method just to test. In the Test class I only put @Inject Service service;

When I call the interface method, is giving nullpointer.

What else is needed to use CDI? I’ve put Weld dependency to use annotations:

    <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se-core</artifactId>
        <version>2.4.1.Final</version>
    </dependency>

I also created the same empty Beans.xml, because I read that you need to have this file.

Someone can give me a boost with the CDI configuration?

1 answer

1

It was necessary to know which server you are trying to run on.
If you are using a server after java 6 Beans.xml should already solve the service, which I imagine is not your case. In this link there is good content.
http://blog.caelum.com.br/use-cdi-no-seu-proximo-projeto-java/

Before continuing, change your version of Maven to Artifact-id: Weld-Servlet:

<dependency>
   <groupId>org.jboss.weld.servlet</groupId>
   <artifactId>weld-servlet</artifactId>
   <version>2.4.1.Final</version>
</dependency>

Assuming you are using the most common for studies, which is Tomcat and not in a recent version, I will abbreviate the content of the link since your jars have already been provided by Maven:

Create a file called context.xml with the contents in the META-INF folder:

<?xml version="1.0" encoding="UTF-8"?>
    <Context>
    <Manager pathname=""/> <!-- disables storage of sessions -->
  <Resource name="BeanManager"
      auth="Container"
      type="javax.enterprise.inject.spi.BeanManager"
      factory="org.jboss.weld.resources.ManagerObjectFactory"/>
</Context>

And add the Weld configuration to the web.xml:

<listener>
    <listener-class>   
        org.jboss.weld.environment.servlet.Listener
    </listener-class>
</listener>

<resource-env-ref>
   <resource-env-ref-name>BeanManager</resource-env-ref-name>
   <resource-env-ref-type>
      javax.enterprise.inject.spi.BeanManager
   </resource-env-ref-type>
</resource-env-ref>
  • Pedro, I’m using Tomcat yes, in version 9. I did what you said but now I can’t even start the server. &#xA;&#xA;java.lang.NoSuchMethodError: org.jboss.weld.bootstrap.spi.BeansXml.getUrl()Ljava/net/URL;&#xA;at org.jboss.weld.xml.BeansXmlParser.merge(BeansXmlParser.java:165) GRAVE: Exception sending context destroyed Event to Listener instance of class org.jboss.Weld.environment.Servlet.Listener java.lang.Nullpointerexception

  • I made a test project for Tomcat 9. It does not need the settings in web.xml, but following the guidelines of Balus C in the following link, it is better to keep the context. http://balusc.omnifaces.org/2013/10/how-to-install-cdi-in-tomcat.html I’ll update the response with another version of Weld on Maven dependencies, that’s probably what’s causing your problem, replace your dependency with the new one and run the test.

  • Pedro, I tried exactly how it is on the link you sent, but it didn’t work. You’re still giving nullpointer to the interface method call I injected. I commited the CDI stuff to github, can you help me out, bro? https://github.com/guilhermenass/controle-financial

  • Looking over I saw that you are injecting the service directly and calling the method, but how would the container know which implementation of the interface is being used ? When using inject you are using the dependency inversion concept, i.e., the instantiation of the object to be a function of the web container and not its, however, interfaces can only be instantiated anonymously, with an implementation being specified for the abstract method. Try injecting Userservicebean directly and tell me what happens.

  • I tried to inject directly the Userservicebean and continues giving nullpointer.

  • Can someone help me?

  • Hello William, did you manage to solve? I have the same problem as you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.