在java se 环境中应用程序的焊接容器服务如何?
how the weld container service for application in java se environment?
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.
现在我想:
1.知道 se 应用程序的焊接服务,换句话说,主要方法做了什么?
2。我怎样才能 运行 我的应用程序成功使用焊接?
3。什么时候应该使用注解@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?
错误消息告诉您必须在目录 META-INF
下创建一个名为 beans.xml
的文件。所以要解决这个问题,只需执行以下操作:
src/main/resources
目录下名为 META-INF
的目录
- 在
META-INF
下创建一个名为 beans.xml
的空文件,以便您的项目启用 CDI。
- when we should use the annotation @Produces
如果您想使用任何 Java 原始类型(例如 Integer、String...)作为 CDI bean,或外部库中的任何类型/class 不包含class路径中的META-INF/beans.xml
文件。
示例:
public class SayHello {
@Produces
private double pi = 3.14159;
// the rest of the code
}
并且您可以将 PI
的值注入代码中的另一个位置,如:
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);
// ...
}
值 3.14159 应该显示在控制台上。
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.
现在我想: 1.知道 se 应用程序的焊接服务,换句话说,主要方法做了什么?
2。我怎样才能 运行 我的应用程序成功使用焊接?
3。什么时候应该使用注解@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?
错误消息告诉您必须在目录 META-INF
下创建一个名为 beans.xml
的文件。所以要解决这个问题,只需执行以下操作:
src/main/resources
目录下名为META-INF
的目录- 在
META-INF
下创建一个名为beans.xml
的空文件,以便您的项目启用 CDI。
- when we should use the annotation @Produces
如果您想使用任何 Java 原始类型(例如 Integer、String...)作为 CDI bean,或外部库中的任何类型/class 不包含class路径中的META-INF/beans.xml
文件。
示例:
public class SayHello {
@Produces
private double pi = 3.14159;
// the rest of the code
}
并且您可以将 PI
的值注入代码中的另一个位置,如:
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);
// ...
}
值 3.14159 应该显示在控制台上。