Weld 和 Java SE
我是 Weld 的新手,一直在努力理解它的概念。我对 Spring 有一点经验,但对 Guice 没有任何经验,所以我对 DI 框架几乎是新手。
这是一个介绍 CDI 的教程,但是是在 Web 应用程序的上下文中。我很想知道这在 Java SE 中是如何工作的。我创建了以下类,但不知道如何在 Java SE 应用程序中使用 DefaultItemDao 类(或任何其他替代类)测试 ItemProcessor 的执行方法。
以下是这些类:
public class Item {
private int value;
private int limit;
public Item(int v, int l) {
value = v;
limit = l;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
@Override
public String toString() {
return "Item [value=" + value + ", limit=" + limit + "]";
}
}
import java.util.List;
public interface ItemDao {
List<Item> fetchItems();
}
import java.util.ArrayList;
import java.util.List;
public class DefaultItemDao implements ItemDao {
@Override
public List<Item> fetchItems() {
List<Item> results = new ArrayList<Item>(){{
add(new Item(1,2));
add(new Item(2,3));
}};
return results;
}
}
import java.util.List;
import javax.inject.Inject;
public class ItemProcessor {
@Inject
private ItemDao itemDao;
public void execute() {
List<Item> items = itemDao.fetchItems();
for (Item item : items) {
System.out.println("Found item: "+item);
}
}
}
我不知道如何为 ItemProcessor 类编写测试客户端。有人可以帮我理解如何用 CDI 编写一个吗?
谢谢,库马尔
I'm new to Weld and have been trying to get my head around it's concepts. I have a little experience with Spring and nothing with Guice, so I'm pretty much a novice with the DI frameworks.
Here's a tutorial that introduce CDI, but in the context of web apps. I'm interested to see how this works in Java SE alone. I have created the following classes, but have no idea how to test the ItemProcessor's execute method with the DefaultItemDao class (or any other alternative) in a Java SE app.
Here're the classes:
public class Item {
private int value;
private int limit;
public Item(int v, int l) {
value = v;
limit = l;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
@Override
public String toString() {
return "Item [value=" + value + ", limit=" + limit + "]";
}
}
import java.util.List;
public interface ItemDao {
List<Item> fetchItems();
}
import java.util.ArrayList;
import java.util.List;
public class DefaultItemDao implements ItemDao {
@Override
public List<Item> fetchItems() {
List<Item> results = new ArrayList<Item>(){{
add(new Item(1,2));
add(new Item(2,3));
}};
return results;
}
}
import java.util.List;
import javax.inject.Inject;
public class ItemProcessor {
@Inject
private ItemDao itemDao;
public void execute() {
List<Item> items = itemDao.fetchItems();
for (Item item : items) {
System.out.println("Found item: "+item);
}
}
}
And I have no idea how to write a test client for the ItemProcessor class. Can someone help me understand how to write one with CDI?
Thanks, Kumar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对使用 JavaSE 的注入验证器有同样的问题。最后我设法解决了它。希望它能帮助别人!
我使用的依赖项:
主要方法:
PurchaseOrderService.java
我还在 resources/META-INF 目录中创建了 beans.xml:
I had same question with injection Validator using JavaSE. Finally I managed to solve it. Hope it helps someone!
Dependencies i used:
Main method:
PurchaseOrderService.java
And also i created beans.xml in resources/META-INF directory: