Weld和Java SE [英] Weld and Java SE
问题描述
我是Weld的新手,并且一直试图了解它的概念。我对Spring有一点经验,对Guice没有任何帮助,所以我几乎是DI框架的新手。
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.
这是一个介绍CDI的教程,但是在Web应用程序的上下文中。我很想知道它在Java SE中是如何工作的。我创建了以下类,但不知道如何在Java SE应用程序中使用DefaultItemDao类(或任何其他替代方法)测试ItemProcessor的execute方法。
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.
这里'是这些类:
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写一个吗?
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?
谢谢,Kumar
推荐答案
应该先用Google搜索。< br>
1-来自 Jboss博客
2-来自 Jboss代码库
Should have googled it first.
1- From Jboss blog
2- From Jboss code base
这篇关于Weld和Java SE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!