开发者

Weld and Java SE

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开发者_如何学Go 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜