我创建了 JAX-RS 服务,我想在其中注入一个应用程序范围的 bean。问题是没有注入bean。这是如何引起的,我该如何解决?
JAX-RS 服务:
@Path("room")
public class RoomService {
@Inject
GameController gc;
public RoomService() {}
@Path("create")
@GET
@Produces("application/json")
public String create() {
Room r = new Room();
gc.addRoom(r); // gc is null
return r.toJson();
}
}
应用范围 bean
import java.util.ArrayList;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import pepuch.multuplayergameserver.entity.Game;
import pepuch.multuplayergameserver.entity.Room;
@Named
@ApplicationScoped
public class GameController {
private Game game;
public GameController() {
this.game = new Game(new ArrayList<Room>());
}
public boolean addRoom(Room room) {
if (!game.getRooms().contains(room)) {
return game.getRooms().add(room);
}
return false;
}
}