如何使用CDI和依赖注入

时间:2013-12-13 16:38:55

标签: jsf java-ee jsf-2 dependency-injection cdi

我想在Farma和Pata对象中使用相同的对象“User”。首先在Farma对象内初始化对象用户。我尝试使用@inject进行注释,但是Pata中的对象用户具有null值的名称。拜托,谁能帮我理解我做错了什么?谢谢!

@Named
@SessionScoped
public class Farma implements Serializable {
    @Inject private User user;

    @PostConstruct
    public void initialize(){
         user.setName("MyName");
    }
    // Getters and Setters
}

@Named
@SessionScoped
public class Pata implements Serializable {
    @Inject private  User user;

    public String getFuzzyName() {
        // Here I want to use the object "user" with the name "MyName" to do some logic
    }
    // Getters and Setters
}

public class User implements Serializable {
    private String name;

    // Getters and Setters

3 个答案:

答案 0 :(得分:1)

只是确定用户对象的范围不允许您初始化它。 使用“producer方法”来控制bean的创建。 试试这个:

@SessionScoped
public class Pata implements Serializable {
    @Inject
    @SessionUser // inject here using the producer method
    private  User user;

    public String getFuzzyName() {
        return user.getName();
    }
}
@SessionScoped
public class Farma implements Serializable {
    @Produces
    @SessionUser    // qualifier to tie injection points to this method
    @SessionScoped  // to ensure it will be called once per session for any number of injection points
    public User produceUser() {
        System.out.println("Creating user");
        User u = new User();
        u.setName("User");
        return u;
    }
}
////// that's your custom qualifier, it's in a separate file
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface SessionUser {}

// no scopes here, it is defined by the producer method
public class User implements Serializable {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

答案 1 :(得分:0)

您需要了解scoping of CDI bean。如果没有指定,则默认范围是@Dependent范围,这意味着一个对象只存在一个客户端(bean)并且具有与该客户端(bean)相同的生命周期。

在这种情况下,它意味着Farma中的用户仅存在于Farma类中并且在Farma类的生命中存在。

Pata中的用户是一个不同的实例,其生命周期与Pata的生命周期相匹配。

您需要正确定位User对象。

答案 2 :(得分:0)

正如axiopisty所说,添加@Named @SessionScoped是正确的方法。

我尝试过,效果很好。

@Named
@SessionScoped
public class Pata implements Serializable {
    @Inject
    private User user;

    public String getFuzzyName() {
        System.out.println(user.getName());
        return user.getName();
    }

    public User getUser() {
        return user;
    }

    public void setUser(final User user) {
        this.user = user;
    }
}

@Named
@SessionScoped
public class Farma implements Serializable {
    @Inject
    private User user;

    @PostConstruct
    public void initialize() {
        user.setName("MyName");
    }

    // Getters and Setters

    public User getUser() {
        return user;
    }

    public void setUser(final User user) {
        this.user = user;
    }
}


@Named
@SessionScoped
public class User implements Serializable {
    private String name = "Default";

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }
}


<h:outputText value="#{farma}"></h:outputText><br />
<h:outputText value="#{pata}"></h:outputText><br />
<h:outputText value="#{pata.fuzzyName}"></h:outputText>