java - 焊接和 Java SE

标签 java java-ee-6 cdi jboss-weld

我是 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 写一个吗?

谢谢,库马尔

最佳答案

我对使用 JavaSE 的注入(inject) validator 有同样的问题。最后我设法解决了它。希望对某人有所帮助!

我使用的依赖:

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.0.Alpha2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-cdi</artifactId>
            <version>6.0.0.Alpha2</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se</artifactId>
            <version>2.4.3.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.6</version>
        </dependency>

主要方法:

    Weld weld = new Weld().interceptors(Validator.class);
    WeldContainer container = weld.initialize();
    PurchaseOrderService service = 
    container.select(ru.code.service.PurchaseOrderService.class).get();
    Customer customer = new Customer(.....);
    service.createCustomer(customer);
    weld.shutdown();

PurchaseOrderService.java

@Inject
private Validator validator;
private Set<ConstraintViolation<Customer>> violations;   

public PurchaseOrderService() {
}

public void createCustomer(Customer customer) {
    violations = validator.validate(customer);
    if (violations.size() > 0) {
        throw new ConstraintViolationException(violations);
    }
}

我还在 resources/META-INF 目录中创建了 beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
        bean-discovery-mode="all">
</beans>

关于java - 焊接和 Java SE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7562472/

相关文章:

java - 根据某些 System.getProperty() 更改 web.xml

jakarta-ee - Java EE 6 中的 EJB 模块是什么?

jsf - 为什么有不同的bean管理注解

java - JSF : Passing an Object from one backing-bean to another backing-bean

java - 方法返回字符串与字符串变量之间的区别

Java - 链接列表泛型子列表

java - 扩展类+新参数

java - Jax ws rs 过滤器

Java EE CDI 实例 |如何获得真正的类(class)

java - 如何替换字符串中的 XML 标签? (还要检查它是否以 xml 标签开头)