I am a novice in weld, and through several days exploration but I only know some fundamental concept of weld.
I'm intended to use weld container in java se environment. And follw What is the easiest way to have CDI and JPA in Java SE? my code segment is below:
public class SayHello {
public SayHello(){}
public void sayHello(){
System.out.println("Hello");
}
}
import javax.inject.Inject;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
public class UseInject {
private SayHello obj;
public UseInject(){}
@Inject
public UseInject(SayHello obj){
this.obj = obj;
}
public void show(){
obj.sayHello();
}
public static void main(String[] args){
Weld weld = new Weld();
WeldContainer container = weld.initialize();
UseInject ui = container.instance().select(UseInject.class).get();
ui.show();
weld.shutdown();
}
}
and my application is based on maven. Here is the jar dependency segment in pom.xml
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.0.Alpha17</version>
my intention is inject SayHello object into UseInject Object so the final output of this application is "Hello". But things are not so smooth, the jvm report the follow error:
and through search from internet,there is a saying that build a empty beans.xml is ok, and I follow it can't make effect. And as for the main method I imitate What is the easiest way to have CDI and JPA in Java SE? I don't know what thing it do, and What is the easiest way to have CDI and JPA in Java SE? use the @Produces annotation, I don't know whether I should use it too. And I had intended use it for the SayHello Class, but I don't know which jar should I import in order to use it,so I give up.
Now I want to: 1. know how the weld service for se application in other words what things the main method does?
2. How can I run my application successfully using weld?
3. when we should use the annotation @Produces
And I made reference to a number of relevant issues e.g.weld and java seHow to bootstrap weld-se in a JUnit testetc. but find they are all to senior for me. thanks for your attention advance.
- How can I run my application successfully using weld?
The error message is telling you that you have to create a file named beans.xml
under the directory META-INF
. So to solve the problem just do the following:
META-INF
under src/main/resources
directorybeans.xml
under META-INF
so that your project is CDI enabled.
- when we should use the annotation @Produces
You use this annotation if you want to use any Java primitive types such as Integer, String, ... as CDI beans, or any type / class in an external library which does not contain the META-INF/beans.xml
file in the classpath.
Example:
public class SayHello {
@Produces
private double pi = 3.14159;
// the rest of the code
}
and you can inject the value of PI
in another place in your code as:
public class UseInject {
@Inject
private double pi; // 3.14159 will be injected here
// the rest of the code
public static void main(...) {
// ....
UseInject ui = container.instance().select(UseInject.class).get();
ui.show();
System.out.println(ui.pi);
// ...
}
The value 3.14159 should be displayed on the console.