Welding is not initialized properly

I am creating a basic environment for learning CDI in JavaEE7. I have the following code to run Weld . Just starting and shutting down.

 package com.anshbansal; import org.jboss.weld.environment.se.Weld; import org.jboss.weld.environment.se.WeldContainer; public class Main { public static void main(String[] args) { Weld weld = new Weld(); WeldContainer container = weld.initialize(); weld.shutdown(); } } 

I am on my console.

 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/Softs/Programming/Java/Java%20JARs/JBoss%20Weld-2.0.3/jar/weld-se.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/Softs/Programming/Java/Java%20JARs/JBoss%20Weld-2.0.3/jar/weld-servlet.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory] [main] INFO org.jboss.weld.Version - WELD-000900 2.0.3 (Final) [main] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. log4j:WARN No appenders could be found for logger (org.jboss.logging). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

Problematic line WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. . It just means that dependency injection will not work. But I'm not sure what the problem is. I added weld-se.jar to my CLASSPATH . I have not even reached the point of initialization of the object, then why does this problem arise?

Weld's official documentation also gives the same code I got after reading this answer . The same code is used in Antonio Goncalves' Beginning of Java EE 7. I checked the import from this github location . Therefore, if I used the correct class path and did not create any object, then why does this problem arise?

+8
java java-ee cdi weld
source share
3 answers

To run a Java EE application, an application server (or container) is required. This container simplifies your life with regard to the integration of various services (security, messaging, transactions, etc.) necessary to launch a corporate application.

If you are not using an application server (for example, you do it in your example), you need to do this integration yourself (for example, create your own server). This is a very difficult and useless task since there are servers.

The code you show in your question is how you can use the patented Weld part to start the CDI container manually when you don't want or can use the container.

If you carefully read Antonioโ€™s book, youโ€™ll see that the xxxiv page page in the Download and Run Code section indicates that you need to deploy your code to Glassfish 4, one of the open source Java EE 7 servers (another JBoss Wildfly )

Appendix A books ( p. 539 ) describe very precisely how to set up the environment for running sample books and simple Java EE applications. Please follow the instructions in this part and you will see that developing and deploying a Java EE 7 application is very simple.

+1
source share

Your setup is suitable for learning CDI in Java SE.

To use CDI in Java EE, you obviously need a Java EE container, a simple old application with the main method will not.

Weld simply informs you that transactions are not available (since you are not working in an EE container), so any CDI functions associated with transactions will be disabled.

Injection injection will work in your case if you are not trying to inject any Java EE objects or use any CDI features that require a Java EE container.

+5
source share

To set up a CDI learning environment, you only need a CDI implementation, such as the Weld reference implementation; You do not need a Java EE container.

An easy way is to create a maven project with a dependency in pom.xml : (Be sure to create an empty or minimal beans.xml file in the META-INF before running the java application. If you switch to maven, you can create META-INF in the src/main/resources directory src/main/resources )

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

Here is the application:

 public class CdiApplication { private Weld weld; private WeldContainer container; private BookService bookService; private void init() { weld = new Weld(); container = weld.initialize(); BookService = container.instance().select(BookService.class).get(); } private void start() { Book book = bookService.createBook("My title", "My description", 23.95); System.out.println(book); } private void shutdown() { weld.shutdown(); } public static void main(String[] args) { CdiApplication application = new CdiApplication(); application.init(); application.start(); application.shutdown(); } } 

Book Class:

 public class Book { // Book is a POJO private String title; private String description; private double price; private String id; // ISBN or ISSN private Date instanciationDate; public Book() { // default constructor } public Book(String title, String description, double price) { this.title = title; this.description = description; this.price = price; // the BookService is responsible to set the id (ISBN) and date fields. } // setters and getters // toString method } 

And the BookService class for creating a book and setting its initialization date and identifier (ISBN) using the NumberGenerator entered:

 public class BookService { @Inject private NumberGenerator numberGenerator; private Date instanciationDate; @PostConstruct private void setInstanciationDate() { instanciationDate = new Date(); } public Book createBook(String title, String description, double price) { Book book = new Book(title, description, price); book.setId(numberGenerator.generateNumber()); book.setDate(instanciationDate); return book; } } 

In a servlet environment, the servlet container is responsible for loading the CDI, which means that you need to deploy your "web application" in a Java EE container such as Tomcat or Wildfly.

+2
source share

All Articles