Weld 和 Java SE

发布于 2024-12-06 18:49:32 字数 1539 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔岸观火 2024-12-13 18:49:32

我对使用 JavaSE 的注入验证器有同样的问题。最后我设法解决了它。希望它能帮助别人!

我使用的依赖项:

        <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>

I had same question with injection Validator using JavaSE. Finally I managed to solve it. Hope it helps someone!

Dependencies i used:

        <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>

Main method:

    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);
    }
}

And also i created beans.xml in resources/META-INF directory:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文