package controller;
import java.io.Serializable;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import lombok.Data;
import lombok.extern.java.Log;
import entities.Commission;
@SessionScoped
@Data
@Log
@Named
public class CommissionController implements Serializable {
private static final long serialVersionUID = -8452887234021054225L;
@Inject
private UserController userController;
@Inject
private CartController cartController;
private Commission commission;
@PostConstruct
public void init() {
commission = new Commission();
log.info("new CommissionController instance");
}
public void makeNewCommission() {
commission.setCart(cartController.getCart());
commission.setOrderDate(new Date());
commission.setOrderer(userController.getUser());
cartController.clearCart();
log.info(commission.toString());
}
}
现在我有一个不同的bean,但它认为它是完全相同的结构:
package controller;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import lombok.Data;
import lombok.extern.java.Log;
import entities.Article;
import entities.Cart;
@SessionScoped
@Data
@Log
@Named
public class CartController implements Serializable {
private static final long serialVersionUID = 649140288918816488L;
@Inject
private UserController userController;
@Inject
private ArticleController articleController;
private Cart cart;
public void addToCart(Article article) {
cart.getArticleList().add(article);
cart.setSum(cart.getSum() + article.getPrice());
}
public void clearCart() {
cart.getArticleList().clear();
cart.setSum(0.0);
}
@PreDestroy
public void destroy() {
log.info("bean destroyed");
}
@PostConstruct
public void init() {
cart = new Cart();
log.info("new CartController instance");
}
public void removeFromCart(Article article) {
cart.getArticleList().remove(article);
cart.setSum(cart.getSum() - article.getPrice());
}
}
我无法向自己解释,但是第二个bean一次又一次地实例化,我无法在其中存储任何内容,因为它总是引用另一个bean。
请帮助我揭开这个神秘面纱。如果您需要其他信息,我很乐意给您。也许,我非常缺乏理解,但就目前而言,我无法帮助自己。
答案 0 :(得分:1)
Gimby给了我这个问题的答案。实际上,JBoss AS 7.1.0社区版已经过时了。使用WildFly 8.0.0 Final,一切都运行得很好。因此,如果您正在使用CDI的奇妙可能性,请尝试使用WildFly而不是JBoss AS 7.1.0最终社区。 p>